Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 sql exception thrown

Author  Topic 

wwg
Starting Member

1 Post

Posted - 2004-09-22 : 17:46:58
I'm having trouble with some code that should be pretty easy. I have a text box where I enter a name, press a button and a query is sent to the db. Then the results of the query are databinded to a label. Works great the first time but when I try to enter something different in the text box a sql exception is thrown.

Here's the code for the button:

private void button2_Click(object sender, System.EventArgs e)
{



try
{
sqlSelectCommand1.Connection = sqlConnection1;
sqlSelectCommand1.CommandText =sqlDataAdapter1.SelectCommand.CommandText + " WHERE first_name='"+ txtFirst_Search.Text +"'";
sqlDataAdapter1.Fill(dataSet11);

}



catch(SqlException sq_ex)

{

MessageBox.Show(sq_ex.Message + "\nLine " + sq_ex.LineNumber + "\n" + sq_ex.Source);


}



lblFirst_Name.DataBindings.Clear();
lblFirst_Name.DataBindings.Add("Text", dataSet11.tblEmployee_Added, "first_name");

sqlConnection1.Close();




}

Exception says "Incorrect syntax near keyword WHERE"...

But it runs great the first time?

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-09-22 : 22:59:48
Run it in debug and give us the final command you're sending to SQL Server so we don't have to parse your code. :)

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

sabirpatel
Starting Member

22 Posts

Posted - 2004-09-23 : 01:47:46
Hi,

You use the SQL profiler to trace the call to the procedure. You will be able to debug it.

Best of luck.

Regards
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-09-23 : 05:38:03
according to my ESP server this is your problem:
sqlSelectCommand1.CommandText = sqlDataAdapter1.SelectCommand.CommandText + " WHERE first_name='"+ txtFirst_Search.Text +"'";
because of that your statement looks like:
select ... from ... where ... where...

you probably need to handle that

EDIT:
as you do a serch maybe you should use
"WHERE first_name like '%" + txtFirst_Search.Text + "%'"

Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -