| 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!
|