3

When I run my code on debug I get this error:

Conversion failed when converting from a string to a uniqueidentifier

Here is the code:

public class UserObject
{
    private string m_name = string.Empty;

    public UserObject(string id)
    {
    #region Internal Logic
        try
        {
            using (SqlConnection cn = new SqlConnection(SiteConfig.ConnectionString))
            {
                string sSQL = "SELECT [UserName] FROM [aspnet_users] WHERE [UserID] = @UserID";
                using (SqlCommand cm = new SqlCommand(sSQL, cn))
                {
                    cm.Parameters.AddWithValue("@UserID", id);
                    cn.Open();
                    using (SqlDataReader rd = cm.ExecuteReader())
                    {
                        while (rd.Read())
                        {
                            m_name = rd[0].ToString();
                        }
                        rd.Close();
                    }
                    cn.Close();
                }
            }
        }
        catch (Exception ex)
        {

        }
    #endregion Internal logic
    }
}
Bridge
  • 29,818
  • 9
  • 60
  • 82
blanket
  • 79
  • 2
  • 6
  • 1
    Is UserId a UniqueIdetifier in your table? If so what is the value if `id` when you debug? – Jamiec May 11 '12 at 11:45
  • 2
    have you tried Guid.Parse(id) ? – daryal May 11 '12 at 11:46
  • 8
    Do you *really* have a completely empty catch block, catching *all* exceptions? That's not a good idea, and may well be hiding what's *really* going wrong. – Jon Skeet May 11 '12 at 11:46
  • yes my UserId is a uniqueIdentifier and the id does not have a value.. – blanket May 11 '12 at 11:49
  • check if the id is "string.empty" or "DBNull.Value" then create "guid.empty" and use it. – pvaju896 May 11 '12 at 11:59
  • try using catch block and find it. That'll be grt*. Use ex.StackTrace and identify it.... – pvaju896 May 11 '12 at 12:00
  • `""` is not `null`!! `null` is `null` (which is not a valid `Guid`), `""` is an empty string, which is also not a valid `Guid`. – Thorsten Dittmar May 11 '12 at 12:04
  • the id is string.empty...and i dont have much info on the guid.empty or how i can apply it.. – blanket May 11 '12 at 12:05
  • I've added some code to my answer. This handles three cases: `id == null`, `id == ""` and `id == valid Guid`. This should give you hints. – Thorsten Dittmar May 11 '12 at 12:08
  • Guid test = new Guid(string.Empty); Console.Write(test); try the above two line of codes and see what happens - this is how string.empty works with guid(gives you error). – pvaju896 May 11 '12 at 12:14
  • And Empty guid is this one; Guid mine1 = Guid.Empty; Console.Write(mine1.ToString()); outputs->00000000-0000-0000-0000-000000000000 – pvaju896 May 11 '12 at 12:15
  • And even this gives you error - > Guid check = new Guid(); check = null; "Guid cannot be null(is non-nullable)" – pvaju896 May 11 '12 at 12:17

1 Answers1

4

You said in your comment to the question that id does not have a value when being passed into the method. From the database point of view, uniqueidentifiers can be null (DBNull in C#), but to achieve this, you'd have to leave out the parameter or set DBNull.Value explicitly.

In C#, Guid can not be null - so you must either provide Guid.Empty or a string that can be converted to a Guid in the call to AddWithValue.

EDIT
Sample code follows: Please note that given the SQL statement you use, you won't get any results for the Guid.Empty case unless you have a user the ID of which contains only 0s. I suggest, you change the where clause of your SQL statement as follows:

WHERE [UserId] = ISNULL(@UserID, [UserId])

That way, you get all users when you pass null.

public UserObject(string id)
{
    try
    {
        using (SqlConnection cn = new SqlConnection(SiteConfig.ConnectionString))
        {
            string sSQL = "SELECT [UserName] FROM [aspnet_users] WHERE [UserID] = @UserID";
            using (SqlCommand cm = new SqlCommand(sSQL, cn))
            {

                if (id.Length == 0)
                    cm.Parameters.AddWithValue("@UserID", Guid.Empty);
                else if (id == null)
                    cm.Parameters.AddWithValue("@UserID", DBNull.Value);
                else
                    cm.Parameters.AddWithValue("@UserID", Guid.Parse(id));

                cn.Open();
                using (SqlDataReader rd = cm.ExecuteReader())
                {
                    while (rd.Read())
                    {
                        m_name = rd[0].ToString();
                    }
                    rd.Close();
                }
                cn.Close();
            }
        }
    }
    catch (Exception ex)
    {

    }
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • Guid.Empty?? not familiar with it, how can i use it? – blanket May 11 '12 at 12:11
  • Shown in my code. Basically: `Guid myGuid = Guid.Empty;`. Please note that `Guid.Empty` is `00000000-0000-0000-0000-000000000000`; – Thorsten Dittmar May 11 '12 at 12:11
  • Non-code specific answer is [here](https://stackoverflow.com/questions/20940350/conversion-failed-when-converting-from-a-character-string-to-uniqueidentifier) but is probably relevant. – B5A7 Nov 04 '21 at 01:14