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();
}
Marius VasilePosted Sep 23, 2023, 10:12 AM
The problem is with
On update it will get all red and hide the value
Marius VasilePosted Sep 23, 2023, 9:22 AM
Thank you for your replies,
Session["IdUserTest"] gives correct info everytime, I've checked that. on first load and after update
Count value should change after each update, actually it shows how many questions remains unanawered in a quiz. So it gives me the correct value but something happens here
countTest shows the correct value after update but
doesn't update the value
Ajay KumarPosted Sep 23, 2023, 8:16 AM
The issue you're experiencing, where the count displayed after updating a post is not being updated, is likely due to how the
Session["IdUserTest"]is being managed.In your
GetTestCountmethod, you retrieve theidUserTestfrom the session and then use it in your SQL query:int idUserTest = Convert.ToInt32(Session["IdUserTest"]);
In your
UpdateTestmethod, you don't explicitly update theSession["IdUserTest"]value after performing the update operation, which could lead to a mismatch between theidUserTestused in theGetTestCountmethod and the actual updated value.To fix this issue, make sure you update the
Session["IdUserTest"]value after performing the update operation in theUpdateTestmethod. You can do this by setting the session variable to the new value you want to use. For example:Session["IdUserTest"] = newIdUserTestValue;
Replace
newIdUserTestValuewith the actual value you want to use after the update. This way, when you callGetTestCountafter the update, it will use the updatedidUserTestvalue, and your count should be displayed correctly.Tahir AnsariPosted Sep 23, 2023, 8:08 AM
If your count procedure works correctly on the first load but doesn't display the updated count after performing an update procedure,
ViewState: In ASP.NET WebForms, controls' values are typically stored in ViewState, which is then used to restore their state after postbacks. Ensure that ViewState is enabled for the controls related to the count display (`txtCount` and `lblCount`). If ViewState is disabled or not functioning correctly, the count might not persist after an update.
Control ID : Make sure the IDs of your controls (`txtCount` and `lblCount`) have not changed between the initial load and postback. ASP.NET relies on control IDs to restore their values during postbacks.
Error Handling: Check if any unhandled exceptions are occurring during the postback process that might prevent the count from updating. Add appropriate error handling to catch and report any exceptions.
Debugging: Use debugging tools to inspect the values of variables and controls during postbacks to understand where the issue is occurring.
Event Handling: Double-check that the event handlers are correctly wired up to the controls and that the update procedure is correctly triggering the postback.