Pourquoi l’état de la session ne fonctionne pas

la programmation


Lorsque je clique sur le bouton Ajouter sur la page d’administration, je souhaite qu’il affiche le numéro d’unité total de chaque groupe sanguin sur la page utilisateur. Donc ici, j’ai utilisé l’état de la session pour l’afficher, mais cela ne fonctionne toujours pas et n’affiche aucune erreur. J’ai également apporté des modifications à la configuration Web.

Ce que j’ai essayé :

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

        }

Page d’administration

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

Solution 1

Regarde ton code :

Session[BloodGroup1 + "TotalUnit1"] = TotalUnit1;

Et

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

Les noms d’index ne sont pas les mêmes – le code “Définir la valeur” se termine par un “1”, pas le code “Lire la valeur”.

コメント

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