Hi
I am getting above error on below line
C#
Session["dupRecords"] = "";
Session["totRecords"] = "";
Session["dupRecords"] = ((int?)Session["dupRecords"] ?? 0) + 1;Thanks
Hi
I am getting above error on below line
Session["dupRecords"] = "";
Session["totRecords"] = "";
Session["dupRecords"] = ((int?)Session["dupRecords"] ?? 0) + 1;Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 24, 2023, 12:18 PM EDT
To resol ve thiserror find below the updates code
// Retrieve the value from session and cast it to int int dupRecords = (int)(Session["dupRecords"] ?? 0); int totRecords = (int)(Session["totRecords"] ?? 0); // Increment the values dupRecords++; totRecords++; // Store the updated values back in session Session["dupRecords"] = dupRecords; Session["totRecords"] = totRecords;In the updated code, i cast the retrieved values from
Session["dupRecords"]andSession["totRecords"]tointusing(int)(Session["..."] ?? 0). The(int?)part is not required since we are explicitly assigning a default value of 0 using the null coalescing operator (??) if the session variable is null or does not exist.After incrementing the values, we assign the updated values back to the session variables
Session["dupRecords"]andSession["totRecords"].By explicitly casting the values to
int, you can perform arithmetic operations on them without encountering any errors.