Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
See more: Code Hi,

I've created a program in C#, when I search by date an error is displayed

I need to search by date in SQL Server, can I do that? Or can I use the convert function in the SELECT statement

Please help me

the table is outlaid the column date_outlaid
the erorr display
is
syntax erorr convertingthe varchar value aug20 2011 12:00 Am to column of data type int
the code is
C#
private void display_outliad_f(string x)
        {
            //creat adabter to execute the query
            SqlDataAdapter ad = new SqlDataAdapter(x, con1);
            //create comand buillder
            SqlCommandBuilder cBulider = new SqlCommandBuilder(ad);
            //create data table to hold the result query
            DataTable dTable = new DataTable();
            //fill the data table
            ad.Fill(dTable);
            //to sycron between table and grv
            BindingSource Bsource = new BindingSource();
            //set bsouce to the datasource
            Bsource.DataSource = dTable;
            dataGridView1.DataSource = Bsource;
        }



the statmants ask is
C#
private void textBox4_TextChanged(object sender, EventArgs e)
        {
         display_outliad_f("select * from outlaid where date_outlaid="+date );
        }


help me please to chang this code
Posted
Updated 27-Oct-11 22:54pm
v3

Try this:
C#
private void textBox4_TextChanged(object sender, EventArgs e)
        {
         display_outliad_f("select * from outlaid where date_outlaid=\""+date.ToToShortDateString() + """ );
        }
 
Share this answer
 
v2
Comments
Mehdi Gholam 28-Oct-11 5:24am    
Fixed the string deliminator
abuhmamm 28-Oct-11 18:39pm    
when applied this error
The error says the cannot convert varchar to int.

What is the datatype of date_outlaid column
 
Share this answer
 
Just to add to what DaveKerr said: SQL server always wants dates in ISO format: yyyy-MM-dd, so the solution he gave may help, but probably will need changes, as the ToShortDateString method returns the date in the format appropriate to your current locale.

Instead, consider using
date.ToString("yyyy-MM-dd")
Or, (preferably) using it as a SQL Parameter instead - this would mean changing your display_outliad_f method quite a bit, but is a very good idea in the long run.

All of this may still not do what you want - it assumes that you are storing date_outlaid as a DateTime in your SQL database.
 
Share this answer
 
Comments
abuhmamm 28-Oct-11 18:41pm    
when applied this the erorr program
error is the conversion of char data type to datetime data type resulted in out-of-rang datetime value
please help me
try this

C#
private void textBox4_TextChanged(object sender, EventArgs e)
        {
         display_outliad_f("select * from outlaid where date_outlaid='"+date.ToString("yyyy-MMM-dd") + "'" );
        }
 
Share this answer
 
v2

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