【解決方法】セッション状態が機能しない理由


管理者ページで追加ボタンをクリックすると、ユーザーページに各血液型の合計ユニット数が表示されるようにしたいです。 そこで、ここではセッション状態を使用して表示しましたが、それでも機能せず、エラーも表示されません。 Web 設定も変更しました。

私が試したこと:

This Button is from Gridview (Admin page)

protected void Button3_Click(object sender, EventArgs e)
{
    con.Open();

    //    string query1 = "SELECT SUM(Unit) AS TotalUnit,Blood_Group FROM TableDF GROUP BY Blood_Group ";

    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();
}
This is 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+_TotalUnit"] != null)
            {
                Label3.Text = " " + Session["A+_TotalUnit"].ToString();
            }
            if (Session["A-_TotalUnit"] != null)
            {
                Label4.Text = "" + Session["A-_TotalUnit"].ToString();
            }
            if (Session["B+_TotalUnit"] != null)
            {
                Label5.Text = "" + Session["B+_TotalUnit"].ToString();
            }
            if (Session["B-_TotalUnit"] != null)
            {
                Label6.Text = " " + Session["B-_TotalUnit"].ToString();
            }
            if (Session["O+_TotalUnit"] != null)
            {
                Label7.Text = " " + Session["O+_TotalUnit"].ToString();
            }
            if (Session["O-_TotalUnit"] != null)
            {
                Label8.Text = " " + Session["O-_TotalUnit"].ToString();
            }
            if (Session["AB+_TotalUnit"] != null)
            {
                Label9.Text = "" + Session["AB+_TotalUnit"].ToString();
            }
            if (Session["AB-_TotalUnit"] != null)
            {
                Label10.Text = "" + Session["AB-_TotalUnit"].ToString();
            }

            

        }

and Web.Config changess
<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」で終わりますが、「値の読み取り」コードは「1」で終わりません。

コメント

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