Joel Bwana

Joel Bwana

  • NA
  • 24
  • 1.7k

My update function not working

Aug 27 2020 3:59 PM
When I click "View", data from Database populates the form. I click on a field, make changes, click "Update". Update not working. No error! Where is my problem?
 
Button View
  1. try  
  2. {  
  3. conn.Open();  
  4. index = 0;  
  5. //showData(index);  
  6. var query = "select * FROM Questions"//WHERE AuthorizationCode='" + txtauthorizationcode.Text + "'  
  7. var cmd = new SqlCommand(query, conn);  
  8. SqlDataReader dr = cmd.ExecuteReader();  
  9. var sSingle = "Single Choice";  
  10. var sMultiple = "Multiple Choice";  
  11. while (dr.Read())  
  12. {  
  13. if (dr.GetString(9) == sSingle)  
  14. {  
  15. grpmultipleanswers.Visible = false;  
  16. grpsingleanswers.Visible = true;  
  17. txtquestion.Text = dr.GetString(2);  
  18. //txtquestion.Text = dr.GetValue(2).ToString();  
  19. txta.Text = dr.GetString(3);  
  20. txtb.Text = dr.GetString(4);  
  21. txtc.Text = dr.GetString(5);  
  22. txtd.Text = dr.GetString(6);  
  23. cmbqtype.SelectedItem = dr.GetString(9);  
  24. cmbcomplexity.SelectedItem = dr.GetString(10);  
  25. lblscore.Text = dr.GetInt32(11).ToString();  
  26. lblquestionno.Text ="Question " + dr.GetInt32(0).ToString();  
  27. }  
  28. if (dr.GetString(9) == sMultiple)  
  29. {  
  30. grpmultipleanswers.Visible = true;  
  31. grpsingleanswers.Visible = false;  
  32. txtquestion.Text = dr.GetString(2);  
  33. cmbqtype.SelectedItem = dr.GetString(9);  
  34. cmbcomplexity.SelectedItem = dr.GetString(10);  
  35. lblscore.Text = dr.GetInt32(11).ToString();  
  36. //lblquestion.Text = dr.GetInt32(0).ToString();  
  37. lblquestionno.Text = "Question " + dr.GetInt32(0).ToString();  
  38. //lblquestion.Text = "Question" + " " + dr.GetInt32(0).ToString();  
  39. txtchkchoiceA.Text = dr.GetString(3);  
  40. txtchkchoiceB.Text = dr.GetString(4);  
  41. txtchkchoiceC.Text = dr.GetString(5);  
  42. txtchkchoiceD.Text = dr.GetString(6);  
  43. txtchkchoiceE.Text = dr.GetString(7);  
  44. txtchkchoiceF.Text = dr.GetString(8);  
  45. }  
  46. }  
  47. //btnfirst.Hide();  
  48. btnview.Hide();  
  49. btnupdate.Show();  
  50. conn.Close();  
  51. }  
  52. catch (Exception ex)  
  53. {  
  54. MessageBox.Show("Error Occured" + ex);  
  55. }  
Button Update
  1. try  
  2. {  
  3. SqlConnection conn = new SqlConnection(@"Data Source = (LocalDB)\MSSQLlocaldb; Initial Catalog = AdminAuthentication; Integrated Security = True");  
  4. var query = "update Questions Set QText= @QText, ChoiceA=@ChoiceA, ChoiceB=@ChoiceB,ChoiceC=@ChoiceC,ChoiceD=@ChoiceD, ChoiceE=@ChoiceE,ChoiceF=@ChoiceF,QType=@QType,QComplexity=@QComplexity,QPoints=@QPoints,AuthorizationCode=@AuthorizationCode,Exhibit=@exhibit WHERE QID=@QID";  
  5. var cmd = new SqlCommand(query, conn);  
  6. conn.Open();  
  7. cmd.Parameters.AddWithValue("@QID", SqlDbType.Int).Value = lblquestion.Text;  
  8. cmd.Parameters.AddWithValue("@QText", SqlDbType.VarChar).Value = txtquestion.Text;  
  9. cmd.Parameters.AddWithValue("@ChoiceA", SqlDbType.VarChar).Value = txta.Text;  
  10. cmd.Parameters.AddWithValue("@ChoiceB", SqlDbType.VarChar).Value = txtb.Text;  
  11. cmd.Parameters.AddWithValue("@ChoiceC", SqlDbType.VarChar).Value = txtc.Text;  
  12. cmd.Parameters.AddWithValue("@ChoiceD", SqlDbType.VarChar).Value = txtd.Text;  
  13. cmd.Parameters.AddWithValue("@ChoiceE", SqlDbType.VarChar).Value = txtchkchoiceE.Text;  
  14. cmd.Parameters.AddWithValue("@ChoiceF", SqlDbType.VarChar).Value = txtchkchoiceF.Text;  
  15. cmd.Parameters.AddWithValue("@QPoints", SqlDbType.Int).Value = lblscore.Text;  
  16. cmd.Parameters.AddWithValue("@QType", SqlDbType.VarChar).Value = cmbqtype.SelectedItem;  
  17. cmd.Parameters.AddWithValue("@QComplexity", SqlDbType.VarChar).Value = cmbcomplexity.SelectedItem;  
  18. cmd.Parameters.AddWithValue("@AuthorizationCode", SqlDbType.Int).Value = txtauthorizationcode.Text;  
  19. cmd.Parameters.Add(new SqlParameter("@exhibit", images));  
  20. cmd.ExecuteNonQuery();  
  21. txtquestion.Clear();  
  22. txta.Clear();  
  23. txtb.Clear();  
  24. txtc.Clear();  
  25. txtd.Clear();  
  26. txtchkchoiceA.Clear();  
  27. txtchkchoiceB.Clear();  
  28. txtchkchoiceC.Clear();  
  29. txtchkchoiceD.Clear();  
  30. txtchkchoiceE.Clear();  
  31. txtchkchoiceF.Clear();  
  32. lblquestion.Text = "";  
  33. lblquestionno.Text = "";  
  34. lblscore.Text = "";  
  35. cmbqtype.SelectedItem = null;  
  36. cmbcomplexity.SelectedItem = null;  
  37. //MessageBox.Show("Data succefully updated");  
  38. //this.Hide();  
  39. //AdminPanel ap = new AdminPanel();  
  40. //ap.Show();  
  41. ////what next?  
  42. conn.Close();  
  43. }  
  44. catch (Exception ex)  
  45. {  
  46. MessageBox.Show("Error Occured" + ex);  
  47. }  

Answers (1)