![](/img/trans.png)
[英]Implicit conversion from data type sql_variant to varchar is not allowed
[英]System.Data.SqlClient.SqlException: Implicit conversion from data type sql_variant to uniqueidentifier is not allowed.
當我嘗試在ListView中編輯我的文本並隨后在數據庫中更新它時,我遇到此錯誤。 當我單擊ListView中的更新按鈕時,會顯示此錯誤消息。 由於我的UserID是uniqueIdentifier,因此無法更新到我的數據庫。 但我真的不知道我的代碼中會做出哪些更改。
我的錯誤消息顯示:
Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query.
我在.cs中的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 =@ContentRev 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();
}
我的Userid數據類型是Object,ContentRev是String。
這是你的問題:
var ID = ListView1.DataKeys[e.ItemIndex].Value.ToString();
如果這是uniqueIdentifier(或GUID),則應顯示為:
Guid ID = new Guid(ListView1.DataKeys[e.ItemIndex].Value.ToString());
我實際上認為var ID
會起作用,只要右側是GUID即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.