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
- try
- {
- conn.Open();
- index = 0;
-
- var query = "select * FROM Questions";
- var cmd = new SqlCommand(query, conn);
- SqlDataReader dr = cmd.ExecuteReader();
- var sSingle = "Single Choice";
- var sMultiple = "Multiple Choice";
- while (dr.Read())
- {
- if (dr.GetString(9) == sSingle)
- {
- grpmultipleanswers.Visible = false;
- grpsingleanswers.Visible = true;
- txtquestion.Text = dr.GetString(2);
-
- txta.Text = dr.GetString(3);
- txtb.Text = dr.GetString(4);
- txtc.Text = dr.GetString(5);
- txtd.Text = dr.GetString(6);
- cmbqtype.SelectedItem = dr.GetString(9);
- cmbcomplexity.SelectedItem = dr.GetString(10);
- lblscore.Text = dr.GetInt32(11).ToString();
- lblquestionno.Text ="Question " + dr.GetInt32(0).ToString();
- }
- if (dr.GetString(9) == sMultiple)
- {
- grpmultipleanswers.Visible = true;
- grpsingleanswers.Visible = false;
- txtquestion.Text = dr.GetString(2);
- cmbqtype.SelectedItem = dr.GetString(9);
- cmbcomplexity.SelectedItem = dr.GetString(10);
- lblscore.Text = dr.GetInt32(11).ToString();
-
- lblquestionno.Text = "Question " + dr.GetInt32(0).ToString();
-
- txtchkchoiceA.Text = dr.GetString(3);
- txtchkchoiceB.Text = dr.GetString(4);
- txtchkchoiceC.Text = dr.GetString(5);
- txtchkchoiceD.Text = dr.GetString(6);
- txtchkchoiceE.Text = dr.GetString(7);
- txtchkchoiceF.Text = dr.GetString(8);
- }
- }
-
- btnview.Hide();
- btnupdate.Show();
- conn.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error Occured" + ex);
- }
Button Update
- try
- {
- SqlConnection conn = new SqlConnection(@"Data Source = (LocalDB)\MSSQLlocaldb; Initial Catalog = AdminAuthentication; Integrated Security = True");
- 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";
- var cmd = new SqlCommand(query, conn);
- conn.Open();
- cmd.Parameters.AddWithValue("@QID", SqlDbType.Int).Value = lblquestion.Text;
- cmd.Parameters.AddWithValue("@QText", SqlDbType.VarChar).Value = txtquestion.Text;
- cmd.Parameters.AddWithValue("@ChoiceA", SqlDbType.VarChar).Value = txta.Text;
- cmd.Parameters.AddWithValue("@ChoiceB", SqlDbType.VarChar).Value = txtb.Text;
- cmd.Parameters.AddWithValue("@ChoiceC", SqlDbType.VarChar).Value = txtc.Text;
- cmd.Parameters.AddWithValue("@ChoiceD", SqlDbType.VarChar).Value = txtd.Text;
- cmd.Parameters.AddWithValue("@ChoiceE", SqlDbType.VarChar).Value = txtchkchoiceE.Text;
- cmd.Parameters.AddWithValue("@ChoiceF", SqlDbType.VarChar).Value = txtchkchoiceF.Text;
- cmd.Parameters.AddWithValue("@QPoints", SqlDbType.Int).Value = lblscore.Text;
- cmd.Parameters.AddWithValue("@QType", SqlDbType.VarChar).Value = cmbqtype.SelectedItem;
- cmd.Parameters.AddWithValue("@QComplexity", SqlDbType.VarChar).Value = cmbcomplexity.SelectedItem;
- cmd.Parameters.AddWithValue("@AuthorizationCode", SqlDbType.Int).Value = txtauthorizationcode.Text;
- cmd.Parameters.Add(new SqlParameter("@exhibit", images));
- cmd.ExecuteNonQuery();
- txtquestion.Clear();
- txta.Clear();
- txtb.Clear();
- txtc.Clear();
- txtd.Clear();
- txtchkchoiceA.Clear();
- txtchkchoiceB.Clear();
- txtchkchoiceC.Clear();
- txtchkchoiceD.Clear();
- txtchkchoiceE.Clear();
- txtchkchoiceF.Clear();
- lblquestion.Text = "";
- lblquestionno.Text = "";
- lblscore.Text = "";
- cmbqtype.SelectedItem = null;
- cmbcomplexity.SelectedItem = null;
-
-
-
-
-
- conn.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error Occured" + ex);
- }