2013-05-02 80 views
0

我使用數據庫中的值加載一個組合框。在組合框選擇更改中,第一個選擇將所有值返回爲0,下一個選擇返回數據庫上一行的值

當我選擇從下拉列表框的第一個選項,它獲取所有其他數值爲0,而當我從下拉框中選擇下一個選項,它顯示了一個選項的結果

下面是我的代碼

這對組合框

Dim cmd As New SqlCommand("select CompanyName from Company", conn) 'Company Name 
    Dim dt As New DataTable 'Company Name 
    Dim da As New SqlDataAdapter(cmd) 'Company Name 
    da.Fill(dt) 'Company Name 

    'Company Name 

    cmbCompanyName.ItemsSource = dt.DefaultView 
    cmbCompanyName.DisplayMemberPath = "CompanyName" 
    cmbCompanyName.SelectedValuePath = "CompanyName" 

和下面一個是在組合選擇填充字段改變

Try 

     Dim dt As New DataTable 
     Dim connection As New SqlConnection("Data Source=KITT7-PC;Initial Catalog=project;User ID=sa;Password=1234") 
     connection.Open() 

     Dim cmd3 As New SqlCommand("select * from Company where CompanyName='" & cmbCompanyName.Text & "'", connection) 
     cmd3.ExecuteNonQuery() 
     Dim da As New SqlDataAdapter(cmd3) 
     da.Fill(dt) 

     If dt.Rows.Count > 1 Then 
      MessageBox.Show("More than one record found for the company name") 
     End If 

     If dt.Rows.Count = 0 Then 
      MessageBox.Show("No records found") 
      Exit Sub 
     End If 

     cmbCompanyCode.Visibility = Windows.Visibility.Hidden 
     txtCompanyCode.Visibility = Windows.Visibility.Visible 

     cmbComapnyAlias.Visibility = Windows.Visibility.Hidden 
     txtCompanyAlias.Visibility = Windows.Visibility.Visible 

     For Each dr As DataRow In dt.Rows 
      txtCompanyCode.Text = dt.Rows(0)("CompanyCode").ToString() 
      ' txtCompanyName.Text = dt.Rows(0)("CompanyName").ToString() 
      txtCompanyAlias.Text = dt.Rows(0)("Alias").ToString() 
      txtAddress.Text = dt.Rows(0)("Address").ToString() 
      cmbCity.Text = dt.Rows(0)("city").ToString() 
      txtPincode.Text = dt.Rows(0)("Pincode").ToString() 
      cmbState.Text = dt.Rows(0)("State").ToString() 
      txtCountry.Text = dt.Rows(0)("Country").ToString() 

請告訴我是什麼問題。

回答

1

在你的For Each循環中,你總是參考dt.Rows(0)("something"),不是嗎?

我想你需要改變:

For Each dr As DataRow In dt.Rows 
      txtCompanyCode.Text = dt.Rows(0)("CompanyCode").ToString() 
      ' txtCompanyName.Text = dt.Rows(0)("CompanyName").ToString() 
      txtCompanyAlias.Text = dt.Rows(0)("Alias").ToString() 

喜歡的東西

For Each dr As DataRow In dt.Rows 
      txtCompanyCode.Text = dr ("CompanyCode").ToString() 
      ' txtCompanyName.Text = dr ("CompanyName").ToString() 
      txtCompanyAlias.Text = dr ("Alias").ToString() 
+0

我試圖this..but它不是取這聽起來很奇怪任何數據... – 2013-05-02 13:55:10

+0

。無論如何,它是否工作,如果你改變你的循環迭代索引,然後使用像 dt.Rows(i)。「CompanyCode」)。ToString()? – milanio 2013-05-03 11:59:03

相關問題