当我尝试在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。
答案 0 :(得分:4)
这是你的问题:
var ID = ListView1.DataKeys[e.ItemIndex].Value.ToString();
如果这是uniqueIdentifier(或GUID),则应显示为:
Guid ID = new Guid(ListView1.DataKeys[e.ItemIndex].Value.ToString());
我实际上认为只要右侧是GUID,var ID
就会起作用。