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

时间:2013-06-12 18:30:40

标签: c# sql sql-server

我目前正在尝试使用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 table WHERE column = value”。应用程序连接到数据库就好了但是如果我没有使用搜索过滤器,数据表似乎只会填充。

数据表似乎未按以下方式填写:

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

每次跳过,因为数据表中的行数为零。

编辑3:

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

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

1 个答案:

答案 0 :(得分:0)

事实证明,我在程序中使用的SelectionChanged事件等都是错误的。在每种方法下,我实现了以下

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

感谢John Kragh的回答: Is there Selected Tab Changed Event in the standard WPF Tab Control