Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Data set and query
 

Data set and query

I need to create a query that is determine by what a user clicks on in a listbox that is generated by a dataset and then use that information to create a sql query against an oracle database. I have my dataset setup and it displays in my listbox but how do i read the other column in my dataset that corresponds to what they click on in the listbox?
Cptkirkh  Friday, December 22, 2006 4:35 PM

You display the records in a ListBox

ListBox control has a "DataSource" property, inyou case, you seta dataset asthe datasource of the listbox.

Listbox controlhas "DisplayMember" and "Value Member" fields. DisplayMember field specify the column name to display and the ValueMember field specify the value of the field. Most of the timeID fileds are assigned as the value member field. Whenan item of the listbox is clicked you can get the "selectedValue" and you can do whateever youwant to do with it, you can create new command, or you can get another columns vakue buusing select method and so on..

Check out this sample. Open a new windows form, open the code page, delete everything and paste the code shown below and run it..

Public Class Form1

Dim dtStudents As DataTable
Friend WithEvents lstStudents As ListBox

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' 1st replace the listbox on the form
Me.lstStudents = New ListBox
Me.lstStudents.Width = Me.Width / 2
Me.lstStudents.Height = Me.Height
Me.lstStudents.Location = New Point(0, 0)
Me.Controls.Add(lstStudents)

' 2nd call the sub to create the datatable
CreateDataTable()
' 3rd add the datatable as the datasource of the listbox
Me.lstStudents.DisplayMember = "Name"
Me.lstStudents.ValueMember = "Id"
Me.lstStudents.DataSource = Me.dtStudents
End Sub

''' <summary>
''' this sub will create the data table and add new records in the table
''' </summary>
''' <remarks></remarks>
Public Sub CreateDataTable()
dtStudents =
New DataTable
Dim clId As New DataColumn("Id")
clId.DataType =
GetType(Integer)
Dim clName As New DataColumn("Name")
Dim clSurname As New DataColumn("Surname")
dtStudents.Columns.Add(clId)
dtStudents.Columns.Add(clName)
dtStudents.Columns.Add(clSurname)
For i As Integer = 0 To 20
Dim mRow As DataRow = dtStudents.NewRow
mRow.Item(
"Id") = i * 2
mRow.Item(
"Name") = "Name" & i + 1
mRow.Item(
"Surname") = "Surname" & i + 1
dtStudents.Rows.Add(mRow)
Next
End Sub

Private Sub lstStudents_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstStudents.SelectedValueChanged
ListItemClicked(
Me.lstStudents.SelectedValue)
End Sub

Private Sub ListItemClicked(ByVal StudentId As Integer)
'Do what ever you want to do here
' LIKE: Dim cmd As New SqlClient.SqlCommand("Select * from NOTES where studentId = " & StudentId)
'bla
'bla
'bla
'You can even query the datatable.. So simple :
Dim Surname As String
'Select method of the datatable returs a row() collection, in this case it will return only one row, however you need to specify the index which is (0)
Dim row As DataRow = Me.dtStudents.Select("Id =" & StudentId)(0)
Surname = row.Item(
"Surname").ToString
MsgBox(Surname)
End Sub
End Class

I hope it helps, I spent 15 minutews on it :))

Ayhan Yerli _TR-NL_  Thursday, December 28, 2006 6:37 PM

Cptkirkh,

Here is a good news that it is possible for you to show multi column in one ListBox control. I searched the codeproject and found an example on the Multi Column ListBox. However, it is written in C#.

http://www.codeproject.com/cs/combobox/multicolumnlistbox.asp

You can also download the code sample from the website. Hope that can help you!

Bruno Yu  Thursday, December 28, 2006 6:39 AM

You display the records in a ListBox

ListBox control has a "DataSource" property, inyou case, you seta dataset asthe datasource of the listbox.

Listbox controlhas "DisplayMember" and "Value Member" fields. DisplayMember field specify the column name to display and the ValueMember field specify the value of the field. Most of the timeID fileds are assigned as the value member field. Whenan item of the listbox is clicked you can get the "selectedValue" and you can do whateever youwant to do with it, you can create new command, or you can get another columns vakue buusing select method and so on..

Check out this sample. Open a new windows form, open the code page, delete everything and paste the code shown below and run it..

Public Class Form1

Dim dtStudents As DataTable
Friend WithEvents lstStudents As ListBox

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' 1st replace the listbox on the form
Me.lstStudents = New ListBox
Me.lstStudents.Width = Me.Width / 2
Me.lstStudents.Height = Me.Height
Me.lstStudents.Location = New Point(0, 0)
Me.Controls.Add(lstStudents)

' 2nd call the sub to create the datatable
CreateDataTable()
' 3rd add the datatable as the datasource of the listbox
Me.lstStudents.DisplayMember = "Name"
Me.lstStudents.ValueMember = "Id"
Me.lstStudents.DataSource = Me.dtStudents
End Sub

''' <summary>
''' this sub will create the data table and add new records in the table
''' </summary>
''' <remarks></remarks>
Public Sub CreateDataTable()
dtStudents =
New DataTable
Dim clId As New DataColumn("Id")
clId.DataType =
GetType(Integer)
Dim clName As New DataColumn("Name")
Dim clSurname As New DataColumn("Surname")
dtStudents.Columns.Add(clId)
dtStudents.Columns.Add(clName)
dtStudents.Columns.Add(clSurname)
For i As Integer = 0 To 20
Dim mRow As DataRow = dtStudents.NewRow
mRow.Item(
"Id") = i * 2
mRow.Item(
"Name") = "Name" & i + 1
mRow.Item(
"Surname") = "Surname" & i + 1
dtStudents.Rows.Add(mRow)
Next
End Sub

Private Sub lstStudents_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstStudents.SelectedValueChanged
ListItemClicked(
Me.lstStudents.SelectedValue)
End Sub

Private Sub ListItemClicked(ByVal StudentId As Integer)
'Do what ever you want to do here
' LIKE: Dim cmd As New SqlClient.SqlCommand("Select * from NOTES where studentId = " & StudentId)
'bla
'bla
'bla
'You can even query the datatable.. So simple :
Dim Surname As String
'Select method of the datatable returs a row() collection, in this case it will return only one row, however you need to specify the index which is (0)
Dim row As DataRow = Me.dtStudents.Select("Id =" & StudentId)(0)
Surname = row.Item(
"Surname").ToString
MsgBox(Surname)
End Sub
End Class

I hope it helps, I spent 15 minutews on it :))

Ayhan Yerli _TR-NL_  Thursday, December 28, 2006 6:37 PM

You can use google to search for other answers

Custom Search

More Threads

• VB Mobile Connectivity
• Construct an object of a certain type from a string
• How to create client for OPC UA demo server ?
• Extending Windows Explorer
• Impressible Buttons
• How can I know the cpu and Graphic card information from VB.NET 2005
• Explorer Form Problem
• Visual Basic 2003. Capture output from external exe
• Button_Click Event won't fire
• Immediate mode