2017-05-04 104 views
2

我试图获取当前页面的当前GUID并将值保存到SQL中。当页面加载时,它将GUID加载到标签中。所以我想从标签中取出GUID并放入到sql中,但它给了我错误:无法将参数值从字符串转换为Guid。这是我的代码:无法将参数值从字符串转换为Guid c#aspx

protected void btnAddOGCoverageTeamMembers_Click(object sender, EventArgs e) 
    { 
     try 
     { 

      string AccountGUID = base.Request["account"]; 
      guidget.Text = AccountGUID.ToString(); 

      Hashtable htAMTeam = new Hashtable(); 
      htAMTeam["GUID"] = guidget.Text; 
      htAMTeam["UserID"] = Convert.ToInt32(Person.SelectedValue); 
      htAMTeam["Location"] = Location.SelectedValue; 
      htAMTeam["Product"] = Product.Text; 
      obj.addTeam(htAMTeam); 

     } 

     catch (Exception ex) 
     { 
      lblMsg.Text = ex.Message; 
     } 

    } 

这是代码隐藏页:页面的.cs

public void addTeam(Hashtable htAMTeam) 
{ 
    SqlParameter[] OGTeamparam = new SqlParameter[4]; 
    OGTeamparam[0] = new SqlParameter("@AccountGUID", SqlDbType.UniqueIdentifier); 
    OGTeamparam[1] = new SqlParameter("@UserID", SqlDbType.Int); 
    OGTeamparam[2] = new SqlParameter("@Location", SqlDbType.VarChar, 50); 
    OGTeamparam[3] = new SqlParameter("@Product", SqlDbType.VarChar, 50); 


    OGTeamparam[0].Value = htAMTeam["AccountGUID"]; 
    OGTeamparam[1].Value = htAMTeam["UserID"].ToString(); 
    OGTeamparam[2].Value = htAMTeam["Location"].ToString(); 
    OGTeamparam[3].Value = htAMTeam["Product"].ToString(); 
    SqlHelper.ExecuteNonQuery(OGconnection, CommandType.StoredProcedure, "InsertAccountOGTeam", OGTeamparam); 
} 

回答

1

我已经得到了它工作,这没有诀窍:

OGTeamparam[0].Value = new Guid(Convert.ToString(htAMTeam["AccountGUID"])); 
4

相反

OGTeamparam[0].Value = htAMTeam["AccountGUID"]; 

使用

OGTeamparam[0].Value = new Guid((string)htAMTeam["AccountGUID"]); 
+0

我得到这个错误:CS0246:类型或命名空间名称“GUID”找不到(是否缺少using指令或程序集引用?) –

+1

@ParbhuBissessar是我不好,我missspelled GUID。使用Guid。我编辑了我的答案。 – BWA

+0

感谢和抱歉我得到了另一个错误:'System.Guid.Guid(byte [])'的最佳重载方法匹配有一些无效参数 –

相关问题