DataTable.select problem with variables

  • Thread starter Thread starter Manikandan
  • Start date Start date
M

Manikandan

Hi,
I have a datatable with rows.
When I used datatable.select with values it is working properly,
But when I use the select with variables it is not working.
I tried with putting '(single quote),"" '(double quote) and many
combination, it is not returning any rows
Sample code for demonstartion
DataTable aDataTable = new DataTable();
aDataTable.Columns.Add("s1", typeof(string));
aDataTable.Columns.Add("s2", typeof(string));
aDataTable.Columns.Add("s3", typeof(string));
aDataTable.Columns.Add("s4", typeof(string));
for(int i=0;i<10;i++)
{
DataRow dr = aDataTable.NewRow();
dr["s1"]=i.ToString();
dr["s2"]=i.ToString();
dr["s3"]=i.ToString();
dr["s4"]=i.ToString();
aDataTable.Rows.Add(dr);
}
string v1="1";
string v2="2";
string qry="s1= '" + v1 + "' AND s2='" + v2 + "'" ;
DataRow[] foundRows=aDataTable.Select(qry);
if(foundRows.Length>0)
{
// process here
}

The above code won't return any rows.
But if I use like this means
string qry="s1='1' AND s2='1'";
It is returning rows(i.e foundRows.Length>0) .
I don't what is the difference between the value and variable in
select statement.
I would like to know,as I'm missing quotes or syntax mistake for
variable in select statement.

Thank You,
Regards,
Mani
 
I would like to know,as I'm missing quotes or syntax mistake for
variable in select statement.

Your code is trying to find rows with s1=1 and s2=2. There are no rows
matching that query, because for each of your test rows, s1=s2.

Jon
 
Your code is trying to find rows with s1=1 and s2=2. There are no rows
matching that query, because for each of your test rows, s1=s2.

Jon

Hi,
Sorry i mistakenly used
string v2="2";
It's also "1"
only
i.e string v2="1";

DataTable aDataTable = new DataTable();
aDataTable.Columns.Add("s1", typeof(string));
aDataTable.Columns.Add("s2", typeof(string));
aDataTable.Columns.Add("s3", typeof(string));
aDataTable.Columns.Add("s4", typeof(string));
for(int i=0;i<10;i++)
{
DataRow dr = aDataTable.NewRow();
dr["s1"]=i.ToString();
dr["s2"]=i.ToString();
dr["s3"]=i.ToString();
dr["s4"]=i.ToString();
aDataTable.Rows.Add(dr);
}
string v1="1";
string v2="1";
string qry="s1= '" + v1 + "' AND s2='" + v2 +
"'" ;
DataRow[] foundRows=aDataTable.Select(qry);
if(foundRows.Length>0)
{
// process here
}


I'm actually taking the values from database(sql server) and using in
select statement.
For demonstration only i used above code, is there any chance of
problem from sql server 2000


Thank You,
Regards,
Mani
 
I'm actually taking the values from database(sql server) and using in
select statement.
For demonstration only i used above code, is there any chance of
problem from sql server 2000

Possibly - it's hard to know for sure.

Could you post a short but complete program which demonstrates the
problem?
See http://pobox.com/~skeet/csharp/complete.html for more details.

Jon
 
Possibly - it's hard to know for sure.

Could you post a short but complete program which demonstrates the
problem?
Seehttp://pobox.com/~skeet/csharp/complete.htmlfor more details.

Jon


Hi,
Thanks Jon
I found the mistake after adding the single quotes i gave a space and
then double quotes, while debugging it doesn't show the space(unable
to find the space while debugging, so i think it was correct)

mistaken one
string qry="s1= ' " + v1 + " ' AND s2=' " + v2 + " '" ;
Correct one
string qry="s1= '" + v1 + "' AND s2='" + v2 + "'" ;

Thank You,
Regards,
Mani
 
This is one of the things that has bugged me about the Select method on
the data set for the longest time. We have ways of parameterizing queries
to the database, and I always felt that we should have something similar for
datasets.

Fortunately, LINQ to DataSets will fix a lot of that.
 
Back
Top