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.
 
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...
 
B

bruce barker

you code allows sql injection it should be:

myCommand.CommandText=@"
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

DropDownList 1
Using HyperlinkColumn! 4
Images in datagrid! 0
RadioButtonList&Datagrid 0
Button Column Select. 18
Editable datagrid 5
data grid fails to update! 3
dropdownlist 4

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top