I don’t want to use built-in data binding through Visual Studio, I would like to learn and code directly. I have created a form with a listbox on the left side and several texbox on the right side. I have code working that populates a listbox from my data reader (MyReader). The listbox works fine. There are two (2) buttons over the listbox to select All Active records or All Inactive records that queries the field “Status” in the “tblAdvocates” table.
So the listbox works fine. Problem is trying to setup the SelectedIndexChanged function. I can’t figure out how to take the selected record from the listbox and use that to fill the textboxes. Do I “re-query” the database using the ID field from the table? Do I use the MyReader from the form load code and pull the data from that?
I’m really at a loss. Your guidance would be appreciated? At 42 years old I feel a little like a “dinosaur” trying to learn a new trick! LOL
Here is my code:
Imports System.Data.SqlClient
Imports System.Windows.Forms.ListBox
Imports System.Data
Imports System.Data.OleDb
Public Class frmAdvocates
Inherits System.Windows.Forms.Form
'Create ADO.NET objects.
Private myConn As SqlConnection
Private myCmd As SqlCommand
Private myReader As SqlDataReader
Private results As String
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Close the database connection
myReader.Close()
myConn.Close()
'Close this form
Me.Close()
End Sub
Private Sub frmAdvocates_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Clear items in listbox
lbxVictimAdvocates.Items.Clear()
'Create a Connection object.
myConn = New SqlConnection("Initial Catalog=VictimAdvantage_db;" & _
"Data Source=WALSH\SQLEXPRESS;Initial Catalog=VictimAdvantage_db;Integrated Security=True")
'Create a Command object.
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT NameFirst, NameMidInt, NameLast, ID, Status FROM tblAdvocates WHERE Status='Active' ORDER BY NameLast"
'Open the connection.
myConn.Open()
'Fill reader
myReader = myCmd.ExecuteReader()
'Concatenate the query result into a string.
Do While myReader.Read()
results = results & myReader.GetString(2) & ", " & myReader.GetString(0) & " " & myReader.GetString(1) & vbLf
lbxVictimAdvocates.Items.Add(myReader.GetString(2) & ", " & myReader.GetString(0) & vbLf)
Loop
'Check for DOB entry and calculate age or leave empty
If txtDOB.Text = String.Empty Then
txtAge.Text = ""
Else
txtAge.Text = DateDiff(DateInterval.Year, CDate(txtDOB.Text.Trim), Now.Date)
End If
'Display proper name on Advocate list groupbox
gbxAdvocates.Text = "Advocates - Active"
gbxAdvocates.ForeColor = Color.DarkGreen
'Check for email address
If txtEmail.Text = String.Empty Then
btnSendEmail.Enabled = False
Else
btnSendEmail.Enabled = True
End If
End Sub
Private Sub lbxVictimAdvocates_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbxVictimAdvocates.SelectedIndexChanged
'txtNameLast.Text = lbxVictimAdvocates.Items(lbxVictimAdvocates.SelectedIndex)
'Reference By Name, where Column Name = Description.
txtNameLast.Text = myReader.Item(2).ToString()
'Reference By Index (Zero Based).
txtNameFirst.Text = myReader.Item(1).ToString()
'Check for DOB entry and calculate age or leave empty
If txtDOB.Text = String.Empty Then
txtAge.Text = ""
Else
txtAge.Text = DateDiff(DateInterval.Year, CDate(txtDOB.Text.Trim), Now.Date)
End If
'Check for email address
If txtEmail.Text = String.Empty Then
btnSendEmail.Enabled = False
Else
btnSendEmail.Enabled = True
End If
End Sub
Private Sub btnDisplayRecordsInactive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayRecordsInactive.Click
'Display proper name on Advocate list groupbox
gbxAdvocates.Text = "Advocates - Inactive"
gbxAdvocates.ForeColor = Color.Red
'Clear items in listbox
lbxVictimAdvocates.Items.Clear()
'Create a Connection object.
myConn = New SqlConnection("Initial Catalog=VictimAdvantage_db;" & _
"Data Source=WALSH\SQLEXPRESS;Initial Catalog=VictimAdvantage_db;Integrated Security=True")
'Create a Command object.
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT NameFirst, NameMidInt, NameLast, ID, Status FROM tblAdvocates WHERE Status='Inactive' ORDER BY NameLast"
'Open the connection.
myConn.Open()
myReader = myCmd.ExecuteReader()
'Concatenate the query result into a string.
Do While myReader.Read()
results = results & myReader.GetString(2) & ", " & myReader.GetString(0) & " " & myReader.GetString(1) & vbLf
lbxVictimAdvocates.Items.Add(myReader.GetString(2) & ", " & myReader.GetString(0) & vbLf)
Loop
End Sub
Private Sub btnDisplayAdvocatesActive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayAdvocatesActive.Click
'Display proper name on Advocate list groupbox
gbxAdvocates.Text = "Advocates - Active"
gbxAdvocates.ForeColor = Color.DarkGreen
'Clear items in listbox
lbxVictimAdvocates.Items.Clear()
'Create a Connection object.
myConn = New SqlConnection("Initial Catalog=VictimAdvantage_db;" & _
"Data Source=WALSH\SQLEXPRESS;Initial Catalog=VictimAdvantage_db;Integrated Security=True")
'Create a Command object.
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT NameFirst, NameMidInt, NameLast, ID, Status FROM tblAdvocates WHERE Status='Active' ORDER BY NameLast"
'Open the connection.
myConn.Open()
myReader = myCmd.ExecuteReader()
'Concatenate the query result into a string.
Do While myReader.Read()
results = results & myReader.GetString(2) & ", " & myReader.GetString(0) & " " & myReader.GetString(1) & vbLf
lbxVictimAdvocates.Items.Add(myReader.GetString(2) & ", " & myReader.GetString(0) & vbLf)
Loop
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
End Sub
End Class

New Topic/Question
Reply




MultiQuote





|