R
Rob Venable
Hi all,
Am I on the right track here or am I going about this the wrong way. I'm
new at VB.Net and I've written a little App that displays all contacts
in a Listbox.That part I've got but what I want to do is display all
contact information in the textboxes when I click on a Selected Item in
the Listbox.
I've been reading up and decided the best way to populate the listbox
would be to populate a dataset and then query the dataset to fill in my
textboxes raher than going back to the database.
Here's the code to populate the listbox:
Private Sub ShowContacts()
Dim cn As SqlConnection = New SqlConnection()
cn.ConnectionString =
Configuration.ConfigurationSettings.AppSettings("CONN_STRING")
Dim strSQL As String = "SELECT * FROM contacts"
Try
Dim CM As SqlCommand = New SqlCommand(strSQL, cn)
Dim DA As SqlDataAdapter = New SqlDataAdapter()
DA.SelectCommand = CM
cn.Open()
DA.Fill(DS, "Contacts")
lstContacts.DataSource = DS.Tables("Contacts")
lstContacts.ValueMember = "contact_id"
lstContacts.DisplayMember = "name"
cn.Close()
Catch ex As SqlException
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try
End Sub
I've declared my dataset at the form level so I could use it in my
lstContacts_SelectedIndexChanged Subroutine.
I know I can get the id from the Selected Item by calling the
lstContacts.SelectedValue property but I'm not sure how to query the
dataset and have the contact information return from the selected
item(contact_id) to populate these textboxes e.g txtFirstname,
txtLastname, txtAddress etc...
Am I going about this the right way or is there a better way of doing
this.
Any help would be greatly appreciated.
Thanks
Rob
Am I on the right track here or am I going about this the wrong way. I'm
new at VB.Net and I've written a little App that displays all contacts
in a Listbox.That part I've got but what I want to do is display all
contact information in the textboxes when I click on a Selected Item in
the Listbox.
I've been reading up and decided the best way to populate the listbox
would be to populate a dataset and then query the dataset to fill in my
textboxes raher than going back to the database.
Here's the code to populate the listbox:
Private Sub ShowContacts()
Dim cn As SqlConnection = New SqlConnection()
cn.ConnectionString =
Configuration.ConfigurationSettings.AppSettings("CONN_STRING")
Dim strSQL As String = "SELECT * FROM contacts"
Try
Dim CM As SqlCommand = New SqlCommand(strSQL, cn)
Dim DA As SqlDataAdapter = New SqlDataAdapter()
DA.SelectCommand = CM
cn.Open()
DA.Fill(DS, "Contacts")
lstContacts.DataSource = DS.Tables("Contacts")
lstContacts.ValueMember = "contact_id"
lstContacts.DisplayMember = "name"
cn.Close()
Catch ex As SqlException
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try
End Sub
I've declared my dataset at the form level so I could use it in my
lstContacts_SelectedIndexChanged Subroutine.
I know I can get the id from the Selected Item by calling the
lstContacts.SelectedValue property but I'm not sure how to query the
dataset and have the contact information return from the selected
item(contact_id) to populate these textboxes e.g txtFirstname,
txtLastname, txtAddress etc...
Am I going about this the right way or is there a better way of doing
this.
Any help would be greatly appreciated.
Thanks
Rob