Click here to Skip to main content
Sign Up to vote bad
good
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:32
Edited 6 Nov '12 - 1:37


2 solutions

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  
Comments
Member 8584763 - 6 Nov '12 - 10:10
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:44
Please refer updated solution.
Member 8584763 - 6 Nov '12 - 12:53
I already updated could you look at it?
Member 8584763 - 7 Nov '12 - 4:03
are you still there?
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:16
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?

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 216
1 Sergey Alexandrovich Kryukov 169
2 Tadit Dash 154
3 Richard MacCutchan 145
4 Santhosh G_ 125
0 Sergey Alexandrovich Kryukov 10,338
1 OriginalGriff 7,965
2 CPallini 4,201
3 Rohan Leuva 3,522
4 Maciej Los 3,159


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid