Oladotun Obileye

Oladotun Obileye

  • NA
  • 98
  • 19.4k

how to automatical update datagrid after insert in c#winform

May 27 2019 5:42 PM
Good day everyone i am working on an application in c# windows form that has two usercontrol place on a form.
 
the first one is for registration i.e to register the second one is to view the registered users, to crud. and also has a datagriedview. whenever i register a new it does not show in the second usercontrol except i exit and re-run the code before it will get updated in the datagridview. please i want whenever i insert a new record it should show in the second usercontrol. not only datagridview even label thanks to get updated.
 
this is the usercontrol for registration
  1. private void btnreg_Click(object sender, EventArgs e)  
  2. {  
  3. userprofile up = new userprofile();  
  4. if (txtempno.Text != null && txtfullname.Text != null && dob.Value != null && txtdept.Text != null && txtoccupation.Text != null && txtmaritalstatus.SelectedItem != null && txtemail.Text != null && txtpost.Text != null && txthaddress.Text != null && txtpno.Text != null && txtnation.Text != null && txtorigin.Text != null && pictureBox1.Image != null)  
  5. {  
  6. MemoryStream ms = new MemoryStream();  
  7. pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);  
  8. byte[] img = ms.ToArray();  
  9.   
  10. if (rfemale.Checked == true)  
  11. {  
  12. gender = "Female";  
  13. }  
  14. else if (rmale.Checked == true)  
  15. {  
  16. gender = "Male";  
  17. }  
  18. SqlDataAdapter da = new SqlDataAdapter("Select * FROM Registrationform where Employementnumber='" + txtempno.Text + "' or Email='" + txtemail.Text + "' ", con);  
  19. DataTable dt = new DataTable();  
  20. da.Fill(dt);  
  21. if (dt.Rows.Count >= 1)  
  22. {  
  23. MessageBox.Show("already exist");  
  24. }  
  25. else  
  26. {  
  27. con.Open();  
  28. SqlCommand cmd = new SqlCommand();  
  29. cmd.Connection = con;  
  30. cmd.CommandText = "INSERT INTO Registrationform([Employementnumber],[FullName],[Dateofbirth],[Gender],[Occupation],[Maritalstatus],[Email],[Department],[Position],[HomeAddress],[Phonenumber],[Nationality],[Stateoforigin],[image]) VALUES (@empno,@fullname,@dob,@gen,@occ,@ms,@email,@dept,@post,@haddress,@pno,@nation,@soo,@img)";  
  31. cmd.Parameters.AddWithValue("@empno", txtempno.Text);  
  32. cmd.Parameters.AddWithValue("@fullname", txtfullname.Text);  
  33. cmd.Parameters.AddWithValue("@dob", dob.Value);  
  34. cmd.Parameters.AddWithValue("@gen", gender);  
  35. cmd.Parameters.AddWithValue("@occ", txtoccupation.Text);  
  36. cmd.Parameters.AddWithValue("@ms", txtmaritalstatus.GetItemText(txtmaritalstatus.SelectedItem));  
  37. cmd.Parameters.AddWithValue("@email", txtemail.Text);  
  38. cmd.Parameters.AddWithValue("@dept", txtdept.Text);  
  39. cmd.Parameters.AddWithValue("@post", txtpost.Text);  
  40. cmd.Parameters.AddWithValue("@haddress", txthaddress.Text);  
  41. cmd.Parameters.AddWithValue("@pno", txtpno.Text);  
  42. cmd.Parameters.AddWithValue("@nation", txtnation.Text);  
  43. cmd.Parameters.AddWithValue("@soo", txtorigin.Text);  
  44. cmd.Parameters.AddWithValue("@img", img);  
  45. cmd.ExecuteNonQuery();  
  46. con.Close();  
  47. MessageBox.Show("data entered");  
  48. da.Fill(dt);  
  49. BindingSource bd = new BindingSource();  
  50. up.dtuserpro.DataSource = dt;  
  51. up.dtuserpro.DataSource = bd;  
  52. con.Close();  
  53. }  
  54. }  
  55. else  
  56. {  
  57. MessageBox.Show("Please Fill In All The Necessary Informations");  
  58. }  
  59. }

Answers (2)