正则表达式在不相关的数据表中产生错误

本文关键字:错误 数据表 不相关 正则表达式 | 更新日期: 2023-09-27 18:18:00

我目前正在尝试创建一个c#应用程序的搜索过滤器。我有以下代码:

loanerListBox1.Items.Clear();
string[] recordsRetreivedTemp = new string[recordsRetreived.Count];
recordsRetreived.CopyTo(recordsRetreivedTemp);
string pattern = loanerTextBox21.Text;
{
    foreach (string s in recordsRetreivedTemp)
    {
        if (System.Text.RegularExpressions.Regex.IsMatch(s, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
        {
            loanerListBox1.Items.Add(s);
        }
    }
}

我注意到,在这样做之后,我的一个数据表似乎没有正确填充。连接字符串很好,我已经检查了查询字符串,它似乎构建得很好。我看不出有什么问题。

SQL适配器代码:
//retreive data
SqlDataAdapter adapter = new SqlDataAdapter(querystring, loanersConnection);
DataTable datatable = new DataTable();
adapter.Fill(datatable);
Items = new string[length, datatable.Rows.Count];
if (selectedTab == "LoanerItems")
{
    for (int x = 0; x <= length - 1; x++)
    {
        for (int y = 0; y <= datatable.Rows.Count - 1; y++)
        {
            Items[x, y] = datatable.Rows[y][ColumnLists.LoanerItems[x]] as String;
        }
    }
}

错误本身是一个IndexOutOrRange异常,因为Items数组中没有包含值,程序试图遍历数组以填充更多的文本框。

如果需要,我可以提供任何其他代码。我现在只讲基本的。

编辑:

澄清。只有当我使用搜索过滤器时才会出现问题。在使用它之前,一切似乎都很好。

编辑2:

我还检查了以确保selectedTab变量工作正常。它是正确的,所以问题不应该在那里。正如我所说的查询字符串正确构建,看起来像"SELECT * FROM表WHERE列=值"。应用程序连接到数据库很好,但只有当我没有使用搜索过滤器时,数据表才会出现填充。

数据表似乎没有按照以下方式填充:

        for (int y = 0; y <= datatable.Rows.Count - 1; y++)
        {
            Items[x, y] = datatable.Rows[y][ColumnLists.LoanerItems[x]] as String;
        }

每次都被跳过,因为数据表中没有行。

编辑3:

listbox_selectionchanged方法,用于使用sql适配器初始化类:

    private void loanerListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (loanerListBox1.SelectedItem != null)
        {   //populate textboxes
            string comboBoxTemp1 = loanerComboBox1.SelectedItem.ToString();
            string comboBoxTempFinal = comboBoxTemp1.Replace(" ", string.Empty);
            string listBoxTemp = loanerListBox1.SelectedItem.ToString();
            string whereClause = string.Format("{0} = '{1}'", comboBoxTempFinal, listBoxTemp);
            SQLRetrieve.PopulateColumns populatecolumns = new SQLRetrieve.PopulateColumns(whereClause, tab);
            retreivedColumnsForWrite = populatecolumns.RetrieveColumns();
            populateLoanerItemsPage();
            //populate duplicates combobox
            loanerComboBox2.Items.Clear();
            for (int y = 0; y < retreivedColumnsForWrite.GetLength(1); y++)
            {
                if (!loanerComboBox2.IsEnabled)
                {
                    loanerComboBox2.IsEnabled = true;
                }
                string tobuild = "";
                //see note up top on retreivedColumnsForWrite array
                for (int x = 1; x < retreivedColumnsForWrite.GetLength(0); x++)
                {
                    tobuild += retreivedColumnsForWrite[x, y];
                    tobuild += "|";
                }
                loanerComboBox2.Items.Add(tobuild);
            }
            if (loanerComboBox2.Items.Count == 1)
            {
                loanerComboBox2.IsEnabled = false;
            }
        }
    }

整个类使用SQl适配器:

public class PopulateColumns
{
    //Retreived columns via search query.
    //X is columns, y is rows.
    //Read through each x value for y row.
    //Not through each y value for x row.
    //for (y = 0; y < ?; y++)
        //for (x = 0; x <?; x++)
    private string[,] Items;
    private string querystring;
    private string selectedTab;
    private int length;
    public PopulateColumns(string passedString, string selectedTab)
    {
        //builds query string
         querystring = string.Format("SELECT * FROM {0} WHERE {1}", selectedTab, passedString);
        this.selectedTab = selectedTab;
        if (selectedTab == "LoanerItems")
        {
            length = 30;
        }
        else if (selectedTab == "Customers")
        {
            length = 16;
        }
    }
    public String[,] RetrieveColumns()
    {
        //Open Connection
        SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
        scsb.DataSource = "LLOYD2''";
        scsb.InitialCatalog = "LoanersTest";
        scsb.IntegratedSecurity = true;
        scsb.ConnectTimeout = 30;
        SqlConnection loanersConnection = new SqlConnection(scsb.ConnectionString);
        //retreive data
        SqlDataAdapter adapter = new SqlDataAdapter(querystring, loanersConnection);
        DataTable datatable = new DataTable();
        adapter.Fill(datatable);
        Items = new string[length, datatable.Rows.Count];
        //New one needs to be added per tab.
        if (selectedTab == "LoanerItems")
        {
            for (int x = 0; x <= length - 1; x++)
            {
                for (int y = 0; y <= datatable.Rows.Count - 1; y++)
                {
                    Items[x, y] = datatable.Rows[y][ColumnLists.LoanerItems[x]] as String;
                }
            }
        }
        else if (selectedTab == "Customers")
        {
            for (int x = 0; x <= length - 1; x++)
            {
                for (int y = 0; y <= datatable.Rows.Count - 1; y++)
                {
                    Items[x, y] = datatable.Rows[y][ColumnLists.Customers[x]] as String;
                }
            }
        }
        return Items;
    }
}

仔细检查就会发现textbox.clear();在这里导致错误:

private void loanerComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        loanerListBox1.Items.Clear();
        ***loanerTextBox21.Clear();***
        if (loanerComboBox1.SelectedItem != null)
        {
            Combox1Retrieve retriever = new Combox1Retrieve(loanerComboBox1.SelectedItem.ToString(), tab);
            recordsRetreived = retriever.retrieveSQL();
            for (int i = 0; i <= recordsRetreived.Count - 1; i++)
            {
                if (!loanerListBox1.Items.Contains(recordsRetreived[i]))
                {
                    loanerListBox1.Items.Add(recordsRetreived[i]);
                }
            }
        }
    }

正则表达式在不相关的数据表中产生错误

结果是SelectionChanged事件和我在程序中使用的类似事件都错误地触发了。在每个方法下,我实现了以下

if (e.Source is TabControl)
{
  //do work when tab is changed
}

感谢John Kragh的回答:标准WPF选项卡控件

中是否存在"选定选项卡更改事件"?