How to populate gridview on button_click after searching from access database?

Posted by Usman on Stack Overflow See other posts from Stack Overflow or by Usman
Published on 2012-07-04T09:11:04Z Indexed on 2012/07/04 9:15 UTC
Read the original article Hit count: 260

Filed under:
|

I am creating a form in c#.net . I want to populate the gridview only on button click with entries meeting search criteria.

I have tried but on searching ID it works but on searching FirstName it gives error plz check SQL also.

My Code behind

private void button1_Click(object sender, EventArgs e)
 {
            try
            {
                string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=L:/New project/Project/Project/Data.accdb";

                string sql = "SELECT * FROM AddressBook WHERE FirstName='" + textBox1.Text.ToString();
                OleDbConnection connection = new OleDbConnection(strConn);
                OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
                DataSet ds = new DataSet();
                connection.Open();
                dataadapter.Fill(ds, "AddressBook");
                connection.Close();
                dataGridView1.DataSource = ds;

                dataGridView1.DataMember = "AddressBook";
            }
            catch (System.Exception err)
            {
                this.label27.Visible = true;
                this.label27.Text = err.Message.ToString();
            }
   }

© Stack Overflow or respective owner

Related posts about c#

Related posts about sql