2012-07-31 159 views
1

當我嘗試在ListView中編輯我的文本並隨後在數據庫中更新它時,我正面臨此錯誤。此錯誤消息顯示當我點擊ListView中的更新按鈕時。由於我的UserID是唯一標識符,因此無法更新到我的數據庫。但我真的不知道在我的代碼中做了哪些更改。System.Data.SqlClient.SqlException:不允許將數據類型sql_variant隱式轉換爲uniqueidentifier。

我的錯誤信息表明這一點:在的.cs

Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query. 

我的SQL語句:

protected void updateComment(object sender, ListViewUpdateEventArgs e) 
{ 

    MembershipUser currentUser = Membership.GetUser(); 
    Guid currentUserId = (Guid)currentUser.ProviderUserKey; 

    var ID= ListView1.DataKeys[e.ItemIndex].Value.ToString(); 

    string connectionString = ConfigurationManager.ConnectionStrings["ASPNETConnectionString"].ConnectionString; 
    SqlConnection myConnect = new SqlConnection(connectionString); 

    string updateSql = "UPDATE Review set ContentRev [email protected] WHERE ID = @ID"; 

    using (SqlConnection myConnection = new SqlConnection(connectionString)) 
    { 
     myConnection.Open(); 

     SqlCommand myCommand = new SqlCommand(updateSql, myConnection); 
     myCommand.Parameters.AddWithValue("@ID", ID); 
     myCommand.Parameters.AddWithValue("@ContentRev ", TextBox1.Text.Trim()); 

     myCommand.ExecuteNonQuery(); 

     myConnection.Close(); 

     ListView1.DataBind(); 
     } 

我的用戶ID數據類型是對象和ContentRev是字符串。

+0

不確定你爲什麼提到用戶的ID?它不是SQL的一部分。 – 2012-07-31 12:58:16

回答

4

這是你的問題:

var ID = ListView1.DataKeys[e.ItemIndex].Value.ToString(); 

如果這是一個唯一標識符(或GUID),它應該閱讀:

Guid ID = new Guid(ListView1.DataKeys[e.ItemIndex].Value.ToString()); 

其實,我覺得var ID將工作,只要在右側是一個GUID。

+0

明確地看起來像這樣,這使它成爲一個奇怪的錯誤信息。 – 2012-07-31 12:59:24

+0

我改變了你的建議代碼,但遇到了這個新的錯誤:System.FormatException:Guid應該包含32個數字,4破折號(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。 – kelly 2012-07-31 17:23:52

+0

你能舉一個例子說明這個領域是什麼嗎? – 2012-07-31 19:19:52

相關問題