Hi,
First, your sql command does not seem right. Assuming you have a column
called 'txtname.text' you should use an SqlParameter for the value.
DataAdapter.Fil l will open the connection and close it afterwards by
itself unless the connection is already open.
myCommand.Execu teNonQuery(); serves no function as you aren't using the
result for anything.
This may also be a good time to learn about the 'using' statement, so your
method can be rewritten to the following (beware of linewraps)
private void bttok_click(obj ect sender, System.EventArg s e)
{
// assuming there is a string variable called id and a
connection string called conString
using (SqlConnection con = new SqlConnection(c onString))
{
using (SqlCommand myCommand = new SqlCommand("sel ect *
from employee where txtname.text=@i d", con))
{
SqlParameter myParameter = new SqlParameter("@ id",
SqlDbType.Text) ;
myParameter.Val ue = id;
myCommand.Param eters.Add(myPar ameter);
using (SqlDataAdapter myAdapter = new
SqlDataAdapter( myCommand))
{
DataSet ds = new DataSet();
myAdapter.Fill( ds);
datagrid1.DataS ource = ds;
datagrid1.DataB ind();
}
}
}
}
You may also want to catch any exception thrown if the query or connection
fails in any way to figure out what is wrong.
On Tue, 09 Jan 2007 08:01:47 +0100, rcoco <nc******@yahoo .cawrote:
Hi,
I'm just wondering if I'm on the write truck. this code is ment to look
for an ID number in the datagrid that matches the the ID number that is
in the textbox I created.
But it's not selecting neither is it showing me any error. could some
one help me please?
private void bttok_click(obj ect sender, System.EventArg s e)
{
SqlCommand myCommand = new SqlCommand("sel ect * from employee where
txtname.text=id ",con);
SqlDataAdapter myAdapter=new SqlDataAdapter( myCommand);
DataSet ds = new DataSet();
myAdapter.Fill( ds);
con.Open();
myCommand.Execu teNonQuery();
datagrid1.DataS ource=ds;
datagrid1.DataB ind();
con.Close();
}
Thanks.
--
Happy Coding!
Morten Wennevik [C# MVP]