2
Answers

Error: Incorrect Syntax near 'last_name'

Michelle

Michelle

17y
2.1k
1
For the life of me I cannot figure out what is wrong with my basic testing code. Here is what I have.

SqlDataReader rdr = null;

SqlConnection con = null;

SqlCommand cmd = null;

string txtLastName = TextBox1.Text;

try

{

string ConnectionString = "server=DEL-CD212; uid=sa;" +

"pwd=graver; database=Test;";

con= new SqlConnection(ConnectionString);

con.Open();

 

string CommandText = "SELECT last_name, first_name" + "FROM try" + "WHERE last_name=;" + txtLastName;

cmd = new SqlCommand(CommandText);

cmd.Connection = con;

rdr = cmd.ExecuteReader();

while(rdr.Read())

{

ListBox1.Items.Add(rdr["last_name"].ToString() + ", " + rdr["first_name"].ToString());

}

}

catch(Exception ex)

{

ListBox1.Items.Add(ex.Message);

}

finally

{

if(rdr !=null)

rdr.Close();

if(con.State == ConnectionState.Open)

con.Close();

}

Any help would be greatly appreciated.

Answers (2)