Click here to Skip to main content
12,352,294 members (53,644 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hello,

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);
con.Open();
dr = cmd.ExecuteReader();
DataSet sd = new DataSet(); ;
 
while (dr.Read())
{
    DateTime d = (DateTime)dr["dat"];
    if (d >= fro && d <= to)
    {
 
    }
}
dr.Close();

How to send the result of datareader to dataset?
Posted 6-Nov-12 1:32am
Edited 6-Nov-12 1:37am
v2
Rate this: bad
 
good
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 http://msdn.microsoft.com/en-us/library/9xk1h71t.aspx[^] 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)
  Permalink  
Comments
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
 
good
Please Sign up or sign in to vote.

Solution 2

Use SQL BETWEEN Operator. Have a look at below link.
http://www.w3schools.com/sql/sql_between.asp

Edit
Do not concatenate values of input controls(textbox) to your query. This is potential high risk considering SQL Injection.
http://en.wikipedia.org/wiki/SQL_injection
http://msdn.microsoft.com/en-us/library/ms161953(v=sql.105).aspx

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;
  Permalink  
v2
Comments
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.160621.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