locked
Conversion failed when converting from a character string to uniqueidentifier RRS feed

  • Question

  • User-684658481 posted

     Trying simple delete against a Photo table but hitting error as conversion type error. Any suggestion welome!

     

                connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Ero2ConnectionString1"].ConnectionString.ToString());
                connection.Open();
                MembershipUser currentUser = Membership.GetUser();  // Get a reference to the currently logged on user
                Guid currentUserId = (Guid)currentUser.ProviderUserKey;// Determine the currently logged on user's UserId value
                string sql = "Delete FROM Photos WHERE UserId = 'currentUser'";
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.ExecuteScalar();
                connection.Close();


     

    -		$exception	{"Conversion failed when converting from a character string to uniqueidentifier."}	System.Exception {System.Data.SqlClient.SqlException}
    


     

    Is there a better way to write this sql statement? or convert the UserId to int32 before executing? Not sure how to do that.

    Wednesday, January 12, 2011 3:28 PM

Answers

  • User-1998067147 posted

    Hi,

    can you check the value of currentuser.ProviderUserKey? 

    Also give a try to :

    Guid currentUserId = new Guid(currentUser.ProviderUserKey.ToString());// Determine the currently logged on user's UserId value  
    string sql = "Delete FROM Photos WHERE UserId = '" + currentUserId.ToString() + "'";
    
    //or you can simply use
    
    string sql = "Delete FROM Photos WHERE UserId = '" + currentUser.ProviderUserKey.ToString() + "'";

    Please note that it is better to use a parameterized query. 

    Regards,

    Ahsan Muhammad Khan


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 13, 2011 5:35 PM

All replies

  • User-1998067147 posted

    Hi,

    Change your statement to

    Guid currentUserId = new Guid(currentUser.ProviderUserKey);

    Regards,

    Ahsan Muhammad Khan

    Wednesday, January 12, 2011 3:38 PM
  • User187056398 posted

    string sql = "Delete FROM Photos WHERE UserId = 'currentUser'";

    Your sql string should look something like this:

    "Delete FROM Photos WHERE UserId = 'BF4347FA-C7FD-41ED-88DF-168E5DFD5952'  "

    However, note that most people on this site will tell you to use parameters.

    Wednesday, January 12, 2011 3:43 PM
  • User-684658481 posted

    That sounds great, but how do you get the database to return that Id # in that format specific to the user that is logged in? That number needs to be specific the record that I am trying to delete. Parameters are easier nbut I must have mine messed up somehow. 

    Wednesday, January 12, 2011 4:18 PM
  • User-684658481 posted

    Ahsan, That won't go!

    I replaced my  

    Guid currentUserId = (Guid)currentUser.ProviderUserKey;

    With your suggestion but it wouldn't accept is as is. Not sure why

     

     

     

    Wednesday, January 12, 2011 4:23 PM
  • User-1998067147 posted

    Hi,

    can you check the value of currentuser.ProviderUserKey? 

    Also give a try to :

    Guid currentUserId = new Guid(currentUser.ProviderUserKey.ToString());// Determine the currently logged on user's UserId value  
    string sql = "Delete FROM Photos WHERE UserId = '" + currentUserId.ToString() + "'";
    
    //or you can simply use
    
    string sql = "Delete FROM Photos WHERE UserId = '" + currentUser.ProviderUserKey.ToString() + "'";

    Please note that it is better to use a parameterized query. 

    Regards,

    Ahsan Muhammad Khan


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 13, 2011 5:35 PM
  • User-684658481 posted

    Hi Ahsan,

    Here is how I finished up & all works fine. I needed the add Parameters - Thanks for the input!

                connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Ero2ConnectionString1"].ConnectionString.ToString());
                connection.Open();
                MembershipUser currentUser = Membership.GetUser();  // Get a reference to the currently logged on user
                Guid currentUserId = (Guid)currentUser.ProviderUserKey;// Determine the currently logged on user's UserId value
                string sql = "Delete FROM Photos WHERE UserId = @currentUserId";
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.Parameters.Add("@currentUserId", SqlDbType.VarChar).Value = currentUserId.ToString();
                cmd.ExecuteScalar();
                connection.Close();


     

     

    Thursday, January 13, 2011 7:23 PM