Correct syntax for pasing a SELECtT statement in a string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What is the correct syntax for passing a select statement via the Reader.

The following code doesn't return anything.

Dim strSQL2 As String = "SELECT * from ATable WHERE ' " &
ComboBox1.Text & " ' = ' " & TextBox1.Text & " '"

Dim cmd As New OleDbCommand(strSQL2, Conn)
Dim rdr As OleDbDataReader
rdr = cmd.ExecuteReader
ListBox1.Items.Clear()
ListBox1.Items.Add("")
While rdr.Read
ListBox1.Items.Add(rdr("Col_One") & " " & rdr("Col_Two") &
" " & rdr("Col_Three") & " " & rdr("Col_Four"))
End While
rdr.Close()


Thanks
 
Column names should not be quoted

Dim strSQL2 As String = "SELECT * from ATable WHERE " &
ComboBox1.Text & " = ' " & TextBox1.Text & " '"

should work.

Ranjan
 
Gordon,

A webpage sample watch typos changed in this message

\\\
cmd.CommandText = "Select * from tblUsers " & _
"where (UserId = @UserId)"
Dim myparam1 As New OleDb.OleDbParameter("@UserId", _
OleDb.OleDbType.Integer)
cmd.Parameters.Add(myparam1)
myparam1.Value = 1
Dim dr As OleDb.OleDbDataReader
dr = cmd.ExecuteReader()
DataGrid1.DataSource = dr
DataGrid1.DataBind()
///
I hope this helps?

Cor
 
Back
Top