Click here to Skip to main content
15,883,911 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am passing some value from 1st Page to TheAdmin Page.
I can assign the value in Session["user"] perfectly .but when it redirects to that page null value passing.Please help me.Below is my code.

1stPage
C#
UserInfo loggedin = new UserInfo();
loggedin.UserID = 1;
loggedin.UserName = "Manas";
loggedin.SessionID = 111;
loggedin.ClientName = "Cl1";
loggedin.ClientID = 1;
Session["user"]=loggidin;
   
Response.Write("<script>");
Response.Write("var height = screen.height;");
Response.Write("var width = screen.width;");
Response.Write("var params='left=0,top=0,height=' + height +',width=' + width + ',resizable=yes,scrollbars=yes,status=yes';");

Response.Write("var win=window.open('TheoAdmin.aspx?rand=" + new System.Random().NextDouble().ToString() + "','" + objclientuser.UserName + "_Window" + new System.Random().Next().ToString() + "',params);");
Response.Write("win.focus();");

Response.Write("if(navigator.userAgent.indexOf('MSIE 6.0') !=-1){window.opener = window;window.close();}");
Response.Write("else{window.open('', '_self', '');window.close();}");
Response.Write("</script>");

The Admin Page
C#
<!DOCTYPE html>
<script runat="server">

    string clientWidth = "";
    string clientHeight = "";
    string browser = "";

    string xmlString = "";

    protected void Page_Load(object sender, EventArgs e)
    {

       
        UserInfo loggedin = (UserInfo)Session["user"];

        if (loggedin == null)
        {
            Response.Redirect("Logout.aspx");
        }
        else
        {
            
            clientWidth = "100%";
            clientHeight = "100%";

            xmlString += "<root>";
            xmlString += "<username>" + loggedin.UserName + "</username>";
            xmlString += "<userid>" + loggedin.UserID + "</userid>";
            xmlString += "<hosturl>" + ConfigurationManager.AppSettings["HostURL"] + "</hosturl>";
            xmlString += "<serviceurl>" + ConfigurationManager.AppSettings["ServiceURL"] + "</serviceurl>";
            xmlString += "<random>" + new System.Random().NextDouble().ToString() + "</random>";
            xmlString += "<version>" + ConfigurationManager.AppSettings["TAVersion"] + "</version>";
            xmlString += "<sessionid>" + loggedin.SessionID + "</sessionid>";
            xmlString += "<clientname>" + loggedin.ClientName + "</clientname>";
            xmlString += "<subscriptionid>" + HttpUtility.UrlEncode(loggedin.SubscriptionID) + "</subscriptionid>";
            //xmlString += "<auth>" + Request.Cookies["LOOKINGGLASSFORMSAUTH"].Value + "</auth>";
            xmlString += "<sess>" + Request.Cookies["ASP.NET_SessionId"].Value + "</sess>";
            xmlString += "<currentyear>" + System.DateTime.Now.Year.ToString() + "</currentyear>";
            xmlString += "</root>";
        }
    }
</script>
<html>
Posted
Updated 5-Aug-13 20:12pm
v2

I found a typo at first glance here.
UserInfo loggedin = new UserInfo();
.................................
Session["user"]=loggidin;//it should be loggedin 

I dont know whether it may be an answer to your question. Let me know if its not an issue.

Regards..:laugh:
 
Share this answer
 
v2
Comments
connect2manas 6-Aug-13 2:40am    
Hi leuva,
Thanks for your answer.
but in above loggedin contain null value.
idenizeni 6-Aug-13 12:18pm    
The loggedin variable would always be null because you mapped loggidin in your first page.
idenizeni 6-Aug-13 12:15pm    
I'm inclined to believe Rohan Leuva may be onto something. In your first page you create a variable called 'loggedin' and initialize it. But immediately after initializing you set your session object to be 'loggidin', which would be null as it's never been initialized. In your admin page the object would always be null because you never set your initialized object to the session variable in your first page.
connect2manas 6-Aug-13 13:24pm    
thanks for you answer.
adriancs 6-Aug-13 13:33pm    
Yes, what RLH68 mentioned is correct.
By creating a static object of (UserInfo)Session["user"] can avoid the typo error.
See my answer.
P.S: I don't understand why our posts were downvoted. At least, the downvoter should explain why. Or else the act is just an naughty spam act.
You can create a class like this:
C#
using System;
using System.Web;

namespace MyWeb
{
    public class AppSession
    {
        public static UserInfo LoginUser
        {
            get
            {
                if (HttpContext.Current.Session["userinfo"] == null)
                {
                    HttpContext.Current.Session["userinfo"] = new UserInfo();
                }
                return (UserInfo)HttpContext.Current.Session["userinfo"];
            }
            set
            {
                // Detect null value
                // Delete this IF ELSE checking if you want to allow a null value to be assigned
                if (value == null)
                {
                    throw new Exception("Value assigned to Session-LoginUser detected.");
                }

                HttpContext.Current.Session["userinfo"] = value;
            }
        }
    }
}

Then you can access it any where in your application:
C#
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AppSession.LoginUser = new UserInfo();
            AppSession.LoginUser.ClientID = 1;
            AppSession.LoginUser.SessionID = 1;
            AppSession.LoginUser.ClientName = "Cl1";
            AppSession.LoginUser.UserID = 1;
            AppSession.LoginUser.UserName = "Manas";
        }
    }
}


Then at the second page:
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (AppSession.LoginUser == null)
    {
        Response.Redirect("~/Logout.aspx");
    }
}


Now, go ahead and try again. Whenever there is a null value assigned to AppSession.LoginUser, an Exception will be thrown. From there you can trace where is the error occur in your code.

By the way, this block of code:
C#
Response.Write("<script>");
Response.Write("var height = screen.height;");
Response.Write("var width = screen.width;");
Response.Write("var params='left=0,top=0,height=' + height +',width=' + width + ',resizable=yes,scrollbars=yes,status=yes';");

Can be written as this:
C#
Response.Write(@"

<script type=""text/javascript"">
    var height = screen.height;
    var width = screen.width;
    var params='left=0,top=0,height=' + height +',width=' + width + ',resizable=yes,scrollbars=yes,status=yes';"
                
);

You won't have to repeat Response.Write() for each line.
If you want to escape a double quote symbol
"

you can escape it by typing it twice like this:
""


Another example of using @"":
This code:
C#
xmlString += "<root>";
xmlString += "<username>" + loggedin.UserName + "</username>";
xmlString += "<userid>" + loggedin.UserID + "</userid>";
xmlString += "<hosturl>" + ConfigurationManager.AppSettings["HostURL"] + "</hosturl>";
xmlString += "<serviceurl>" + ConfigurationManager.AppSettings["ServiceURL"] + "</serviceurl>";
xmlString += "<random>" + new System.Random().NextDouble().ToString() + "</random>";
xmlString += "<version>" + ConfigurationManager.AppSettings["TAVersion"] + "</version>";
xmlString += "<sessionid>" + loggedin.SessionID + "</sessionid>";
xmlString += "<clientname>" + loggedin.ClientName + "</clientname>";
xmlString += "<subscriptionid>" + HttpUtility.UrlEncode(loggedin.SubscriptionID) + "</subscriptionid>";
//xmlString += "<auth>" + Request.Cookies["LOOKINGGLASSFORMSAUTH"].Value + "</auth>";
xmlString += "<sess>" + Request.Cookies["ASP.NET_SessionId"].Value + "</sess>";
xmlString += "<currentyear>" + System.DateTime.Now.Year.ToString() + "</currentyear>";
xmlString += "</root>";

Can be written as:
C#
xmlString = @"
<root>
    <username>" + loggedin.UserName + @"</username>
    <userid>" + loggedin.UserID + @"</userid>
    <hosturl>" + ConfigurationManager.AppSettings["HostURL"] + @"</hosturl>
    <serviceurl>" + ConfigurationManager.AppSettings["ServiceURL"] + @"</serviceurl>
    <random>" + new System.Random().NextDouble().ToString() + @"</random>
    <version>" + ConfigurationManager.AppSettings["TAVersion"] + @"</version>
    <sessionid>" + loggedin.SessionID + @"</sessionid>
    <clientname>" + loggedin.ClientName + @"</clientname>
    <subscriptionid>" + HttpUtility.UrlEncode(loggedin.SubscriptionID) + @"</subscriptionid>
    <sess>" + Request.Cookies["ASP.NET_SessionId"].Value + @"</sess>
    <currentyear>" + System.DateTime.Now.Year.ToString() + @"</currentyear>
</root>";

You won't have to repeat "xmlString +=" on each line.
 
Share this answer
 
v8
Comments
Thanks7872 7-Aug-13 0:04am    
Hmmmm..This is a good answer. Needs to be up voted. I see no reason for down vote. Now a days people have just forgot the meaning of it.
session does not required i dont know this will help you or not
C#
string loggedin = convert.tostring( Session["user"]);

if (loggedin == "")
{
Response.Redirect("Logout.aspx");
}
else
{

clientWidth = "100%";
clientHeight = "100%";


if loggdin is null then you definately have another problems

regards......:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900