Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Combo-Box Text in SQL-Query
 

Combo-Box Text in SQL-Query

Hi,
i have a simple search in an application:

Private Sub btnSuche_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuche.Click
    
        Dim cmd As DbCommand = New SqlCommand()
        cmd.CommandText = "SELECT * FROM stat11patienten"
        cmd.Connection = conn
        Dim ds As New DataSet()
        Dim da As DbDataAdapter = New SqlDataAdapter()
        da.SelectCommand = cmd
        da.Fill(ds)
        Dim tbl As DataTable = ds.Tables(0)

        ListView1.Clear()

        initializeListView()

        Dim rows() As DataRow = tbl.Select("Vorname LIKE '" & txtSuche.Text & "'")
        For Each row As DataRow In rows
            Dim itm As New ListViewItem(row("ID").ToString)
            itm.SubItems.Add(row("Nachname").ToString)
            itm.SubItems.Add(row("Vorname").ToString)
            itm.SubItems.Add(row("Dauer").ToString)
            itm.SubItems.Add(row("Fachbereich").ToString)
            Dim d As DateTime = Convert.ToDateTime(row("Datum"))
            itm.SubItems.Add(d.Date.ToString(d))

            ListView1.Items.Add(itm)

        Next

    End Sub
In this line:
Dim rows() As DataRow = tbl.Select("Vorname LIKE '" & txtSuche.Text & "'")<br/>

i will use instead of "Vorname" the selected Row-Title that comes from a combo-box.

I dont know the right way to do this.

Thanks for every answers.

Markus
mullfreak  Wednesday, April 15, 2009 6:23 PM

Depending on the style and how the combo box is bound, something like this should work:

Dim rows() As DataRow = tbl.Select(cb.text & " LIKE '" & txtSuche.Text & "'")

  • Marked As Answer bymullfreak Wednesday, April 15, 2009 6:53 PM
  •  
DeborahK  Wednesday, April 15, 2009 6:29 PM
Dim rows() As DataRow = tbl.Select(ComboBox1.SelectedItem.Text & " LIKE '" & txtSuche.Text & "'")
Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
A.m.a.L - aditi.com - Think Product  Wednesday, April 15, 2009 6:31 PM

Depending on the style and how the combo box is bound, something like this should work:

Dim rows() As DataRow = tbl.Select(cb.text & " LIKE '" & txtSuche.Text & "'")

  • Marked As Answer bymullfreak Wednesday, April 15, 2009 6:53 PM
  •  
DeborahK  Wednesday, April 15, 2009 6:29 PM
Dim rows() As DataRow = tbl.Select(ComboBox1.SelectedItem.Text & " LIKE '" & txtSuche.Text & "'")
Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
A.m.a.L - aditi.com - Think Product  Wednesday, April 15, 2009 6:31 PM
Thank you both, i use this one:
Dim rows() As DataRow = tbl.Select(cboSuche.Text & " LIKE '" & txtSuche.Text & "'")

But i can only search strings, no int and date.

I think i need three select´s and a select case for the combobox. Is this correct?

Markus
mullfreak  Wednesday, April 15, 2009 6:53 PM
It seems correct.

Post your code for confirmation
Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
A.m.a.L - aditi.com - Think Product  Wednesday, April 15, 2009 6:54 PM

You can use google to search for other answers

Custom Search

More Threads

• XML Help
• creating a word document - do you know where to look? Help Please...
• Programming for Comm port or USB
• CUSTOM tooltips in VB.NET (bold, italic ...)
• AddHandler causes FileNotFoundException?
• Composite keys
• Unreadeble text
• VB.net, excel - copying excel file attributes
• Visual Basic Pmt, IPmt, and PPmt functions
• Opening remote registry on Vista fails