01.//Bind Your Table With Datagrid...
02.
03.private void Bind()
04.{
05. OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/mdb1.mdb");
06. conn.Open();
07. OleDbCommand cmd = new OleDbCommand("SELECT * FROM Employee", conn);
08. DataSet ds = new DataSet();
09. OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
10. adp.Fill(ds);
11. dataGridView1.DataSource = ds.Tables[0];
12. conn.Close();
13.}
14.
15.private void Form1_Load(object sender, EventArgs e)
16.{
17. Bind();
18.}
19.
20.private void button1_Click(object sender, EventArgs e)
21.{
22. OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/mdb1.mdb");
23. try
24. {
25. int flag = 0;
26. conn.Open();
27. OleDbDataReader dr;
28. OleDbCommand cmd=new OleDbCommand("SELECT EmpName FROM Employee",conn);
29. dr = cmd.ExecuteReader();
30. while (dr.Read())
31. {
32. //Here It Will Check that Is Name Is Already In Database Or Not...
33. if(textBox1.Text.ToString() == dr.GetValue(0).ToString())
34. {
35. MessageBox.Show("Name Is Already In The Database.");
36. flag = 0;
37. }
38. else
39. {
40. flag = 1;
41. }
42. }
43. conn.Close();
44.
45. //If Name Is Not In Database Then Insert That Name in Database....
46. if (flag == 1)
47. {
48. conn.Open();
49. cmd.CommandText = "INSERT INTO Employee (EmpId,EmpName) VALUES("+ Convert.ToInt16(txtId.Text) +",'" + textBox1.Text + "')";
50. cmd.ExecuteNonQuery();
51. conn.Close();
52. }
53. Bind();
54. }
55. catch (Exception ex)
56. {
57. MessageBox.Show(ex.ToString());
58. }
59. }
60.}