Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to fill a specific value on the combobox from the database which is to be filtered by using date.
Please help me to do this.

What I have tried:

C#
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Rd\\Documents\\Soundar1.accdb");
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();

 private void button1_Click(object sender, EventArgs e)
        {
            //try
            //{
                connection.Open();

                string sql = "select Sno from Table1 where dt=" + dateTimePicker1.Value.ToString("MM/dd/yyyy");
                OleDbDataAdapter da = new OleDbDataAdapter(sql, connection);
                da.Fill(dt);
                //da.Fill(ds,"Table1");
                comboBox1.DisplayMember = "Sno";
                comboBox1.DataSource = dt;

                connection.Close();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.ToString());
            //}
        } 


I tried this but its not working.
Posted
Updated 6-May-16 23:32pm
v3
Comments

1 solution

try this

C#
string value = dateTimePicker1.Value.ToString("MM/dd/yyyy");
           string sql = "select Sno from Table1 where dt= @dt";
           OleDbDataAdapter da = new OleDbDataAdapter(sql, connection);
           da.SelectCommand.Parameters.AddWithValue("@dt", value);
           da.Fill(dt);
 
Share this answer
 

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