Trying to use a txtbox to input a number for a sql query in VS 2012 C#

Hello, I am creating a Windows Form App that the user will input info into a form and then generate a letter based on the information. I also need for them to be able to lookup these at a later date by a few options (Name, tracking number, amount).

The input form works well the info is going into the DB, but with the windows form and the datagridview. I cant lookup the way I want to it seems to just return all the data.

For example I want to use a text Box to input a tracking number and then return the data for that one row.
riley00Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
So that would look something like:
SqlConnection conn = new SqlConnection(myConnString);
SqlCommand cmd = new SqlCommand("select * from tracking_table where tracking_number = @tracking_number", conn);

cmd.Parameters.Add(new SqlParameter("@tracking_number", txtTrackingNumber.text));

try
{
   conn.Open();
   DataReader dr = conn.ExecuteReader();
   if (dr.Read())
   {
     // process dr
   }
}
catch (Exception ex)
{
  // handle errors.
}
finally
{
 conn.Close();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
riley00Author Commented:
Sorry, I think I get what this code but not sure how to get the gridview to show results of this. I think I have tried to many things and I am just confused right now. Sorry.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Your grid view needs a datasource.

So in your process DR you would put that into a datatable, and from there set the gridview's datasource to a table.

eg:

 // process dr

while (dr.Read())
{
    DataRow row = dataTable.NewRow();
   row["Column1"] = dr["Column1"];
   // add additional columns
   dataTable.Rows.Add(row);
}
GridView.DataSource = dataTable;
GridView.DataBind();

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.