如何使用datareader从combobox selecteditem获取数据到文本框

时间:2011-04-26 19:11:25

标签: sql vb.net datareader

我有一个带有一个组合框和文本框的表单 现在我有一个SQL数据库,其中包含一个名为balance的表。它有两列,一列是customername,另一列是obbalance 现在我已将所有客户名称绑定到组合框 我要做的是:
当用户从组合框中选择客户名称时,文本框应显示所选obbalance的{​​{1}}。这里的客户名称不会重复,只有一个客户名称 我能做什么?请帮我。

customername

我已经这样做但仍然无法看到文本框中的值和obbalance是数据库sql中的flost值

1 个答案:

答案 0 :(得分:0)

Private Sub doSomething()
Dim conectionstring As String
        conectionstring = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\SHOPPROJECT\SHOPPROJECT\shop.mdf;Integrated Security=True;User Instance=True"


    Dim ST As String = ComboBox1.SelectedText

    Dim sqlcon As New SqlConnection(conectionstring)

    Dim sqlcmd As New SqlCommand("SELECT OBBALANCE, CUSTOMERNAME FROM BALANCE" ,sqlcon)
    sqlcmd.CommandType = CommandType.Text

    Try
        sqlcon.Open()
        Dim sdr As SqlDataReader = sqlcmd.ExecuteReader()

        If sdr IsNot Nothing Then
            Dim dt As DataTable
            dt.Load(sdr)

            With yourCombobox
                .DataSource = dt
                .DisplayMember = "CUSTOMERNAME"
                .ValueMember = "OBBALANCE"

            End With

        End If

    Catch ex As SqlException
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub comboBoxChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles yourCombobox.SelectedIndexChanged

textbox.text = yourCombobox.SelectedValue.ToString

End Sub