使用datetimepicker,combobox& amp;过滤datagridview时没有得到结果文本框 [英] Not getting results while filtering datagridview using datetimepicker, combobox & textbox

查看:97
本文介绍了使用datetimepicker,combobox& amp;过滤datagridview时没有得到结果文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未获得结果使用Datetimepicker过滤datagridview时,Combobox&文本框





使用Access数据库。



我的日期格式为: MM / dd / yyyy



而且我想用运行过滤器按钮得到这个结果。



我尝试过:



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 :

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"];
            }
        }

推荐答案

如前所述,您应该使用参数。这将允许您安全地将值传递给查询而没有转换问题。



您提供的代码显而易见的问题是:哪一个语句失败。第二个问题是:使用调试器执行时整个SQL语句是什么样的?换句话说,字符串连接的结果是什么?



当你发布使用的SQL语句时,我们有一些具体的东西需要调查。不幸的是,如果没有看到实际的声明,我们只能猜测连接到SQL字符串的内容。



如前所述,使用调试器调查正在运行的代码和变量值并检查调用Fill时实际执行的内容。
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.


这篇关于使用datetimepicker,combobox&amp; amp;过滤datagridview时没有得到结果文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆