473,472 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DataTable.select problem with variables

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

May 1 '07 #1
5 20827
On May 1, 11:24 am, Manikandan <plmanikan...@gmail.comwrote:

<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

May 1 '07 #2
On May 1, 11:29 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On May 1, 11:24 am, Manikandan <plmanikan...@gmail.comwrote:

<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

May 1 '07 #3
On May 1, 11:50 am, Manikandan <plmanikan...@gmail.comwrote:

<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

May 1 '07 #4
On May 1, 12:02 pm, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On May 1, 11:50 am, Manikandan <plmanikan...@gmail.comwrote:

<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
May 1 '07 #5
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]
- mv*@spam.guard.caspershouse.com

"Manikandan" <pl**********@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
On May 1, 12:02 pm, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
>On May 1, 11:50 am, Manikandan <plmanikan...@gmail.comwrote:

<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


May 1 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: randy | last post by:
Hello all, I have a DataTable which I am building column by column and adding rows after each new column. The DataTable columns match the columns in my database table. I'm building the...
5
by: Stefan Turalski \(stic\) | last post by:
Hi, I'm wondering if there is a way to send a method parametrs by ref when DataTabel is a type of this value ? I done some sort of select over DataTable columns, just by removing them froma...
3
by: nandan | last post by:
Hi, Has any one ever compared the performance of calling a DataTable's Select method with a stored procedure doing the same thing? My point is: dataRows = DataTable.Select(filter) is better or...
4
by: Kris Rudin | last post by:
I am displaying a table of information on a web page, using an asp:table that I populate dynamically. On this page I give the user the options to group the rows by certain fields, and/or filter the...
4
by: slaprade | last post by:
I am loading a weeks worth of web logs into a dataset using Imports Microsoft.Data.Odbc These are text - fixed length fields so I have written a schema for them. The adapter fill looks like this...
3
by: Jon | last post by:
I'm learning about datatables. When using the example provided by MS in the ..NET Framework Class Library for DATATABLE (see below) I get an error on line 3 that says "Type expected". Is something...
12
by: Doug Bell | last post by:
Hi, I am having problems trying to create a (temporary) DataTable from a selection from a DataGrid (dgOrders). dtOrdDetails is declared as a Public DataTable Sub is: Dim stFilter as String...
5
by: wbosw | last post by:
I'm trying to filter the datatable at runtime from data selected from dropdown listboxes and a texbox. I place the values into variables and use the variables in the filter expression. However,...
6
by: fniles | last post by:
I am using VB.NET 2005 and Access database. My program uses a timer that kicks in every 1 min to read from a database and copy the dataset table to a datatable. This database is in a class called...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.