通过单击Visual Basic 2012中的组合框项在文本框中显示SQL中的数据

通过单击Visual Basic 2012中的组合框项在文本框中显示SQL中的数据,sql,combobox,textbox,Sql,Combobox,Textbox,我在SQL中有三列,我希望通过单击ComboBox中的一项,将数据显示在三个文本框中 问题并没有真正显现出来。我可以很好地保存数据并显示出来。但是,其中一个文本字段将仅显示一个单元格,并且在选择其他项目时不会更新。希望这是有意义的 以下是我保存的代码: Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim myconnect As New SqlClient.SqlC

我在SQL中有三列,我希望通过单击ComboBox中的一项,将数据显示在三个文本框中

问题并没有真正显现出来。我可以很好地保存数据并显示出来。但是,其中一个文本字段将仅显示一个单元格,并且在选择其他项目时不会更新。希望这是有意义的

以下是我保存的代码:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim myconnect As New SqlClient.SqlConnection
    myconnect.ConnectionString = "Data Source=.\INFLOWSQL;Initial Catalog=RioDiary;Integrated Security=True"


    Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()
    mycommand.Connection = myconnect
    mycommand.CommandText = "INSERT INTO MyDiary (Title, DiaryContent, DiaryDate) VALUES (@Title, @DiaryContent, @DiaryDate)"
    myconnect.Open()

    Try
        mycommand.Parameters.Add("@Title", SqlDbType.VarChar).Value = TextBox1.Text
        mycommand.Parameters.Add("@DiaryContent", SqlDbType.VarChar).Value = TextBox2.Text
        mycommand.Parameters.Add("@DiaryDate", SqlDbType.VarChar).Value = TextBox3.Text

        mycommand.ExecuteNonQuery()
        MsgBox("Success")
    Catch ex As System.Data.SqlClient.SqlException
以下是在表格1中显示的代码:

Imports System.Data.SqlClient

Public Class Form1
    Dim con As New SqlConnection
    Dim ds As New DataSet
    Dim da As New SqlDataAdapter
    Dim sql As String
    Dim inc As Integer
    Dim MaxRows As Integer
    Dim max As String
    Dim dt As New DataTable

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'RioDiaryDataSet5.MyDiary' table. You can move, or remove it, as needed.
        Me.MyDiaryTableAdapter.Fill(Me.RioDiaryDataSet5.MyDiary)
        Dim con As New SqlConnection("  Data Source=.\INFLOWSQL;Initial Catalog=RioDiary;Integrated Security=True")
        Dim cmd As New SqlCommand("Select Title,DiaryContent,DiaryDate FROM MyDiary ")
        Dim da As New SqlDataAdapter
        da.SelectCommand = cmd
        cmd.Connection = con
        da.Fill(ds, "MyDiary")

        con.Open()

        ComboBox1.DataSource = ds.Tables(0)
        ComboBox1.DisplayMember = "Title'"
        ComboBox1.ValueMember = "DiaryContent"
    End Sub

    Private Sub NewEntryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewEntryToolStripMenuItem.Click
        AddEntry.Show()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged



        If (Not Me.ComboBox1.SelectedValue Is Nothing) Then
            Me.TextBox2.Text = ComboBox1.Text
            Me.TextBox3.Text = ComboBox1.SelectedValue.ToString


        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
      If dt.Rows.Count > 0 Then
        TextBox1.Text = dt.Rows(0)("DiaryDate").ToString()  'Where ColumnName is the Field from the DB that you want to display

    End If
    End Sub
End Class
正如您从代码中看到的,我希望通过单击组合框中的Title,在两个单独的文本框中显示Title和DiaryContent。这个很好用。 我的问题是DiaryDate只显示第一个条目,在从ComboBox中选择项目时不会更新


有人可以帮忙吗?

您可以尝试使用ComboBox的
SelectedItem
属性。由于ComboBox的数据源是这里的
DataTable
,ComboBox的
SelectedItem
表示
DataTable
中的一行。如果手头有
DataRow
,您可以在文本框中显示
DataRow
的任何列的值:

........
If (Not Me.ComboBox1.SelectedItem Is Nothing) Then
    Dim SelectedItem = TryCast(comboBox1.SelectedItem, DataRowView)
    Me.TextBox1.Text = SelectedItem.Row("Title").ToString()
    Me.TextBox2.Text = SelectedItem.Row("DiaryContent").ToString()
    Me.TextBox3.Text = SelectedItem.Row("DiaryDate").ToString()
End If
........

谢谢你。我尝试了你所说的,但得到了有关系统的错误。Data.Row无法转换为字符串,而且[“Title”]中需要标识符等。更新了我的答案,我的错,被C#syntaxis与你混淆了。哈哈,非常感谢你。很好用!不客气。看着你的个人资料照片,啊。。真想不到。英雄联盟
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim cn As New SqlClient.SqlConnection("Data Source=thee-pc;Initial Catalog=jobportal;Integrated Security=True")
    Dim cmd As New SqlClient.SqlCommand
    Dim tbl As New DataTable
    Dim da As New SqlClient.SqlDataAdapter
    Dim reader As SqlClient.SqlDataReader
    Try
        cn.Open()
        Dim sql As String
        sql = "select * from Register"
        cmd = New SqlClient.SqlCommand(sql, cn)
        reader = cmd.ExecuteReader
        While reader.Read
            Dim id = reader.Item("cid")
            ComboBox1.Items.Add(id)
        End While
        cn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim cn As New SqlClient.SqlConnection("Data Source=thee-pc;Initial Catalog=jobportal;Integrated Security=True")
    Dim cmd As New SqlClient.SqlCommand
    Dim tbl As New DataTable
    Dim da As New SqlClient.SqlDataAdapter
    Dim reader As SqlClient.SqlDataReader
    Try
        cn.Open()
        Dim sql As String
        sql = "select * from register where cid ='" + ComboBox1.Text + "'"
        cmd = New SqlClient.SqlCommand(sql, cn)
        reader = cmd.ExecuteReader
        While reader.Read
            TextBox1.Text = reader.Item("cname")
            TextBox2.Text = reader.Item("dob")

        End While
        cn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub