Connecting Tech Pros Worldwide Forums | Help | Site Map

VB.NET: ComboBox in dynamic SQL Statement Question

Newbie
 
Join Date: Aug 2006
Posts: 1
#1: Aug 9 '06
Have some mercy on me, I am a NEWBIE! :rolleyes:

I was wondering if in VB.NET it was possible to take a combobox and use that as part of a select statement to change a label. Obviously this is done through an event handler, but I am not sure how to set up the code to use the selected value of the combo box and insert it in a sql statment.

I have no sample code, for I am lost in even starting this part of the project. Please help anybody?!

Newbie
 
Join Date: Aug 2006
Location: Ohio
Posts: 12
#2: Aug 13 '06

re: VB.NET: ComboBox in dynamic SQL Statement Question


In .NET a ComboBox can be used similarly as an HTML DropDownList in that it has a SelectedValue and a SelectedText property. This only applies when you're binding the ComboBox to a DataSource, and have the DisplayMember & ValueMember properties set. To know when the user changes their selection use the SelectedIndexChanged event of the ComboBox, and get the value from the property having the data that you need.

Bound ComboBox Example:
'Preferrably you would be getting your binding data from the database
Private Function ExampleDataTable() As DataTable
Dim dt As New DataTable()
Dim ComboDisplayData() As String = {"Display Text One", "Display Text Two"}
Dim ComboValueData() As Integer = {0, 1}

With dt.Columns
.Add("ValueData")
.Add("DisplayData")
End With

For i As Integer = 0 To ComboDisplayData.Length - 1
Dim row As DataRow = dt.NewRow

row("ValueData") = ComboValueData(i)
row("DisplayData") = ComboDisplayData(i)

dt.Rows.Add(row)
Next

Return dt

End Function

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

ComboBox1.DisplayMember = "DisplayData" 'Data displayed to user
ComboBox1.ValueMember = "ValueData" 'The value needed for sql statement
ComboBox1.DataSource = Me.ExampleDataTable()

End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim ValueNeededForSqlStatement As Integer = CInt(ComboBox1.SelectedValue)

'Your SQL Statement with concatenated value here
'Lable.Text = Value From Database

End Sub

Static ComboBox Items Example:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim ValueNeededForSqlStatement As String = Trim(ComboBox1.SelectedText)

'Your SQL Statement with concatenated value here
'Lable.Text = Value From Database

End Sub

I hope this help!
Reply