2016-06-24 94 views
0

我想創建一個從SQL數據集填充的級聯ComboBox。 我的問題是我編譯代碼時沒有收到任何錯誤。但是這兩個組合框並沒有按照預期的方式填充數據。你能幫我找到我犯的錯誤嗎?級聯組合框

Private Sub FillSalesPerson() 
    Dim strConnString As String 
    strConnString = ConfigurationManager.ConnectionStrings("con").ConnectionString 
    Dim DS As New SqlConnection(strConnString) 
    Dim cmd As New SqlCommand() 
    cmd.Connection = DS 
    cmd.CommandType = CommandType.Text 
    cmd.CommandText = "select distinct Name,Salesperson_Code from dbo.View_Customers_With_Sales_People" 
    Dim objeDS As DataSet = New DataSet() 
    Dim dAdapter As New SqlDataAdapter 
    dAdapter.SelectCommand = cmd 
    DS.Open() 
    dAdapter.Fill(objeDS) 
    DS.Close() 

    ComboBox1.ValueMember = "Salesperson_Code" 
    ComboBox1.DisplayMember = "Name" 
    ComboBox1.DataSource = objeDS.Tables(0) 
End Sub 

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged 
    If ComboBox1.SelectedValue.ToString() <> " " Then 
     Dim Salesperson_Code As Integer = Convert.ToInt32(ComboBox1.SelectedValue.ToString()) 
     FillStates(Salesperson_Code) 
     'ComboBox2.SelectedIndex = 0' 
    End If 
End Sub 

Private Sub FillStates(Salesperson_Code As Integer) 
    Dim strConnString As String = ConfigurationManager.ConnectionStrings("con").ConnectionString 
    Dim DS As New SqlConnection(strConnString) 
    Dim cmd As New SqlCommand() 
    cmd.Connection = DS 
    cmd.CommandType = CommandType.Text 
    cmd.CommandText = "Select [No_],[Company Name] from [dbo].[View_Customers_With_Sales_People] where (Salesperson_Code= @Salesperson_Code)" 
    cmd.Parameters.AddWithValue("@Salesperson_Code", Salesperson_Code) 
    Dim objDs As New DataSet() 
    Dim adapter As New SqlDataAdapter() 
    adapter.SelectCommand = cmd 
    DS.Open() 
    adapter.Fill(objDs) 
    DS.Close() 
    If objDs.Tables(0).Rows.Count > 0 Then 
     ComboBox2.ValueMember = "[No_]" 
     ComboBox2.DisplayMember = "[Company Name]" 
     ComboBox2.DataSource = objDs.Tables(0) 
    End If 
End Sub 
+0

什麼是 「無法按照預期填充」 是什麼意思?沒有數據?錯誤的數據?慢? – Crowcoder

+0

組合框未填充數據。編譯後它們是空的,謝謝 – Randy

+0

如果沒有異常被拋出,那麼我懷疑你的查詢只是不返回行。 – Crowcoder

回答

0

嘗試改變ComboBox1.DataSource = objeDS.Tables(0)ComboBox1.RowSource = objeDS.Tables(0)