Solved

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

Posted on 2014-12-04
3
604 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

615 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