简体   繁体   中英

How to use Session variables asp.net c#

I cant seem to pass my variable through to the next page. Can anyone tell me what i am doing wrong?

var finalValue = value * sqlCouponValue;
finalValue = Math.Round(finalValue, 2);
Session["discountedOrderTotal"] = finalValue.ToString();

where i am trying to call it again on the next page:

e.Row.Cells[4].Text = "$" + Session["discountOrderTotal"];

Does anyone have any ideas? I have never used session variables before and am not sure why it is just returning a $ . Any help would be much appreciated. Thanks!

you have different names. discountedOrderTotal vs discountOrderTotal

The first thing you should be careful is the name of the session. It should be same and when you are retrieving the session then you need to specify the type because it returns an object. So try this when you call this to next page

e.Row.Cells[4].Text = "$" + Session["discountedOrderTotal"].ToString();
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Table ID="Table1" runat="server">
        <asp:TableRow>
            <asp:TableCell>Transfer Request ID</asp:TableCell>
                           <asp:TableCell>  <asp:Label ID="Label1" runat="server" Text='<%# Eval("TransferRequestId") %>'></asp:Label></asp:TableCell>
                           <asp:TableCell></asp:TableCell>
        </asp:TableRow>
        <asp:TableRow>
             <asp:TableCell>Employee Name</asp:TableCell>
                           <asp:TableCell>  <asp:Label ID="Label3" runat="server" Text='<%# Eval("EmployeeName") %>'></asp:Label></asp:TableCell>
                           <asp:TableCell></asp:TableCell>
        </asp:TableRow>

        <asp:TableRow>
            <asp:TableCell><asp:Button ID="Button1" runat="server" Value="APPROVE" Text="Approve" OnClick="Button1_Click"></asp:Button></asp:TableCell>

            <asp:TableCell><asp:Button ID="Button2" runat="server" value="REJECT" Text="Reject" Onclick="Button2_Click"></asp:Button></asp:TableCell>
        </asp:TableRow>
    </asp:Table>

</asp:Content>
public partial class Approve : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Convert.ToString(Session["id"]);
        Label3.Text = Convert.ToString(Session["name"]);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        int id1=Convert.ToInt32(Session["id"]);
        TransferDAL dalob = new TransferDAL();
        int x = dalob.updateapprove(id1);

        Response.Redirect("View.aspx");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int id1 = Convert.ToInt32(Session["id"]);
        TransferDAL dalob = new TransferDAL();
        int r = dalob.updatereject(id1);
        Response.Redirect("View.aspx");
    }
}
public partial class View : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            List<TransferBO> List2 = new List<TransferBO>();
            TransferDAL obj1 = new TransferDAL();
            List2 = obj1.view();
            Gridview1.DataSource = List2;
            Gridview1.DataBind();



        }

    }

    protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow SelectedRow = Gridview1.SelectedRow;
        Label id1 = (Label)SelectedRow.FindControl("Label1") as Label;
        int id2 = Convert.ToInt32(id1.Text);
        Label id3 = (Label)SelectedRow.FindControl("Label3") as Label;
        string id4 = Convert.ToString(id3.Text);
        Session["id"] = id2;
        Session["name"] = id4;
        Response.Redirect("Approve.aspx");



    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM