Click here to Skip to main content
12,559,240 members (45,619 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#

I've c# app, I'm comparing in query between dates ,it doesn't recognize the comparison .The error is
An expression of non-boolean type specified in a context where a condition is expected, near '10/31/2012 09:00:00 م'.
Here's my code

string x=textBox1.Text;
string y=textBox1.Text;
DateTime fro=Convert.ToDateTime (x);
DateTime to=Convert.ToDateTime (y);
SqlConnection con = new SqlConnection(@"Data Source=.\Ahmed;Initial Catalog=Market;Integrated Security=True");
cmd=new SqlCommand ( "SELECT dat , product_id FROM  market where dat >= '" + fro + "' and '"+to+"' ",con);
dr = cmd.ExecuteReader();
DataSet sd = new DataSet(); ;
while (dr.Read())
    DateTime d = (DateTime)dr["dat"];
    if (d >= fro && d <= to)

How to send the result of datareader to dataset?
Posted 6-Nov-12 1:32am
Updated 6-Nov-12 1:37am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Well first i had a problem similar in which, the conversion to datetime was correct, but the month became the day.
Look at[^] to see the culture format.

Second SELECT dat , product_id FROM market where dat >= YOUR_FIRST_DATE and ??? () you mush specify what do you want to compare.

Advise: look in the debug session and get the command that will be run, and run it in sql server(or what you use)
Member 8584763 6-Nov-12 8:16am
copy error

cmd=new SqlCommand ( "SELECT dat , product_id FROM market where dat >= '" + fro + "' and dat<='"+to+"' ",con);
How to send from sqldatareader to dataset?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Use SQL BETWEEN Operator. Have a look at below link.

Do not concatenate values of input controls(textbox) to your query. This is potential high risk considering SQL Injection.

Comming to solution, try as below code.
cmd=new SqlCommand ( "SELECT dat , product_id FROM  market where dat BETWEEN  @fro and  @to ",con);
cmd.Parameters.Add("@fro", SqlDbType.VarChar);
cmd.Parameters("@fro").Value = x;
cmd.Parameters.Add("@to", SqlDbType.VarChar);
cmd.Parameters("@to").Value = y;
Member 8584763 6-Nov-12 10:10am
Well, it goes fine till this line
SqlDataAdapter ad1 = new SqlDataAdapter(cmd);
ad1.Fill(dy, "product");
it said Conversion failed when converting date and/or time from character string. and their data type in the database datetime?
RaisKazi 6-Nov-12 10:44am
Please refer updated solution.
Member 8584763 6-Nov-12 12:53pm
I already updated could you look at it?
Member 8584763 7-Nov-12 4:03am
are you still there?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161026.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100