0

i have created MS Access Database and it is working fine (ADD, DELETE, Update) now i need to use search function. i need to search using text box and a button. i need to know how to search database using primary key and get those row values in to a text box or list box

I did it like this,

        con.Open();
        OleDbCommand cmd_ni = new OleDbCommand();
        cmd_ni.Connection = con;
        cmd_ni.CommandText = "SELECT * FROM Table1 WHERE vehicle_num = @vehicle_num";
        cmd_ni.CommandType = CommandType.Text;

        cmd_ni.Parameters.Add("id", OleDbType.VarChar);
        cmd_ni.Parameters["id"].Value = nu_m;

        OleDbDataReader dr1 = cmd_ni.ExecuteReader();
        while (dr1.Read())
        {
            string ni;

            ni = dr1["vehicle_num"].ToString();

            if (nu_m == ni)
            {
                Class1.nn = ni;

                DialogResult r = MessageBox.Show("the details");

                if (r == DialogResult.OK)
                {
                    //here i need the code to select the data row & show it in textbox.
                }

            }
        }
        dr1.Close();
        con.Close();

after this point i cannot understand what to do. please help me....

1 Answers1

0

Welcome to SO, You should try to google the answer first. But here are some resources that will help you on your way.

You should just add a textbox to your winform, and a button and on button click execute the code you show above. The only thing you need to add is a new parameter to your where statement.

This looks like a good walkthrough for you Youtube and this SO answer looks at how to best structure it.

And after reading the answers look at related on the right hand side of the questions, There are many questions there that are looking in depth at this.

Community
  • 1
  • 1
Archlight
  • 2,019
  • 2
  • 21
  • 34