Solved

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

Posted on 2014-12-04
3
551 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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