2013-06-12 29 views
1

我正在嘗試在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因爲沒有值被包含在項目陣列內並且程序試圖通過在陣列中爲了填充進一步文本框進行迭代。

如果需要,我可以提供任何其他代碼。我現在只是提供基礎知識。

編輯:

澄清。如果我使用搜索過濾器,情況只會中斷。在使用它之前,一切看起來都很好。

編輯2:

我也檢查,以確保selectedTab變量是否工作正常。這是正確的,所以問題不應該在那裏。正如我所說的查詢字符串正確構建,並且看起來像「SELECT * FROM table WHERE column = value」。應用程序連接到數據庫就好了,但如果我沒有使用搜索過濾器,數據表只能填充。

DataTable中出現未填充,如下所示:

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

被跳過每次過,因爲有零行的數據表。

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

怎麼是你正在使用你的for循環集「長度」變量?它可能是一個錯誤的錯誤。 – ajawad987

+0

@ ajawad987'if(selectedTab ==「LoanerItems」) { length = 30; } else if(selectedTab ==「Customers」) { length = 16; '理論上它從不改變。 – DanteTheEgregore

+0

@ ajawad987我也檢查過,以確保selectedTab變量正常工作。這是正確的,所以問題不應該在那裏。正如我所說的查詢字符串正確構建,看起來像「SELECT * FROM _table_ WHERE _column_ = _value_」。應用程序連接到數據庫就好了,但如果我沒有使用搜索過濾器,數據表只能填充。 – DanteTheEgregore

回答