0

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.

Tehscript
  • 2,556
  • 2
  • 13
  • 23
  • Use `SelectedIndexChanged` instead – 3vts Apr 25 '17 at 02:24
  • Thank you for your comment. I did not frankly mention that I am working on WPF but I have tagged it. `SelectedIndexChanged` does not exist within my options. Any other workaround? – Tehscript Apr 25 '17 at 02:36
  • Try `txtSupplier.Text != string.Empty` instead of `!string.IsNullOrEmpty(txtSupplier.Text)` – 3vts Apr 25 '17 at 02:38
  • Thank you for your quick response. Even if I know this is not related with my problem, I tried your suggestion anyways and get the same error. I think my problem is a circular reference, which occurs on combobox selectionchanged event when I call the method based on the same combobox value. I think the error visual studio pops up is irrelevant. – Tehscript Apr 25 '17 at 02:50

1 Answers1

2

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • You hit the bull's eye! I encountered a few more errors after fixing the code, but I see what the problem is now and I have managed to make it work.Thank you for your help! – Tehscript Apr 25 '17 at 15:26