To Show Related Data In Textbox When We Click Related Item In Listbox

Satyapriyanayak
Posted by Satyapriyanayak under VB.NET category on | Points: 40 | Views : 767
Imports System.Data.SqlClient
Module Module1
Public con As New SqlConnection("workstation id=""HOME-Z8CKE1NER2"";packet size=4096;user id=sa;initial catalog=pint" & _
"u;persist security info=False")
Public com As SqlCommand
Public sqlda As SqlDataAdapter
Public ds As DataSet
Public str As String
Public dt As DataTable
Public dr As DataRow
Public dc As DataColumn
Public bm As BindingManagerBase
Public flag As Byte
Public objcom As SqlCommandBuilder
End Module

Method-1

Imports System.Data.SqlClient

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Try
con.Open()
str = "select * from student"
com = New SqlCommand(str, con)
Dim reader As SqlDataReader = com.ExecuteReader
While reader.Read
ListBox1.Items.Add(reader("sid"))
End While
reader.Close()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Try
con.Open()
str = "select * from student where sid='" & ListBox1.Text & "'"
com = New SqlCommand(str, con)
Dim reader As SqlDataReader = com.ExecuteReader
Do While reader.Read
TextBox1.Text = reader("sid")
TextBox2.Text = reader("sname")
TextBox3.Text = reader("smarks")
TextBox4.Text = reader("saddress")
TextBox5.Text = reader("year")
Loop
reader.Close()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class


Method-2

Imports System.Data.SqlClient

Public Class Form2
Inherits System.Windows.Forms.Form

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Try
'con.Open()
str = "select * from student"
com = New SqlCommand(str, con)
sqlda = New SqlDataAdapter(com)
ds = New DataSet
sqlda.Fill(ds, "student")
ListBox1.DataSource = ds.Tables("student")
ListBox1.DisplayMember = "sid"
'con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Try
con.Open()
str = "select * from student where sid='" & ListBox1.Text & "'"
com = New SqlCommand(str, con)
Dim reader As SqlDataReader = com.ExecuteReader
Do While reader.Read
TextBox1.Text = reader("sid")
TextBox2.Text = reader("sname")
TextBox3.Text = reader("smarks")
TextBox4.Text = reader("saddress")
TextBox5.Text = reader("year")
Loop
reader.Close()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
End Class

Method-3


Imports System.Data.SqlClient

Public Class Form3
Inherits System.Windows.Forms.Form

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Try
con.Open()
str = "select * from student"
com = New SqlCommand(str, con)
sqlda = New SqlDataAdapter(com)
ds = New DataSet
sqlda.Fill(ds, "student")
dt = ds.Tables("student")
Dim i As Integer
For i = 0 To dt.Rows.Count - 1
ListBox1.Items.Add(dt.Rows(i).ItemArray(0))
Next
con.Close()

Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

Try
con.Open()
str = "select * from student where sid='" & ListBox1.Text & "'"
com = New SqlCommand(str, con)
Dim reader As SqlDataReader = com.ExecuteReader
Do While reader.Read
TextBox1.Text = reader("sid")
TextBox2.Text = reader("sname")
TextBox3.Text = reader("smarks")
TextBox4.Text = reader("saddress")
TextBox5.Text = reader("year")
Loop
reader.Close()
con.Close()

Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
End Class

Comments or Responses

Login to post response