لماذا لا تعمل حالة الجلسة


عندما أنقر فوق زر “إضافة” في صفحة الإدارة، أريد أن يعرض إجمالي رقم الوحدة لكل فصيلة دم على صفحة المستخدم. لقد استخدمت هنا حالة الجلسة لعرضها، لكنها لا تزال لا تعمل ولا تظهر أي خطأ. لقد قمت أيضًا بإجراء تغييرات على تكوين الويب.

ما حاولت:

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

        }

صفحة الإدارة

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

الحل 1

انظر إلى الكود الخاص بك:

Session[BloodGroup1 + "TotalUnit1"] = TotalUnit1;

و

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

أسماء الفهرس ليست متماثلة – كود “تعيين القيمة” ينتهي بالرقم “1”، بينما كود “اقرأ القيمة” لا ينتهي.

コメント

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