Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When I click the Add Button on the admin page, I want it to display the total unit number of each bloodgroup on the user page. So here, I used session state to display it, but still, it's not working and is not showing any error. I also made changes to the web configuration. 

What I have tried:

<pre>     User page

   protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Label3.Visible = true;
                Label4.Visible = true;
                Label5.Visible = true;
                Label6.Visible = true;
                Label7.Visible = true;
                Label8.Visible = true;
                Label9.Visible = true;
                Label10.Visible = true;

                if (Session["A+_TotalUnit1"] != null)
                {
                    Label11.Text = " " + Session["A+_TotalUnit1"].ToString();
                }
                if (Session["A-_TotalUnit1"] != null)
                {
                    Label12.Text = "" + Session["A-_TotalUnit1"].ToString();
                }
                if (Session["B+_TotalUnit1"] != null)
                {
                    Label13.Text = "" + Session["B+_TotalUnit1"].ToString();
                }
                if (Session["B-_TotalUnit1"] != null)
                {
                    Label14.Text = " " + Session["B-_TotalUnit1"].ToString();
                }
                if (Session["O+_TotalUnit1"] != null)
                {
                    Label15.Text = " " + Session["O+_TotalUnit1"].ToString();
                }
                if (Session["O-_TotalUnit1"] != null)
                {
                    Label8.Text = " " + Session["O-_TotalUnit1"].ToString();
                }
                if (Session["AB+_TotalUnit1"] != null)
                {
                    Label9.Text = "" + Session["AB+_TotalUnit1"].ToString();
                }
                if (Session["AB-_TotalUnit1"] != null)
                {
                    Label10.Text = "" + Session["AB-_TotalUnit1"].ToString();
                }
            }


            string query = "SELECT B_grpD, SUM(Unit_D) AS TotalUnits FROM BloodTable GROUP BY B_grpD";
            SqlCommand com1 = new SqlCommand(query, con);
            con.Open();


            SqlDataReader reader = com1.ExecuteReader();

            while (reader.Read())
            {
                string bloodGroup = reader["B_grpD"].ToString();
                int totalUnits = Convert.ToInt32(reader["TotalUnits"]);

                if (bloodGroup == "A+")
                {
                    Label3.Text = " " + totalUnits;
                }
                else if (bloodGroup == "A-")
                {
                    Label4.Text = " " + totalUnits;
                }
                else if (bloodGroup == "B+")
                {
                    Label5.Text = " " + totalUnits;
                }
                else if (bloodGroup == "B-")
                {
                    Label6.Text = " " + totalUnits;
                }
                else if (bloodGroup == "O+")
                {
                    Label7.Text = "" + totalUnits;
                }
                else if (bloodGroup == "O-")
                {
                    Label8.Text = " " + totalUnits;
                }
                else if (bloodGroup == "AB+")
                {
                    Label9.Text = " " + totalUnits;
                }
                else if (bloodGroup == "AB-")
                {
                    Label10.Text = " " + totalUnits;
                }
                // Add similar conditions for other blood groups

            }
            reader.Close();
            con.Close();

        }


Admin page
protected void Button3_Click(object sender, EventArgs e)
{


    //    string query1 = "SELECT SUM(Unit) AS TotalUnit,Blood_Group FROM TableDF GROUP BY Blood_Group ";
    try
    {
        con.Open();
        SqlCommand com2 = new SqlCommand("SELECT SUM(Unit) AS TotalUnit1,Blood_Group FROM TableDF GROUP BY Blood_Group ", con);



        SqlDataReader reader = com2.ExecuteReader();

        while (reader.Read())
        {
            string BloodGroup1 = reader["Blood_Group"].ToString();
            int TotalUnit1 = Convert.ToInt32(reader["TotalUnit1"]);

            Session[BloodGroup1 + "TotalUnit1"] = TotalUnit1;

        }
        reader.Close();
        con.Close();
    }
    catch (Exception ex)
    {
        // log this exception or throw it up the StackTrace
        // we do not need a finally-block to close the connection since it will be closed implicitly in an using-statement
        Response.Write("Email sent to " + ex);
    }
}

Web.config

<system.web>
 <sessionState mode="InProc" cookieless="false" timeout="20" />....
Posted
Updated 24-Feb-24 1:27am
v3

1 solution

Look at your code:
Session[BloodGroup1 + "TotalUnit1"] = TotalUnit1;
And
if (Session["A+_TotalUnit"] != null)
{
    Label3.Text = " " + Session["A+_TotalUnit"].ToString();
}
The index names aren't the same - the "Set the value" code ends with a "1", the "Read the value" code doesn't.
 
Share this answer
 
Comments
OriginalGriff 24-Feb-24 6:55am    
"It's not working" is one of the most useless problem descriptions we get: it tells us absolutely nothing about the problem. We don't know if you get an error message, or the wrong data, or even that that code compiles successfully!
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So tell us what happens when you run that code, what you expected to happen, how you checked what happened. Tell us what the debugger showed you was going on. Show us you modified code. Help us to help you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900