通过单击Visual Basic 2012中的ComboBox项来显示TextBox中的SQL数据 [英] Displaying Data from SQL in TextBox by clicking ComboBox item in Visual Basic 2012

查看:101
本文介绍了通过单击Visual Basic 2012中的ComboBox项来显示TextBox中的SQL数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



这是我保存的代码:



I have three columns in SQL, which I want the data displayed in three textboxes by clicking an item in ComboBox.

The issue is not really displaying. I can save the data fine and display. However, one of the text fields will only show one cell and not update when a different item is selected. Hope this is making sense.

Here is the code I have for saving:

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





这里是用form1显示的代码:





And here is the code for displaying in form1:

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

    End Sub
End Class





从代码中可以看出,我想通过单击在两个单独的文本框中显示Title和DiaryContent Combobox中的标题。这很好用。我遇到的问题是DiaryDate只显示第一个条目,并且在从ComboBox中选择项目时不会更新。



有人可以帮忙吗?



As you can see from the code, I want to display Title and DiaryContent in two separate textboxes by clicking on Title in the Combobox. This works fine. The issue I have is DiaryDate only shows the first entry and does not update when selecting the Item from ComboBox.

Can anyone help?

推荐答案

您好。



按以下方式排序:



替换它:



Hi.

Got this sorted by using the following:

Replacing This:

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


 End If





随着这:





With This:

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


这篇关于通过单击Visual Basic 2012中的ComboBox项来显示TextBox中的SQL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆