We help IT Professionals succeed at work.

Invalid cast from 'System.String' to 'System.Guid'.

Wigging
Wigging asked
on
this line is giving me error
 cmd.Parameters.Add("@r", SqlDbType.UniqueIdentifier).Value = Request.QueryString("sequence")


strConn = ConfigurationManager.ConnectionStrings("ussl").ToString
        myconn = New SqlConnection(strConn)
        myconn.Open()
        Dim cmd As New SqlCommand("wmItemOptionFullQuoteSp", myconn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@co_num", SqlDbType.NVarChar).Value = "Q000330351"
        cmd.Parameters.Add("@r", SqlDbType.UniqueIdentifier).Value = Request.QueryString("sequence")
        mydr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

Open in new window

Comment
Watch Question

Try this,

cmd.Parameters.Add("@r", SqlDbType.UniqueIdentifier).Value =  CType(Request.QueryString("sequence"), System.Guid)
By the way, if you use SqlDbType.UniqueIdentifier data type, you need to populate the value with a global unique identifier value (Guid).  You can use System.Guid.NewGuid.ToString to create a new guid.

Author

Commented:
when i add this in code it says   value of type string cannot be converted guid

Author

Commented:
my querystring looks like this
printquotethanks3.aspx?&sequence=4f0ad53f-66c1-4e1d-88ce-63b47646a0d1
Top Expert 2009

Commented:
This is the way to convert a string to a guid:

Dim guidValue As String = "4f0ad53f-66c1-4e1d-88ce-63b47646a0d1"
Dim guid As New Guid(guidValue)

Arun
Try this,

cmd.Parameters.Add("@r", SqlDbType.UniqueIdentifier).Value =  New Guid(Request.QueryString("sequence"))