C# 如何运行依赖于Combobox值并通过Combobox SelectionChanged事件调用的方法

C# 如何运行依赖于Combobox值并通过Combobox SelectionChanged事件调用的方法,c#,sql,wpf,combobox,selectionchanged,C#,Sql,Wpf,Combobox,Selectionchanged,我的datagrid上的每个字段都有多个文本框,它根据这些文本框条目对我的数据进行多重过滤。此过滤由名为ApplyFilter()的方法处理,该方法工作正常 Datagrid由来自MS SQL Server 2008的查询填充,该查询在我们的网络服务器上运行。默认情况下,datagrid会选择前100个结果来加快速度。我有一个名为“cboSelectTop”的组合框,其中填充了数字10、100、1000,所有这些都是供用户增加/减少查询结果的 Applyfilter()基于此组合框值返回查询。当

我的datagrid上的每个字段都有多个文本框,它根据这些文本框条目对我的数据进行多重过滤。此过滤由名为ApplyFilter()的方法处理,该方法工作正常

Datagrid由来自MS SQL Server 2008的查询填充,该查询在我们的网络服务器上运行。默认情况下,datagrid会选择前100个结果来加快速度。我有一个名为“cboSelectTop”的组合框,其中填充了数字10、100、1000,所有这些都是供用户增加/减少查询结果的

Applyfilter()基于此组合框值返回查询。当我从TextBox的TextChanged事件调用Applyfilter()方法时,筛选按预期运行。但如果我从组合框SelectionChanged事件调用ApplyFilter(),我会在(!string.IsNullOrEmpty(txtSupplier.Text))中得到System.NullReferenceException

对象引用未设置为对象的实例。TXT供应商是 空的

问题是:运行Applyfilter()的解决方法是什么,它取决于combobox值,可以从TextBox TextChanged事件和combobox SelectionChanged事件中运行

ApplyFilter()方法的重要部分是:

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

    }

欢迎提供任何开始的线索。

在尝试访问方法中的控件或其任何属性之前,只需检查
txtSupplier
和所有其他控件是否已初始化(!=
null
):

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")
                ...
            }
}

组合框的
SelectionChanged
事件可能在初始化所有控件之前启动,这就是为什么会出现
NullReferenceException

的原因,只需检查
txtSupplier
和所有其他控件是否已初始化(!=
null
)在尝试访问方法中的这些属性或其任何属性之前:

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")
                ...
            }
}

在初始化所有控件之前,
组合框的
SelectionChanged
事件可能会首先触发,这就是为什么会出现
NullReferenceException

使用
SelectedIndexChanged
代替。谢谢您的评论。我并没有坦率地说,我正在工作的WPF,但我已经标记它<代码>所选索引已更改
在我的选项中不存在。还有其他解决方法吗?请尝试
txtSupplier.Text!=string.Empty
而不是
!string.IsNullOrEmpty(txtSupplier.Text)
感谢您的快速回复。即使我知道这与我的问题无关,我还是尝试了你的建议,并得到了同样的错误。我认为我的问题是循环引用,当我基于相同的combobox值调用该方法时,它发生在combobox selectionchanged事件上。我认为visual studio弹出的错误与此无关。txtSupplier?.text使用
SelectedIndexChanged
代替谢谢您的评论。我并没有坦率地说,我正在工作的WPF,但我已经标记它<代码>所选索引已更改
在我的选项中不存在。还有其他解决方法吗?请尝试
txtSupplier.Text!=string.Empty
而不是
!string.IsNullOrEmpty(txtSupplier.Text)
感谢您的快速回复。即使我知道这与我的问题无关,我还是尝试了你的建议,并得到了同样的错误。我认为我的问题是循环引用,当我基于相同的combobox值调用该方法时,它发生在combobox selectionchanged事件上。我认为visual studio弹出的错误与此无关。txtSupplier?.text你击中了靶心!在修复代码后,我又遇到了一些错误,但我现在知道问题出在哪里了,我已经设法使它正常工作。谢谢你的帮助!你击中了靶心!在修复代码后,我又遇到了一些错误,但我现在知道问题出在哪里了,我已经设法使它正常工作。谢谢你的帮助!