Solved

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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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