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.
- public EditQuestions()
- {
- InitializeComponent();
- }
- int index = 0;
- private void EditQuestions_Load(object sender, EventArgs e)
- {
- txtauthorizationcode.Text = EditQuizDash.authcode;
- try
- {
- conn.Open();
- var query = "select * FROM Questions WHERE AuthorizationCode='" + txtauthorizationcode.Text + "'";
- var cmd = new SqlCommand(query, conn);
- SqlDataReader dr = cmd.ExecuteReader();
- var sSingle = "Single Choice";
- var sMultiple = "Multiple Choice";
- var sTrueFalse = "TrueFalse";
- while (dr.Read())
- {
- index = 0;
- if (dr.GetString(9) == sSingle)
- {
- grpsingleanswers.Visible = true;
- grpmultipleanswers.Visible = false;
- txtquestion.Text = dr.GetString(2);
- cmbqtype.SelectedItem = dr.GetString(9);
- cmbcomplexity.SelectedItem = dr.GetString(10);
- lblscore.Text = dr.GetInt32(11).ToString();
- lblquestionlable.Text = "Question " + dr.GetInt32(0).ToString();
- }
- showData(index);
- 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();
- lblquestionlable.Text = "Question " + dr.GetInt32(0).ToString();
- }
- showData(index);
- if (dr.GetString(9) == sTrueFalse)
- {
- grpmultipleanswers.Visible = false;
- grpsingleanswers.Visible = false;
- grptruefalse.Visible = true;
- txtquestion.Text = dr.GetString(2);
- cmbqtype.SelectedItem = dr.GetString(9);
- cmbcomplexity.SelectedItem = dr.GetString(10);
- lblscore.Text = dr.GetInt32(11).ToString();
- lblquestionlable.Text = "Question " + dr.GetInt32(0).ToString();
- }
- showData(index);
- }
- conn.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error Occured" + ex);
- }
- }