Por qué el estado de la sesión no funciona

programación


Cuando hago clic en el botón Agregar en la página de administración, quiero que muestre el número total de unidades de cada grupo sanguíneo en la página del usuario. Aquí utilicé el estado de la sesión para mostrarlo, pero aún así no funciona y no muestra ningún error. También hice cambios en la configuración web.

Lo que he probado:

<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();

        }

Página de administrador

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" />....

Solución 1

Mira tu código:

Session[BloodGroup1 + "TotalUnit1"] = TotalUnit1;

Y

if (Session["A+_TotalUnit"] != null)
{
    Label3.Text = " " + Session["A+_TotalUnit"].ToString();
}

Los nombres de los índices no son los mismos: el código “Establecer el valor” termina con un “1”, el código “Leer el valor” no.

コメント

タイトルとURLをコピーしました