简体   繁体   中英

Update SQL query, unsure why it isn't working as no errors are appearing

I have been staring at this UPDATE statement for a long while and are unsure why my table isn't changing. When I press the button no error appears but my table doesn't not get updated either, I have checked that all of my variables have values on debug and they do.

I'd appreciate any help anyone can give me!

This is the code that contains the statement I need help with:

private void button1_Click(object sender, EventArgs e)
    {

        string studentanswertext = textBox1.Text;
        string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
        string y = GlobalVariableClass.Signedinteacher;
        Convert.ToInt32(y);
        MessageBox.Show(y);

        MessageBox.Show(Convert.ToString(CurrentQuestionID));
        MessageBox.Show(studentanswertext);
        SqlConnection connect = new SqlConnection(connectionString);
        connect.Open();

        SqlCommand command20 = new SqlCommand(@"UPDATE QuestionStudentAssociation SET ([StudentAnswer]=@StudentAnswertext) WHERE ([QuestionID]=@CurrentQID AND [StudentID]=@SignedinStudent )", connect);
        command20.Parameters.AddWithValue("@StudentAnswertext", studentanswertext);
        command20.Parameters.AddWithValue("@CurrentQID", CurrentQuestionID);
        command20.Parameters.AddWithValue("@SignedinStudent", y);
        command20.BeginExecuteNonQuery();


        connect.Close();
    }

This is the whole code for my form if anyone wanted to look at it just in case that is affecting the button even handler:

 namespace ComputingA2_Official_Project
{
public partial class CurrentlySetTestForm : Form

{
    Timer loopTimer = new Timer();
    private int CurrentQuestionID { get; set; }
    private string QuestionSpace { get; set; }
    public CurrentlySetTestForm()
    {
        InitializeComponent();
    }

    private void CurrentlySetTestForm_Load(object sender, EventArgs e)
    {

        string y = GlobalVariableClass.Signedinteacher;

        Convert.ToInt32(y);

        string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
        SqlConnection connect = new SqlConnection(connectionString);

        connect.Open();

        SqlCommand command18 = new SqlCommand("SELECT MIN([QuestionID]) AS QuestionID FROM QuestionStudentAssociation WHERE ( [StudentID]=@Signedinstudent AND [StudentAnswer] IS NULL )", connect);
        command18.Parameters.AddWithValue("@Signedinstudent", y);

        var reader = command18.ExecuteReader();
        while (reader.Read())
        {
            CurrentQuestionID = Convert.ToInt32(reader[0]);

            SqlCommand command19 = new SqlCommand("SELECT ([Question Space]) FROM Questions WHERE ([QuestionID]=@CurrentQID)", connect);
            command19.Parameters.AddWithValue("@CurrentQID", CurrentQuestionID);


            using (SqlDataReader reader2 = command19.ExecuteReader())
            {
                while (reader2.Read())
                {
                    QuestionSpace = Convert.ToString(reader2[0]);
                    label1.Text = QuestionSpace;
                }
            }
         }

        connect.Close();

    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        string studentanswertext = textBox1.Text;
        string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
        string y = GlobalVariableClass.Signedinteacher;
        Convert.ToInt32(y);
        MessageBox.Show(y);

        MessageBox.Show(Convert.ToString(CurrentQuestionID));
        MessageBox.Show(studentanswertext);
        SqlConnection connect = new SqlConnection(connectionString);
        connect.Open();

        SqlCommand command20 = new SqlCommand(@"UPDATE QuestionStudentAssociation SET ([StudentAnswer]=@StudentAnswertext) WHERE ([QuestionID]=@CurrentQID AND [StudentID]=@SignedinStudent )", connect);
        command20.Parameters.AddWithValue("@StudentAnswertext", studentanswertext);
        command20.Parameters.AddWithValue("@CurrentQID", CurrentQuestionID);
        command20.Parameters.AddWithValue("@SignedinStudent", y);
        command20.BeginExecuteNonQuery();


        connect.Close();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {



    }
}

}

I believe the issue is that you are executing the command asynchronously (BeginExecuteNonQuery), but never calling EndExecuteNonQuery to commit it. I also suspect you could just call it synchronously like this:

command20.ExecuteNonQuery();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM