VB.NET: ComboBox in dynamic SQL Statement Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • italian464
    New Member
    • Aug 2006
    • 1

    #1

    VB.NET: ComboBox in dynamic SQL Statement Question

    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?!
  • krodman
    New Member
    • Aug 2006
    • 12

    #2
    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 SelectedIndexCh anged 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 ExampleDataTabl e() As DataTable
    Dim dt As New DataTable()
    Dim ComboDisplayDat a() As String = {"Display Text One", "Display Text Two"}
    Dim ComboValueData( ) As Integer = {0, 1}

    With dt.Columns
    .Add("ValueData ")
    .Add("DisplayDa ta")
    End With

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

    row("ValueData" ) = ComboValueData( i)
    row("DisplayDat a") = ComboDisplayDat a(i)

    dt.Rows.Add(row )
    Next

    Return dt

    End Function

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

    ComboBox1.Displ ayMember = "DisplayDat a" 'Data displayed to user
    ComboBox1.Value Member = "ValueData" 'The value needed for sql statement
    ComboBox1.DataS ource = Me.ExampleDataT able()

    End Sub

    Private Sub ComboBox1_Selec tedIndexChanged (ByVal sender As Object, ByVal e As System.EventArg s) Handles ComboBox1.Selec tedIndexChanged
    Dim ValueNeededForS qlStatement 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_Selec tedIndexChanged (ByVal sender As Object, ByVal e As System.EventArg s) Handles ComboBox1.Selec tedIndexChanged
    Dim ValueNeededForS qlStatement As String = Trim(ComboBox1. SelectedText)

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

    End Sub

    I hope this help!

    Comment

    Working...