Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.32/5 (8 votes)
See more:
i am getting exception with condition checking at page load when the user who is not logged in try to access user's home page


because the session of that user does not exists so it always catch exception..

what should be the right way to check session condition at page load??


here is the code below..


C#
try
        {
            if (Session["user_type"].ToString() == "User")
            {
                if (!IsPostBack)
                {
                    prpobj.userid = Convert.ToInt32(Session["user_id"]);
                    DataTable dtbl = mainobj.viewprofile(prpobj);
                    if (dtbl.Rows[0]["image_thumbpath"].ToString() != string.Empty)
                    {
                        Image.ImageUrl = dtbl.Rows[0]["image_thumbpath"].ToString();
                    }
                    else
                    {
                        Image.ImageUrl = "~/Images/img_default.jpg";
                    }
                    lblusername.Text = dtbl.Rows[0]["first_name"].ToString() + " " + dtbl.Rows[0]["last_name"].ToString();
                }
            }
            else if (Session["user_type"].ToString() == "Admin")
            {
                Response.Redirect("~/Access_Denied1.aspx");
            }
            else
            {
                Response.Redirect("~/Signin.aspx");
            }
        }
        catch (Exception)
        {
            Response.Redirect("~/Signin.aspx");
        }


Note - this guy has edited his question several times to replace it with abuse towards me, because I gave him the right answer. Go figure. I guess if you want to be abused too, you could try to repeat my answer, given that it's the only correct one, and see if he treats you the same.
Posted
Updated 11-Feb-10 19:16pm
v15

steeve_richard wrote:
if (Session["user_type"].ToString() == "User")


This is broken on so many levels. You should buy a basic C# book and read it, because the very basics appear to be lost on you.

if (Session["user_type"] != null


is what you need to check to make sure it's not null.

The other thing is, you should use a class that has static public strings to replace typing user_type over and over. What if someone puts User_Type in somewhere, or usre_type, or some other typo ? The code will compile but behave unpredictable. Personally, I'd make the user types an enum, not a set of strings, which also causes them to be strongly typed and not random.
 
Share this answer
 
I deleted your post, do NOT post fake 'answers' edit your post if you have something useful to say. In this case, you did not. You said:

VB
i do not want to buy a book...  
why do not you tell me?


Well, if you don't want to buy a book, then you sure as hell should not be working on any commercial projects, if you're writing a secure login system just for fun, you should still buy a book, or at least do some reading.

Nevertheless, I DID tell you, I told you exactly what is wrong, gave you code you could copy and paste, and gave you some other general advice as to why your code is terrible as is. How could you miss it ? This is why you need a book, you don't know enough to know the answer when it's right in front of you.
 
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