I do not know what seems the problem in my code. I want to add the data to my database (using windows form) but whenever I refresh my database, no updates were observed. Thank you for your help. Here is my code:
public void btnSave_Click(object sender, EventArgs e)
{
string question = txtQuestion.Text;
string choice1 = txtbChoice1.Text;
string choice2 = txtbChoice2.Text;
string choice3 = txtbChoice3.Text;
string choice4 = txtbChoice4.Text;
string answer = txtbAnswer.Text;
if (txtQuestion.Text.Trim().Length != 0 && txtbChoice1.Text.Trim().Length != 0 && txtbChoice2.Text.Trim().Length != 0 &&
txtbChoice3.Text.Trim().Length != 0 && txtbChoice4.Text.Trim().Length != 0 && txtbAnswer.Text.Trim().Length != 0 &&
(answer.Trim() == choice1.Trim() || answer.Trim() == choice2.Trim() || answer.Trim() == choice3.Trim() || answer.Trim() == choice4.Trim()))
{
//connection to database
Connection conn = new Connection();
conn.connect();
SqlCommand cmd = new SqlCommand("INSERT INTO Questions (Question, Choice1, Choice2, Choice3, Choice4, Correct_Answer) values (@Question, @Choice1, @Choice2, @Choice3, @Choice4, @Correct_Answer)", Connection.conn);
cmd.Parameters.AddWithValue("@Question ", question);
cmd.Parameters.AddWithValue("@Choice1", choice1);
cmd.Parameters.AddWithValue("@Choice2", choice2);
cmd.Parameters.AddWithValue("@Choice3", choice3);
cmd.Parameters.AddWithValue("@Choice4", choice4);
cmd.Parameters.AddWithValue("@Correct_Answer", answer);
DialogResult dialogResult = MessageBox.Show("Do you want to add another question?", "Question", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
this.Hide();
NewQuestions nNewQuestion = new NewQuestions();
nNewQuestion.Show();
}
else if (dialogResult == DialogResult.No)
{
this.Hide();
Options nOptions = new Options();
nOptions.Show();
}
conn.disconnect();
}
And here is my database (in case it is needed):
CREATE TABLE "Questions"
(
QuestionID int IDENTITY NOT NULL,
Question VARCHAR(500) NOT NULL,
Choice1 VARCHAR(50) NOT NULL,
Choice2 VARCHAR(50) NOT NULL,
Choice3 VARCHAR(50) NOT NULL,
Choice4 VARCHAR(50) NOT NULL,
Correct_Answer VARCHAR(50) NOT NULL,
PRIMARY KEY (questionID)
)