uniqueidentifier in SQL Query

K

Kenneth Keeley

Hi,
I am trying to get the results out of an SQL database where the Id is
equal to a given value. The Id field is a uniqueidentifier this is what I am
trying to do.

SQLQuery = new SqlDataAdapter("SELECT Id, Title, Publication, InsertDate,
NoPages, Colour, Status " +
"FROM Features WHERE Id={guid {" + Request.QueryString[ "id" ] + "}};",
DataBaseConnection);

The Querystring value looks like this "3B9767E0-56DD-49FB-8CC0-79A8FE95CEFF"

I keep getting errors and I would like to find a sample of how to do this.

Thanks
Kenneth.
 
L

Lucas Tam

The Querystring value looks like this
"3B9767E0-56DD-49FB-8CC0-79A8FE95CEFF"

I keep getting errors and I would like to find a sample of how to do
this.

You need to encapsulate your ID like:

ID='{3B9767E0-56DD-49FB-8CC0-79A8FE95CEFF}'.

Make sure you have the single quotes and curly brackets.

To insert into a GUID field, you need to use the NewID() function to
generate a GUID.
 
G

Greg Burns

(untested)

Dim cmd As New SqlCommand("SELECT Id, Title, Publication, InsertDate,
NoPages, Colour, Status FROM Features WHERE Id=@guid", DataBaseConnection)

cmd.Parameters.Add(New SqlParameter("@guid", SqlDbType.UniqueIdentifier,
16)).Value = New Guid(Request.QueryString("id").ToString)

Dim da as New SqlDataAdapter(cmd)

Greg
 
K

Kenneth Keeley

Thanks that fixed it.

Lucas Tam said:
You need to encapsulate your ID like:

ID='{3B9767E0-56DD-49FB-8CC0-79A8FE95CEFF}'.

Make sure you have the single quotes and curly brackets.

To insert into a GUID field, you need to use the NewID() function to
generate a GUID.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top