Solved

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

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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