none
datetime picker

    Question

  • i am using this code . i want to select records from database wher date is between these two datetime pikers values.

    in database column name is date and data type is also date. in datetime piker properties i set formate short.

     error is         Incorrect syntax near '4'.


    try  {
                    SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer Where date>='" + dateTimePicker1.Value + "'AND date<=" + dateTimePicker2.Value+ " ", my);
                    da.Fill(dt);
                    dataGridView1.DataSource = dt.DefaultView;
                }

                catch
                {
                    throw;
                    
                    
                    }
     

    Sunday, August 25, 2013 11:28 AM

Answers

  • Looks like the second date value is not enclosed in single quotes.

    Rather to framing the query inside Adapter, declare string variable and assign the framed query to that variable.

    It helps in debugging the syntax errors.


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Sunday, August 25, 2013 11:34 AM

All replies

  • Looks like the second date value is not enclosed in single quotes.

    Rather to framing the query inside Adapter, declare string variable and assign the framed query to that variable.

    It helps in debugging the syntax errors.


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Sunday, August 25, 2013 11:34 AM
  • plz a example

    Sunday, August 25, 2013 11:43 AM
  • Refer the below example.

    SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer Where date >= '" + dateTimePicker1.Value + "' AND 
    date <= '" + dateTimePicker2.Value + "'", my);


    Regards, RSingh

    Sunday, August 25, 2013 12:21 PM
  • plz a example

        
    String query="Select * from addcustomer Where date>='" + dateTimePicker1.Value + "'AND date<= ' " + dateTimePicker2.Value+ " ' ";
    SqlDataAdapter da = new SqlDataAdapter(query, my);
                    da.Fill(dt);

    With this u can see the framed query stored in variable named query and easily check the syntax by looking at via debug mode or copy paste in SSMS directly and execute for correctness.

    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Sunday, August 25, 2013 1:00 PM