PC Review


Reply
Thread Tools Rate Thread

DataTable.select problem with variables

 
 
Manikandan
Guest
Posts: n/a
 
      1st May 2007
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

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      1st May 2007
On May 1, 11:24 am, Manikandan <plmanikan...@gmail.com> wrote:

<snip>

> 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

 
Reply With Quote
 
Manikandan
Guest
Posts: n/a
 
      1st May 2007
On May 1, 11:29 am, "Jon Skeet [C# MVP]" <s...@pobox.com> wrote:
> On May 1, 11:24 am, Manikandan <plmanikan...@gmail.com> wrote:
>
> <snip>
>
> > 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


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

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      1st May 2007
On May 1, 11:50 am, Manikandan <plmanikan...@gmail.com> wrote:

<snip>

> 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

 
Reply With Quote
 
Manikandan
Guest
Posts: n/a
 
      1st May 2007
On May 1, 12:02 pm, "Jon Skeet [C# MVP]" <s...@pobox.com> wrote:
> On May 1, 11:50 am, Manikandan <plmanikan...@gmail.com> wrote:
>
> <snip>
>
> > 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?
> 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


 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      1st May 2007
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.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Manikandan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On May 1, 12:02 pm, "Jon Skeet [C# MVP]" <s...@pobox.com> wrote:
>> On May 1, 11:50 am, Manikandan <plmanikan...@gmail.com> wrote:
>>
>> <snip>
>>
>> > 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?
>> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataTable.Select problem in ASP.NET 2.0 manoj241176@gmail.com Microsoft ADO .NET 1 30th Aug 2006 12:24 PM
DataTable.Select problem Tom Microsoft C# .NET 0 8th Mar 2006 08:37 PM
datatable.select problem Frank Microsoft C# .NET 9 4th Apr 2005 02:18 PM
DataTable.Select problem Alex Markov Microsoft ADO .NET 2 6th Sep 2003 05:13 PM
DataTable.Select problem Alex Markov Microsoft C# .NET 1 5th Sep 2003 08:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:20 PM.