0
votes

I've a datatable with a column containing GUIDs. I want to select a row matching a specific GUID. I wrote the following code,

DataRow[] dRows = dtListSettings.Select("ListGUID = " + Convert.ToString(ViewState["GUID"]));

The GUID i'm comparing is 500c2b6a-a3a7-457f-90ed-c96768d91520. But i'm getting the error - Syntax error: Missing operand after 'c2b6a' operator.

Any ideas?

Thank you NLV

4
Yah, what @ozczecho said. Without the quotes, you're creating a select statement that is attempting a math operation (-). - Joel Etherton

4 Answers

6
votes

Need a single quote:

Something like:

string.Format("ListGUID = '{0}'", Convert.ToString(ViewState["GUID"]));
2
votes

Try surrounding your select statement parameter in single quotes, like this:

DataRow[] dRows = dtListSettings.Select("ListGUID = '" + Convert.ToString(ViewState["GUID"]) + "'");
2
votes

This does not work for IN. E.g.

DataRow[] dRows = dtListSettings.Select("ListGUID IN ('" + Convert.ToString(ViewState["GUID"]) + "')");
0
votes

I resolve this with two bellow methods:

DataTable dt = new DataTable(); // your datatable with data
Guid gid = Guid.NewGuid(); // searching guid
DataRow[] dra = dt.Select("GuidColumn = '" + gid.ToString() + "'")
// OR
DataRow[] dra = dt.Select("GuidColumn = Convert('" + gid.ToString() + "', 'System.Guid')");

hope be useful ;)