I’m not sure whts wrong with this code…
else if (combo_View.Text == "Orders")
{
da.SelectCommand = new OleDbCommand("SELECT * FROM TestQuery WHERE (VendorName = @VendorName OR @VendorName = '') AND (CustomerName = @CustomerName OR @CustomerName = '') AND (PO = @PO OR @PO = '') AND (ItemNum = @ItemNum OR @ItemNum = '') AND (orderDate BETWEEN @From AND @To) ORDER BY CustomerName", cs);
//da.SelectCommand = new OleDbCommand("SELECT * FROM TestQuery WHERE orderDate BETWEEN @From AND @To", cs);
da.SelectCommand.Parameters.Add("@VendorName", OleDbType.VarChar).Value = combo_VendorView.Text.ToString();
da.SelectCommand.Parameters.Add("@CustomerName", OleDbType.VarChar).Value = combo_CustomerView.Text.ToString();
da.SelectCommand.Parameters.Add("@From", OleDbType.Date).Value = "#" + tp_viewFrom.Value.Date.ToString("M/d/yyyy") + "#";
da.SelectCommand.Parameters.Add("@To", OleDbType.Date).Value = "#" + tp_viewTo.Value.Date.ToString("M/d/yyyy") + "#";
da.SelectCommand.Parameters.Add("@PO", OleDbType.VarChar).Value = txt_POLookup.Text.ToString();
da.SelectCommand.Parameters.Add("@ItemNum", OleDbType.VarChar).Value = combo_ItemNumLookup.Text.ToString();
dsB.Clear();
da.Fill(dsB);
dgv_DataLookup.DataSource = dsB.Tables[0];
}
basiclly, I want to fill a datagrid with a between statement. For the uncommented SelectCommand, I appear to get no values on my datagrid when a select valid dates. However, when I take out the comments of the select command after that, it works when its only date. Can anyone tell me whats wrong with the first command when I have other parameters with it? Thanks
Edit:
More strangeness, when I use this select command:
da.SelectCommand = new OleDbCommand("SELECT * FROM TestQuery WHERE
(VendorName = @VendorName OR @VendorName = '')
AND (CustomerName = @CustomerName OR @CustomerName = '')
AND orderDate BETWEEN @From AND @To ORDER BY CustomerName", cs);
it works but I add anything more to it and it stops…
If any of your data values or the parameters themselves in the between clause are null, the between will always be false. Test for null values on the @from and @to and put the between in an or with that test.