如何根据Windows组合框选择的值自动填充多个文本框?

时间:2017-02-02 03:38:51

标签: vb.net winforms combobox

我有一个从数据库动态填充的Windows组合框。

然后我有几个文本框,我想根据这个组合框中的选定值自动填充。

当我从组合框中选择一个值时,我的代码会一直发出一条警告,指出没有找到记录,文本框也没有填充数据。

任何想法我做错了什么?

Imports System.Data.SqlClient

Public Class Form1
    Dim conString As String = "Data Source=.\SQLEXPRESS;Initial Catalog=myDB;Integrated Security=True;;MultipleActiveResultSets=True"
    Dim con As New SqlConnection(conString)
    Dim cmd As New SqlCommand()
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        con.Open()
        cmd.Connection = con
        cmd.CommandText = "SELECT locId, name FROM Places"
        Dim adpt As New SqlDataAdapter(cmd)
        Dim dsn As New DataSet()
        adpt.Fill(dsn)
        con.Close()

        ComboBox1.DisplayMember = "name"
        ComboBox1.ValueMember = "LocID"
        ComboBox1.DataSource = dsn.Tables(0)
    End Sub


    Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Try
            con.Open()
            Dim req As String = "SELECT locId, name FROM Places where locID = @locId"
            Dim com As New SqlCommand(req, con)
            'MessageBox.Show(req)
            cmd.Parameters.AddWithValue("@locId", Convert.ToInt32(ComboBox1.SelectedValue))
            Dim dr As SqlDataReader = com.ExecuteReader()
            If dr.Read() Then
                txtLocationID.Text = dr.GetValue(0)
                txtLocation.Text = dr.GetValue(1).ToString()
            End If
        Catch ex As Exception
            MessageBox.Show("No records found")
        Finally
            con.Close()
        End Try
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我想你需要首先遍历datareader,然后才能获得数据。试试这个

Imports System.Data.SqlClient

Public Class Form1
    Dim conString As String = "Data Source=.\SQLEXPRESS;Initial Catalog=myDB;Integrated Security=True;;MultipleActiveResultSets=True"
    Dim con As New SqlConnection(conString)
    Dim cmd As New SqlCommand()
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        con.Open()
        cmd.Connection = con
        cmd.CommandText = "SELECT locId, name FROM Places"
        Dim adpt As New SqlDataAdapter(cmd)
        Dim dsn As New DataSet()
        adpt.Fill(dsn)
        con.Close()

        ComboBox1.DisplayMember = "name"
        ComboBox1.ValueMember = "LocID"
        ComboBox1.DataSource = dsn.Tables(0)
    End Sub


    Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Try
            con.Open()
            Dim req As String = "SELECT locId, name FROM Places where locID = @locId"
            Dim com As New SqlCommand(req, con)

            com.Parameters.AddWithValue("@locId", Convert.ToInt32(ComboBox1.SelectedValue)) ' You supposed to add parameters to instance of `com` object '
            Dim dr As SqlDataReader = com.ExecuteReader()
            While dr.Read
                txtLocationID.Text = dr(0)
                txtLocation.Text = dr(1)
            End While
        Catch ex As Exception
            MessageBox.Show("No records found")
        Finally
            con.Close()
        End Try
    End Sub
End Class