当我尝试在ListView中编辑文本并随后在数据库中对其进行更新时,我遇到此错误。当我在ListView中单击更新按钮时,将显示此错误消息。由于我的用户ID是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。