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()

请告诉我有什么问题。

4

1 回答 1

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()
于 2013-05-02T12:55:48.133 回答