Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm attempting to pass a GUID to a

Status
Not open for further replies.

OrWolf

MIS
Mar 19, 2001
291
I'm attempting to pass a GUID to a SQL statement however first I figured that I needed to change it from a string back into a GUID. I'm attempting to use the ConvertGuid to do this but I'm getting the following error.

Error:
System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
at System.Guid..ctor(String g)
at System.ComponentModel.GuidConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.TypeConverter.ConvertFrom(Object value)


Relevent code:
Dim gExamID As New Guid("B80D56EC-5899-459d-83B4-1AE0BB8418E4")
gExamID = System.ComponentModel.TypeDescriptor.GetConverter(gExamID).ConvertFrom(Me.txtExamID.Text)


The value of me.txtExamID.Text is: 9bb796c4-9c77-47d6-b47d-221d62d9e778


Any assistance or pointers would be greatly appreciated. Thanks!
 
Actually here's what I would suggest:

Dim gExamID as new Guid("B80D56EC-5899-459d-83B4-1AE0BB8418E4")

sql = "INSERT INTO Table (gExamID)
Values('" & gExamID.toString & "')"

Just just using the toString will work. Hopefully this helps.
 
Any time I attempt to pass in a string value, as suggested above, I receive the following error.

Error:
Value of type 'String' cannot be converted to 'System.Data.SQLClient.SQLCommand'.


Statement:
objDataAdapter = New SqlDataAdapter("SELECT ExamID, ExamName FROM Exam WHERE ExamID = '" & gExamID.ToString & "', objConnection")
 
Sorry I generally create my sql statement using the Text.Stringbuilder object and then the SQLDataReader to read it. Anyhow, I looked into the SqlDataAdapter in the MSDN Library and found some sample code. Hopefully this will give you some ideas if you haven't already found this example:

Public Shared Function CreateCustomerAdapter(conn As SqlConnection) As SqlDataAdapter

Dim da As SqlDataAdapter = New SqlDataAdapter()
Dim cmd As SqlCommand

' Create the SelectCommand.

cmd = New SqlCommand("SELECT * FROM Customers " & _
"WHERE Country = @Country AND City = @City", conn)

cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15)
cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15)

da.SelectCommand = cmd

' Create the InsertCommand.

cmd = New SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " & _
"VALUES (@CustomerID, @CompanyName)", conn)

cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")

da.InsertCommand = cmd

Return da
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top