简体   繁体   English

如何将 ComboBox 与数据库链接并在 TextBox 中显示值 if Select ComboBox in VB.NET

[英]How to Link ComboBox with Database and Show values in TextBox if Select ComboBox in VB.NET

I am trying to select a database value from Combobox that has been populated from the database and displayed in Combobox, and I want to click on any option in Combobox and it should display values in the textbox but upon doing so, I get an error that says: data is null. This method or property cannot be called on NULL values.我正在尝试从 Combobox 中获取一个数据库值 select,该值已从数据库中填充并显示在 Combobox 中,我想单击 Combobox 中的任何选项,它应该在文本框中显示值,但在这样做时,我收到一个错误说:数据是 null。不能对 NULL 值调用此方法或属性。

The error: https://snipboard.io/Rn6YwC.jpg CONTINUED SCREENSHOT: https://snipboard.io/i3l8MO.jpg错误: https://snipboard.io/Rn6YwC.jpg续屏: https://snipboard.io/i3l8MO.jpg

This is my code:这是我的代码:

 Private Sub CollectionEntry_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=golden_star"



    Dim READER As MySqlDataReader
    Try
        MysqlConn.Open()
        Dim Query As String
        Query = "select * from golden_star.loan"

        Command = New MySqlCommand(Query, MysqlConn)



        READER = Command.ExecuteReader

        While READER.Read()
            Dim sname = READER.GetString("account_name")
            ComboBox1.Items.Add(sname)

        End While

        MysqlConn.Close()

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

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=golden_star"


    Dim READER As MySqlDataReader
    Dim table As New DataTable
    Try
        MysqlConn.Open()
        Dim Query As String
        Query = "select * from golden_star.loan where account_name = '" & ComboBox1.Text & "' "

        Command = New MySqlCommand(Query, MysqlConn)

        READER = Command.ExecuteReader

        While READER.Read
            txtID.Text = READER.GetInt32("id")
            DatePick.Text = READER.GetDateTime("date")
            txtName.Text = READER.GetString("account_name")
            txtCollections.Text = READER.GetInt32("collections")
            txtContact.Text = READER.GetInt32("contact")
        End While

        MysqlConn.Close()

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

The first code is loading data from the database and putting it on Combobox when form onLoad and the second form is Combobox selected index changed.第一个代码是从数据库加载数据,当表单 onLoad 时将其放在 Combobox 上,第二个表单是 Combobox 选定的索引更改。 Selecting one of the lists and displaying it on the textbox.选择其中一个列表并将其显示在文本框上。

I can't seem to find the error that's causing the error to pop up.我似乎找不到导致错误弹出的错误。 Where did I go wrong that it pops when selecting the list values in Combobox?我 go 哪里错了,在 Combobox 中选择列表值时会弹出?

You don't need any code at all when the user makes a selection.当用户做出选择时,您根本不需要任何代码。 You're getting all the data on the first query so just use that data.您正在获取第一个查询的所有数据,因此只需使用该数据即可。 Populate a DataTable and bind it to all the controls, preferably via a BindingSource .填充DataTable并将其绑定到所有控件,最好通过BindingSource The other controls will then update automatically when a selection is made.做出选择后,其他控件将自动更新。

Using connection As New MySqlConnection("connection string here"),
      command As New SqlCommand("SELECT * FROM golden_star.loan", connection)
    connection.Open()

    Using reader = command.ExecuteReader()
        Dim table As New DataTable

        table.Load(reader)
        BindingSource1.DataSource = table
    End Using
End Using

With ComboBox1
    .DisplayMember = "account_name"
    .ValueMember = "id"
    .DataSource = BindingSource1
End With

txtID.DataBindings.Add(NameOf(txtID.Text), BindingSource1, "id")
DatePick.DataBindings.Add(NameOf(DatePick.Value), BindingSource1, "date")
'etc

Note that, for the DateTimePicker , you should be using the Value property, which is type DateTime , not the Text property.请注意,对于DateTimePicker ,您应该使用DateTime类型的Value属性,而不是Text属性。

Note that the BindingSource gets added to the form in the designer, along with the controls.请注意, BindingSource与控件一起被添加到设计器中的窗体中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM