kelly
kelly

Reputation: 117

System.Data.SqlClient.SqlException: Implicit conversion from data type sql_variant to uniqueidentifier is not allowed.

I am facing this error when i try to edit my text in the ListView and subsequently update it in the database. This error message shows when i click on the update button in my ListView. As my UserID is a uniqueIdentifier, it cannot be updated to my database. But i do not really know what changes to make in my codes.

My error message shows this:

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

My SQL Statement in .cs:

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();
        }

My datatype for Userid is Object and ContentRev is String.

Upvotes: 1

Views: 2155

Answers (1)

Steen Tøttrup
Steen Tøttrup

Reputation: 3835

This is your problem:

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

If this is a uniqueIdentifier (or GUID), it should read:

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

I actually think var ID will work, as long as the right side is a GUID.

Upvotes: 4

Related Questions