I can't find how to take the integer value from my primary key (called QuestionID
) and display it on a label.
I thought it might be a good idea to have this code in a timer because I want it to update every time a record (row) is added.
I would very much appreciate any help in any way as I'm a bit of a newbie! Thank you!
Here is my very pitiful attempt to doing it which I kind of gave up on as I don't know how to take the value from the database:
private void timer1_Tick(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
SqlConnection connect = new SqlConnection(connectionString);
connect.Open();
SqlCommand command6 = new SqlCommand("SELECT ([QuestionID]) FROM Questions", connect);
QuestionNum.Text =
}
The rest of my code is here:
private void button1_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
SqlConnection connect = new SqlConnection(connectionString);
connect.Open();
int checkedradiobutton = 0;
if(radioButton1.Checked)
{
checkedradiobutton = 1;
}
else if(radioButton2.Checked)
{
checkedradiobutton = 2;
}
else if(radioButton3.Checked)
{
checkedradiobutton = 3;
}
string QuestionText = QuestionBox.Text;
string AnswerText = teacheranswerbox.Text;
SqlCommand command5 = new SqlCommand(@"INSERT INTO Questions ([Actual answer], [Question Space], [Question Type]) VALUES (@AnswerText, @QuestionText, @checkedradiobutton)", connect);
command5.Parameters.AddWithValue("@AnswerText", AnswerText);
command5.Parameters.AddWithValue("@QuestionText", QuestionText);
command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton);
command5.ExecuteNonQuery();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
teacheranswerbox.Text = "You cannot store an answer for a long answer essay question";
teacheranswerbox.ReadOnly = true;
}
else
{
teacheranswerbox.ReadOnly = false;
teacheranswerbox.Text = "Enter the correct answer here";
}
}
The table I am trying to take the data from (p.s. the QuestionID
automatically increments):
CREATE TABLE [dbo].[Questions]
(
[QuestionID] INT IDENTITY (1, 1) NOT NULL,
[Actual answer] NVARCHAR (50) NULL,
[Question Space] NVARCHAR (50) NULL,
[Question Type] INT NULL,
PRIMARY KEY CLUSTERED ([QuestionID] ASC)
);