I have multiple TextBoxes for each field on my datagrid, which multi-filters my data based on these textbox entries. This filtering is handled by a method called ApplyFilter() which works fine.
Datagrid is populated by queries from MS SQL Server 2008 which runs on our server on the network. By default datagrid selects top 100 results to speed up things. I have a combobox called "cboSelectTop" which is populated by numbers 10, 100, 1000, ALL for user to increase/decrease the results from query.
Applyfilter() returns query based on this combobox value. When I call Applyfilter() method from TextBox's TextChanged event, filtering runs as intended. But if I call ApplyFilter() from Combobox SelectionChanged event, I get System.NullReferenceException at if(!string.IsNullOrEmpty(txtSupplier.Text))
which says
Object reference not set to an instance of an object. txtSupplier was Null
Question is: What is the workaround to run Applyfilter(), that depends on combobox value, to function both from TextBox TextChanged event and Combobox SelectionChanged event?
The important part of my ApplyFilter() method is:
private void ApplyFilter()
{
string sConn = @"Data Source=;Initial Catalog=;
User ID=;Password=;";
using (SqlConnection sc = new SqlConnection(sConn))
{
sc.Open();
if (!string.IsNullOrEmpty(txtSupplier.Text))
{
sql1 = "CompanyName like '" + txtSupplier.Text + "%'and ";
}
else
{
sql1 = "";
}
.
.
.
if (!string.IsNullOrEmpty(txtPrice.Text))
{
sql17 = "Price like '" + txtPrice.Text + "%'and ";
}
else
{
sql17 = "";
}
if (cboSelectTop.Text == "ALL")
{
sql = "Select * from Priceview Where " + sql1 + sql2 + sql3 + sql4 + sql5 + sql6 + sql7 + sql8 + sql9 + sql10 + sql11 + sql12 + sql13 + sql14 + sql15 + sql16 + sql17;
}
else
{
sql = "Select top " + cboSelectTop.Text + " * from Priceview Where " + sql1 + sql2 + sql3 + sql4 + sql5 + sql6 + sql7 + sql8 + sql9 + sql10 + sql11 + sql12 + sql13 + sql14 + sql15 + sql16 + sql17;
}
if (sql.Substring(sql.Length - 4) == "and ")
{
sql = sql.Remove(sql.Length - 4, 4);
}
else if (sql.Substring(sql.Length - 4) == "ere ")
{
sql = sql.Remove(sql.Length - 7, 7);
}
else
{
if (cboSelectTop.Text == "ALL")
{
sql = "Select * from Priceview";
}
else
{
sql = "Select top " + cboSelectTop.Text + " * from Priceview";
}
}
Console.WriteLine(sql);
SqlCommand com = new SqlCommand(sql, sc);
using (SqlDataAdapter adapter = new SqlDataAdapter(com))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
DgPrices.ItemsSource = dt.DefaultView;
}
}
}
Any clue to start with is appreciated.
Simply check if txtSupplier
and all other controls have been initialized (!= null
) before you try to access them or any their properties in your method:
private void ApplyFilter()
{
string sConn = @"Data Source=;Initial Catalog=;
User ID=;Password=;";
using (SqlConnection sc = new SqlConnection(sConn))
{
sc.Open();
if (txtSupplier != null && !string.IsNullOrEmpty(txtSupplier.Text))
{
sql1 = "CompanyName like '" + txtSupplier.Text + "%'and ";
}
else
{
sql1 = "";
}
.
.
.
if (txtPrice != null && !string.IsNullOrEmpty(txtPrice.Text))
{
sql17 = "Price like '" + txtPrice.Text + "%'and ";
}
else
{
sql17 = "";
}
if (cboSelectTop == null)
return;
if (cboSelectTop.Text == "ALL")
...
}
}
The SelectionChanged
event for a ComboBox
may fire initially before all controls have been initialized and that's why you get a NullReferenceException
.