Solved

"Failed to convert parameter value from a String to a Guid." - Error - vb.net

Posted on 2014-12-04
3
519 Views
Last Modified: 2014-12-04
The fields UserID and SetID are UniqueIdentifiers in the database, and somehow I'm adding them wrong.

Code below is doing an update and fails on "setCmd.ExecuteNonQuery()"

tnx
~j


        sqlConn.Open()

        Dim saveCommand As String
        If CurEdit <> "" Then
            saveCommand = "UPDATE Settings SET Setting = @Setting,Value = @Value,Encrypted = @Encrypted, Description = @Description WHERE setID = @SetID"
        Else
            saveCommand = "INSERT INTO Settings (SetID,UserID,Application,Setting, Value, Encrypted, Description) VALUES(@SetID,@UserID,@Application,@Setting, @Value, @Encrypted, @Description)"
        End If

        Dim setCmd As New SqlCommand(saveCommand, sqlConn)
        setCmd.Parameters.Add("@Setting", Data.SqlDbType.NVarChar).Value = SettingStr
        setCmd.Parameters.Add("@Value", Data.SqlDbType.NVarChar).Value = ValueStr
        setCmd.Parameters.Add("@UserID", Data.SqlDbType.UniqueIdentifier).Value = New Guid("1CF96072-5A51-4CCB-B171-7E84EE1E9E31")
        setCmd.Parameters.Add("@Application", Data.SqlDbType.NVarChar).Value = "852VMI"
        setCmd.Parameters.Add("@Encrypted", Data.SqlDbType.Bit).Value = EncryptRB
        setCmd.Parameters.Add("@Description", Data.SqlDbType.NVarChar).Value = DescStr

        If CurEdit <> "" Then
            setCmd.Parameters.Add("@SetID", Data.SqlDbType.UniqueIdentifier).Value = CurEdit
        Else
            setCmd.Parameters.Add("@SetID", Data.SqlDbType.UniqueIdentifier).Value = Guid.NewGuid.ToString
        End If

        setCmd.ExecuteNonQuery()
        sqlConn.Close()
        sqlConn.Dispose()

Open in new window

0
Comment
Question by:prosit
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
Walter Padrón earned 500 total points
ID: 40481543
Convert your Guid to SqlGuid,  see http://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqlguid.aspx

setCmd.Parameters.Add("@UserID", SqlDbType.UniqueIdentifier).Value = new System.Data.SqlTypes.SqlGuid("1CF96072-5A51-4CCB-B171-7E84EE1E9E31"); 

Open in new window


Best regards
0
 
LVL 2

Author Comment

by:prosit
ID: 40481588
Nah, still giving the same message...

Any other ideas?


        sqlConn.Open()

        Dim saveCommand As String
        If CurEdit <> "" Then
            saveCommand = "UPDATE Settings SET Setting = @Setting,Value = @Value,Encrypted = @Encrypted, Description = @Description WHERE setID = @SetID"
        Else
            saveCommand = "INSERT INTO Settings (SetID,UserID,Application,Setting, Value, Encrypted, Description) VALUES(@SetID,@UserID,@Application,@Setting, @Value, @Encrypted, @Description)"
        End If

        Dim setCmd As New SqlCommand(saveCommand, sqlConn)
        setCmd.Parameters.Add("@Setting", SqlDbType.NVarChar).Value = SettingStr
        setCmd.Parameters.Add("@Value", SqlDbType.NVarChar).Value = ValueStr
        setCmd.Parameters.Add("@UserID", SqlDbType.UniqueIdentifier).Value = New System.Data.SqlTypes.SqlGuid("1CF96072-5A51-4CCB-B171-7E84EE1E9E31")

        setCmd.Parameters.Add("@Application", SqlDbType.NVarChar).Value = "852VMI"
        setCmd.Parameters.Add("@Encrypted", SqlDbType.Bit).Value = EncryptRB
        setCmd.Parameters.Add("@Description", SqlDbType.NVarChar).Value = DescStr

        If CurEdit <> "" Then
            setCmd.Parameters.Add("@SetID", SqlDbType.UniqueIdentifier).Value = New System.Data.SqlTypes.SqlGuid(CurEdit)
        Else
            setCmd.Parameters.Add("@SetID", SqlDbType.UniqueIdentifier).Value = New System.Data.SqlTypes.SqlGuid(Guid.NewGuid.ToString)
        End If

        setCmd.ExecuteNonQuery()
        sqlConn.Close()
        sqlConn.Dispose()

Open in new window

0
 
LVL 2

Author Closing Comment

by:prosit
ID: 40481611
Figured it out, thanks for getting me on track!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now