Click here to Skip to main content
15,878,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

VB
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


And here is the code for displaying in form1:

VB
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


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?
Posted

1 solution

Hi.

Got this sorted by using the following:

Replacing This:

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


 End If


With This:

VB
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900