簡體   English   中英

正則表達式在不相關的數據表中產生錯誤

[英]Regular expression produces error in unrelated datatable

我目前正在嘗試為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]);
                }
            }
        }
    }

原來我在程序中使用的SelectionChanged事件之類的東西都錯誤地觸發了。 在每種方法下,我實現了以下內容

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

感謝在這里找到John Kragh的答案: 標准WPF選項卡控件中是否存在“選定的選項卡已更改事件”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM