Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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
Updated 6-Nov-12 1:37am
v2

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)
 
Share this answer
 
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?
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.
C#
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;
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900