Prolem with parameter!


R

rcoco

I'm having an error message sounding like :
Prepared statement '(@id text)SELECT * from isp_email.staff where @id
like +txtname.' expects parameter @id, which was not supplied.
This message appear when I press button to select a row at run time.
What could be the problem?

SqlCommand myCommand = new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="SELECT * from isp_email.staff where @id like
+txtname.Text";
SqlParameter myparam = new SqlParameter("@id",SqlDbType.Text);
myparam.Value=ID;
myCommand.Parameters.Add(myparam);
SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds);
con.Open();
myCommand.ExecuteNonQuery();
dgupdate.DataSource=ds;
dgupdate.DataBind();
con.Close();
Thank you.
 
Ad

Advertisements

B

bpd

I believe the statement needs to be

myCommand.CommandText="SELECT * from isp_email.staff where id like"
+ txtname.Text;

Remove the parameter code.
Move the last " to after like.
 
B

bpd

I forgot to add % to the SQL statement. It should be:

myCommand.CommandText="SELECT * from isp_email.staff where id like %" +
txtname.Text + "%";

my apologies...
 
Ad

Advertisements

B

bruce barker

you code allows sql injection it should be:

[email protected]"
select *
from isp_email.staff
where id like @id + '%'";
SqlParameter myparam = new SqlParameter("@id",SqlDbType.Text);
myparam.Value=txtname.Text;
myCommand.Parameters.Add(myparam);


-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Trouble with parameter 2
DropDownList 1
Using HyperlinkColumn! 4
data grid fails to update! 3
Button Column Select. 18
dropdownlist 4
System.Data.DataRow error 1
Something Wrong? 9

Top