Something Wrong?

R

rcoco

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(object sender, System.EventArgs e)
{
SqlCommand myCommand = new SqlCommand("select * from employee where
txtname.text=id",con);
SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds);
con.Open();
myCommand.ExecuteNonQuery();
datagrid1.DataSource=ds;
datagrid1.DataBind();
con.Close();
}
Thanks.
 
M

Morten Wennevik [C# MVP]

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.Fill will open the connection and close it afterwards by
itself unless the connection is already open.

myCommand.ExecuteNonQuery(); 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(object sender, System.EventArgs e)
{
// assuming there is a string variable called id and a
connection string called conString
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand myCommand = new SqlCommand("select *
from employee where txtname.text=@id", con))
{
SqlParameter myParameter = new SqlParameter("@id",
SqlDbType.Text);
myParameter.Value = id;
myCommand.Parameters.Add(myParameter);

using (SqlDataAdapter myAdapter = new
SqlDataAdapter(myCommand))
{
DataSet ds = new DataSet();
myAdapter.Fill(ds);

datagrid1.DataSource = ds;
datagrid1.DataBind();
}
}
}
}

You may also want to catch any exception thrown if the query or connection
fails in any way to figure out what is wrong.
 
R

rcoco

Thank you very much but it's not showing any changes even after trying
the try&cacth.
 
M

Morten Wennevik [C# MVP]

I'm not entirely sure I understand your problem. Is your query not
returning anything? (Set a breakpoint after DataAdapter.Fill to see if it
the DataSet contains any data). Can you query the database directly using
Query Analyzer or Sql Server Management Studio?

If the button event runs without any exceptions, the query is well formed,
and the connection valid, but the query itself may not be asking what you
think it does.
 
K

KWienhold

Looking at your SQL Command I would assume that txtname.text is not a
column name, but refers to a textbox on your form.
If that is the case, changing your sqlcommand like this might help:
SqlCommand myCommand = new SqlCommand("select * from employee where id
= " + txtname.Text,con);

Sincerely,
Kevin Wienhold
 
R

rcoco

Actually The Problem is that When I put An Id number in the text Box
and I press the Button There is no row selected yet I want it to select
the row.
 
G

Guest

Thank you very much but it's not showing any changes even after trying
the try&cacth.



--Happy Coding!
Morten Wennevik [C# MVP]

Did you implemented catch part of code
- - - - - - - - - - - - - - - - - - - -
try
{
// some code
}
catch(Exception ex)
{
}
- - - - - - - - - - - - - - - - - - - -
is not enough.
You have to do something with your exception, for example
- - - - - - - - - - - - - - - - - - - -
try
{
// some code
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
//or
//MessageBox.Show(ex.Message);
//or
//Form1.label1.text= "Error, unable to load data, see details below. \n"
+ ex.Message;
}
- - - - - - - - - - - - - - - - - - - -
best,
Slawomir
 
R

rcoco

Thanks Guys I seem not to get exactly what is wrong with this work
because it's like every thing I try it's not working Let me keep trying
if i get it I will tell you.
Thanks again.
 
R

rcoco

thanks,
But i still don't see where I'm going wrong it's not changed . thanks
any way for the help.
 
M

Morten Wennevik [C# MVP]

You still haven't told us any specifics around your problem. Do you get
an exception? Does the query work in the database?
 

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


Top