Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Not getting results While filtering datagridview using Datetimepicker,Combobox & textbox


Using Access databse.

My date format is : MM/dd/yyyy

moreover i want to get this result using "Run Filter" button.

What I have tried:

Error Message : System.Data.OleDb.OleDbException: 'No value given for one or more required parameters.
'

My Code :
C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //Format(dateTimePicker1.Value, "mm/dd/yyyy");
            if(comboBox1.Text == "ID")
            {
                OleDbDataAdapter da = new OleDbDataAdapter("select * from Spldetails where ID like '%" + textBox1.Text + "%' and Date >= ('"+(dateTimePicker1.Value.ToString("mm/dd/yyyy"))+ "') and Date <= ('" + (dateTimePicker2.Value.ToString("mm/dd/yyyy")) + "')", con);
                OleDbCommandBuilder build = new OleDbCommandBuilder(da);
                DataSet ds = new DataSet();
                da.Fill(ds, "Spldetails");
                dataGridView1.DataSource = ds.Tables["Spldetails"];
            }
            if (comboBox1.Text == "Empid")
            {
                OleDbDataAdapter da = new OleDbDataAdapter("select * from Spldetails where EmpId like '%" + textBox1.Text + "%' and Date >= ('" + (dateTimePicker1.Value.ToString("mm/dd/yyyy")) + "') and Date <= ('" + (dateTimePicker2.Value.ToString("mm/dd/yyyy")) + "'))", con);
                OleDbCommandBuilder build = new OleDbCommandBuilder(da);
                DataSet ds = new DataSet();
                da.Fill(ds, "Spldetails");
                dataGridView1.DataSource = ds.Tables["Spldetails"];
            }
        }
Posted
Updated 11-Apr-18 9:20am
v2

1 solution

As explained earlier, you should use parameters. This will allow you to pass the values to the query safely and without conversion problems.

The obvious question with the code you provided is: Which one of the statements fails. And the second question is: Using debugger what does the whole SQL statement look like when executed? In other words what is the result of the string concatenation?

When you post the SQL statement used, we have something concrete to investigate. Unfortunately, without seeing the actual statement we can only guess what has been concatenated into the SQL string.

So as explained earlier, use the debugger to investigate the running code and the variable values and check what really gets executed when Fill is called.
 
Share this answer
 
Comments
Prateek gsharma 12-Apr-18 6:25am    
i will try sir
Prateek gsharma 12-Apr-18 14:41pm    
Still i am getting same error while debugging

private void textBox1_TextChanged(object sender, EventArgs e)
{
//Format(dateTimePicker1.Value, "mm/dd/yyyy");
if(comboBox1.Text == "ID")
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from Spldetails where ID like '% @v1 %' and DDate between @d1 and @d2", con);
cmd.Parameters.Add("@v1", OleDbType.VarChar, 30).Value = this.textBox1.Text;
cmd.Parameters.Add("@d1", OleDbType.Date).Value = this.dateTimePicker1.Text;
cmd.Parameters.Add("@d2", OleDbType.Date).Value = this.dateTimePicker2.Text;
OleDbCommandBuilder build = new OleDbCommandBuilder(da);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
//DataSet ds = new DataSet();
//da.Fill(ds, "Spldetails");
//dataGridView1.DataSource = ds.Tables["Spldetails"];
}
if (comboBox1.Text == "Empid")
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from Spldetails where EmpId like '%@v1%' and DDate between @d1 and @d2", con);
cmd.Parameters.Add("@v1", OleDbType.VarChar, 30).Value = this.textBox1.Text;
cmd.Parameters.Add("@d1", OleDbType.Date).Value = this.dateTimePicker1.Text;
cmd.Parameters.Add("@d2", OleDbType.Date).Value = this.dateTimePicker2.Text;
OleDbCommandBuilder build = new OleDbCommandBuilder(da);
DataTable dt = new DataTable();
//DataSet ds = new DataSet();
da.Fill(dt);
dataGridView1.DataSource = dt;
//da.Fill(ds, "Spldetails");
//dataGridView1.DataSource = ds.Tables["Spldetails"];
}
}
Wendelius 12-Apr-18 23:02pm    
What error do you get?
On which line the error occurs?
Prateek gsharma 13-Apr-18 13:53pm    
Error Message : System.Data.OleDb.OleDbException: 'No value given for one or more required parameters
Wendelius 16-Apr-18 23:09pm    
Based on the error message, you haven't provided values for all of the parameters. It seems that you use a different command object than the data adapters command. Instead of

cmd.Parameters.Add("@v1", OleDbType.VarChar, 30).Value = this.textBox1.Text;

try using

da.SelectCommand.Parameters.Add("@v1", OleDbType.VarChar, 30).Value = this.textBox1.Text;

Also do not concatenate % characters withe the parameter. Set them inside the parameter. In other words

OleDbDataAdapter da = new OleDbDataAdapter("select * from Spldetails where EmpId like '@v1' and DDate between @d1 and @d2", con);
da.SelectCommand.Parameters.Add("@v1", OleDbType.VarChar, 30).Value = "%" + this.textBox1.Text + "%";

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