2017-05-26 106 views
-1

我有這個小問題的項目集合無法修改數據源屬性設置時,我不知道該怎麼去做。我在VB.NET工作填充Combobox與值的另一個

在這裏,我從大學Combobox中的MSAccess數據庫填入了大學名稱。在大學Combobox的SelectedChange事件中,相應的Faculties將被填充到另一個名爲Faculty Combo組合框的Combobox中。我正在使用一個類定義執行此操作,並從類之外的不同方法進行查詢。

因此,當我嘗試從大學Combobox中選擇任何大學時,拋出「設置數據源屬性時無法修改項目集合」的錯誤。但我無法弄清楚,一切看起來都不錯,但我知道行內有什麼問題。

這裏是我的代碼:

類定義代碼:

' Database connection 
Public DBcon As New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" & _ 
              "Data Source=DATABASE.mdb") 
'Get DB Command ready 
Public DBCmd As OleDbCommand 

'Database data 
Public DBDA As OleDbDataAdapter 
Public DBDT As DataTable 

'Parameter Query 
Public Parameter As New List(Of OleDbParameter) 

'Query Stats 
Public Record As Integer 
Public Exception As String 

Public Sub ExecQuery(Query As String) 
    'Reseting the Query Stats 
    Record = 0 
    Exception = "" 

    Try 
     'Open a Connetion 
     DBCon.Open() 

     'Creating Database command 
     DBCmd = New OleDbCommand(Query, DBCon) 

     'Loading Params into Database Command 
     Parameter.ForEach(Sub(x) DBCmd.Parameters.Add(x)) 

     'Clear the List of Params 
     Parameter.Clear() 

     'Execute Command and Fill the DataTable 
     DBDT = New DataTable 
     DBDA = New OleDbDataAdapter(DBCmd) 
     Record = DBDA.Fill(DBDT) 
    Catch ex As Exception 
     Exception = ex.Message 
    End Try 

    'Close Connection 
    If DBCon.State = ConnectionState.Open Then DBCon.Close() 

代碼填充大學組合框:

Try 
     'Query string 
     Dim myQuery As String 
     myQuery = "SELECT * FROM Universities ORDER BY University_Name ASC" 

     'Run query 
     Access_DB.ExecQuery(myQuery) 

     ' In case of Errors Report 
     If NoErrors(True) = False Then Exit Sub 

     'Fill the Combo box 

     'Clear Combobox 
     University_Choice.University_NameComboBox.Items.Clear() 

     'Populate the combo box 
     With University_Choice.University_NameComboBox 
      .DisplayMember = "University_Name" 
      .ValueMember = "University_ID" 
      .DataSource = Access_DB.DBDT 
      .AutoCompleteMode = AutoCompleteMode.SuggestAppend 
      .AutoCompleteSource = AutoCompleteSource.ListItems 
     End With 

     'Display the first item found 
     If Access_DB.Record > 0 Then University_Choice.University_NameComboBox.SelectedIndex = 0 

    Catch ex As Exception 
     Access_DB.Exception = ex.Message 
    End Try 

代碼填充教職員組合框:

Try 
     'Query string 
     Dim QueryFaculty As String 
     QueryFaculty = "SELECT * FROM University_Faculties " & _ 
      "WHERE University_ID = @UniversityID" 

     Access_DB.ParamAdd("@UniversityID", "%" & _ 
           University_Choice.University_NameComboBox.SelectedValue.ToString & "%") 

     'Run query 
     Access_DB.ExecQuery(QueryFaculty) 

     'In case of errors report 
     If NoErrors(True) = False Then Exit Sub 

     'Clear 
     University_Choice.cboFaculty.Items.Clear() 

     'Populate the combo box 
     With University_Choice.cboFaculty 
      .DisplayMember = "Faculty_Name" 
      .ValueMember = "University_Faculties_ID" 
      .DataSource = AccessDB.DBDT 
      .AutoCompleteMode = AutoCompleteMode.SuggestAppend 
      .AutoCompleteSource = AutoCompleteSource.ListItems 
     End With 

     'Display the first item found 
     If Access_DB.Record > 0 Then University_Choice.cboFaculty.SelectedIndex = 0 
    Catch ex As Exception 
     Access_DB.Exception = ex.Message 
    End Try 

如果您需要更多信息,請告訴我。

+0

好的......您的問題在哪裏?堆棧跟蹤是否指示引發錯誤的代碼行? – Sometowngeek

+0

只是把整個代碼而不是特定的代碼片段不會幫助。用戶不希望花時間首先閱讀大量源代碼並提供解決方案。考慮閱讀https://stackoverflow.com/help/how-to-ask –

+0

把一個斷點放在'If NoErrors(True)= False Then Exit Sub'並檢查Access_DB是否與University_Name和University_ID一起返回行。這你檢查觀察窗口。 –

回答

0

AccessDB應該是Access_DB

With University_Choice.cboFaculty 
    .DisplayMember = "Faculty_Name" 
    .ValueMember = "University_Faculties_ID" 
    .DataSource = Access_DB.DBDT 'Typo here 
    .AutoCompleteMode = AutoCompleteMode.SuggestAppend 
    .AutoCompleteSource = AutoCompleteSource.ListItems 
End With 
+0

對於那段長長的代碼,我非常抱歉只是注意到它是一種模糊的。 –

+0

當我通過行檢查,我發現「記錄」變量總是休息到0類定義代碼中的每一次,即這裏'重置查詢統計 記錄= 0。但Try ... Catch塊沒有指定錯誤所在的特定行。我發現的唯一情況是,當填充教師的方法被解僱時,它仍然會經歷相同的類定義,從而將「記錄」變量重置爲零。任何想法被接受,請對這個論壇新來。謝謝 –

相關問題