Joel Bwana

Joel Bwana

  • NA
  • 24
  • 1.7k

Loading Data from database

Sep 22 2020 5:57 PM
I am new to C#.
I have an sql client database. The database has related records(same authcode).This authcode is not my primary key. What i want to achieve is that when a specific form loads, I want to display the related records , one at atime. I will click next to move to the next record. At the moment, it goes to the last record!
 
Here is my code: NOTE I have omitted unnecessary parts of the code.
  1. public EditQuestions()  
  2. {  
  3. InitializeComponent();  
  4. }  
  5. int index = 0;  
  6. private void EditQuestions_Load(object sender, EventArgs e)  
  7. {  
  8. txtauthorizationcode.Text = EditQuizDash.authcode; //This basically transfer the authcode from another form  
  9. try  
  10. {  
  11. conn.Open();  
  12. var query = "select * FROM Questions WHERE AuthorizationCode='" + txtauthorizationcode.Text + "'";  
  13. var cmd = new SqlCommand(query, conn);  
  14. SqlDataReader dr = cmd.ExecuteReader();  
  15. var sSingle = "Single Choice";  
  16. var sMultiple = "Multiple Choice";  
  17. var sTrueFalse = "TrueFalse";  
  18. while (dr.Read())  
  19. {  
  20. index = 0;  
  21. if (dr.GetString(9) == sSingle)  
  22. {  
  23. grpsingleanswers.Visible = true;  
  24. grpmultipleanswers.Visible = false;  
  25. txtquestion.Text = dr.GetString(2);  
  26. cmbqtype.SelectedItem = dr.GetString(9);  
  27. cmbcomplexity.SelectedItem = dr.GetString(10);  
  28. lblscore.Text = dr.GetInt32(11).ToString();  
  29. lblquestionlable.Text = "Question " + dr.GetInt32(0).ToString();  
  30. }  
  31. showData(index);  
  32. if (dr.GetString(9) == sMultiple)  
  33. {  
  34. grpmultipleanswers.Visible = true;  
  35. grpsingleanswers.Visible = false;  
  36. txtquestion.Text = dr.GetString(2);  
  37. cmbqtype.SelectedItem = dr.GetString(9);  
  38. cmbcomplexity.SelectedItem = dr.GetString(10);  
  39. lblscore.Text = dr.GetInt32(11).ToString();  
  40. lblquestionlable.Text = "Question " + dr.GetInt32(0).ToString();  
  41. }  
  42. showData(index);  
  43. if (dr.GetString(9) == sTrueFalse)  
  44. {  
  45. grpmultipleanswers.Visible = false;  
  46. grpsingleanswers.Visible = false;  
  47. grptruefalse.Visible = true;  
  48. txtquestion.Text = dr.GetString(2);  
  49. cmbqtype.SelectedItem = dr.GetString(9);  
  50. cmbcomplexity.SelectedItem = dr.GetString(10);  
  51. lblscore.Text = dr.GetInt32(11).ToString();  
  52. lblquestionlable.Text = "Question " + dr.GetInt32(0).ToString();  
  53. }  
  54. showData(index);  
  55. }  
  56. conn.Close();  
  57. }  
  58. catch (Exception ex)  
  59. {  
  60. MessageBox.Show("Error Occured" + ex);  
  61. }  
  62. }  

Answers (1)