selecting one row from datagrid

R

rcoco

I want to select one Row from a datagrid so I create a
textBbox(txtname) and Button for the user to write the name in textBox
and By the click of the button it select the corresponding row. But
it's not selecting the row neighther I'm getting any mistake message
I'm I missng some thing? I wrote the code on Button_Click sqlCommand as
follows:
SqlCommand myCommand = new SqlCommand("SELECT * from employee where
fullname Like %+txtname+%" ,con);
Thank you.
 
R

Rastko Soskic

Well, I'm not quite sure if you made mistake when you wrote this post, but,
according to
what you wrote here, mistake is in string concatenation. You should write:
SqlCommand myCommand = new SqlCommand("SELECT * from employee where
fullname Like %" + txtname + "%" ,con); // Note opening and closing quotes

Furthermore, if txtname is id of textbox (and not the string) then you
should write txtname.Text
This much I could write, because you haven't supplied much info...
 
R

rcoco

Thanks,
the whole code actually goes like this as per now hope you could hlp
me:

try
{
con.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="SELECT * from employee where fullname Like
%"(e-mail address removed)+"%";
SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds,"isp_email.staff");
con.Open();
myCommand.ExecuteNonQuery();
dgupdate.DataSource=ds;
dgupdate.DataBind();
con.Close();
}
catch(Exception err)
{
Console.WriteLine(err.Message);
}
finally
{
con.Close();
}
Thank you
 
R

Rastko Soskic

Well, as bpd said, first you should remove @.
Then, after line: "myAdapter.Fill..." ...
there is no need to perform opening con, executing command and closing con
again,
(unless you are trying to do something else with that?)
adapter should do it for you, but there is nothing wrong if you are opening
and closing manually.

So, your try block should be something like this...
try
{
con.Open(); // Assuming that con object is properly created with correct
connection string
SqlCommand myCommand = new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="SELECT * from employee where fullname Like %" +
txtname.Text + "%";
SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds,"isp_email.staff");
con.Close();
dgupdate.DataSource=ds;
dgupdate.DataBind();
}
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top