Marius Vasile

Marius Vasile

  • 554
  • 1.9k
  • 147.6k

procedure not loaded after post

Sep 23 2023 6:29 AM

I have a count procedure that should be updated after each post but it will display only on first load, after the update procedure it will show nothing. Why?

TestCount procedure

protected void GetTestCount()
{
    int countTest;
    int idUserTest = Convert.ToInt32(Session["IdUserTest"]);
    using (SqlConnection conn = new SqlConnection(connString))
    {
        string sqlQueryCount = "SELECT Count(*) FROM tblTestDetail WHERE ((Corect IS NULL OR Corect='') AND IdUserTest = @IdUserTest)";
        using (SqlCommand cmd = new SqlCommand(sqlQueryCount, conn))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@IdUserTest", SqlDbType.Int).Value = idUserTest;
            conn.Open();

            countTest = Convert.ToInt32(cmd.ExecuteScalar());
            if(countTest > 0)
            {
                txtCount.Text = countTest.ToString();
                lblCount.Attributes["class"] = lblCount.Attributes["class"].Replace("hidden", "").Trim();
            }
            else
            {
                lblCount.Attributes.Add("class", "hidden");
            }
        }
    }
}

Update procedure

protected void Update_Test(object sender, EventArgs e)
{
    UpdateTest();
}
protected void UpdateTest()
{
    int idUserTest = Convert.ToInt32(Session["IdUserTest"]);
    using (SqlConnection conn = new SqlConnection(connString))
    {
        try
        {
            int idIntrebare = Convert.ToInt32(txtIdIntrebare.Text.ToString());
            string sqlQuery = "UPDATE tblTestDetail SET RaspunsUser=@RaspunsUser, RaspunsCorect=@RaspunsCorect, Corect=@Corect WHERE IdIntrebare=@IdIntrebare AND IdUserTest = @IdUserTest";
            using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@IdUserTest", SqlDbType.Int).Value = idUserTest;
                cmd.Parameters.Add("@IdIntrebare", SqlDbType.Int).Value = idIntrebare;
                cmd.Parameters.Add("@RaspunsUser", SqlDbType.NVarChar).Value = inputRasUser.Text;
                cmd.Parameters.Add("@RaspunsCorect", SqlDbType.NVarChar).Value = inputRasCor.Text;
                cmd.Parameters.Add("@Corect", SqlDbType.NVarChar).Value = inputCor.Text;

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
        catch (Exception ex)
        {
            // Handle any exceptions that may occur during database interaction
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('An error occurred: " + ex.Message + "')", true);
        }
    }
    GetDataTest();
    GetTestDetail();
    GetTestCount();
}

 


Answers (4)