1

I'm a complete stranger to ASP.NET. But, I've had a project to do using it & faced a problem.

It is :

  1. I have a login.aspx File - Where Users provide login User name & Password

  2. If Login details (match Data Base) OK then User automatically redirects to logged_in.aspx.

  3. There's a label (lbl_show) in redirected logged_in.aspx.

  4. I need to show Logged in Username in it.

I read bunch of articles & came with nothing because of my lack of understanding so please help me.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Harindra Singh
  • 371
  • 2
  • 6
  • 17

3 Answers3

0

se session variables in order to pass any value from one page to another.

Assign the Username value to the session variable and use it in your logged_in page as follows:

// In login page
Session["UserName"] = txtUserName.text;

//In logged_in page
label1.text = Session["UserName"];

Also refer the following link for State Management:

http://www.codeproject.com/Articles/492397/State-Management-in-ASP-NET-Introduction

Naren
  • 1,300
  • 14
  • 32
  • Thank You for the Reply Naren. But I get an error like this; Error 1 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) – Harindra Singh Mar 08 '13 at 04:47
  • try this: label1.text = Session["UserName"].ToString(); – Naren Mar 08 '13 at 04:54
  • @HarinLakmal Be aware that Sessions expire and that the value you expect to be there may not be there at all. Persistent cookie is much more reliable method to manage authentication. You will run into issues with this in the long run. – MikeSmithDev Mar 08 '13 at 05:12
0

You need to set an Authentication Cookie. It's easy and will allow you to leverage ASP.NET functionality easily (many built-in controls and also user-access control). I detail how in this SO post:

Using cookies to auto-login a user in asp.net (custom login)

Community
  • 1
  • 1
MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
0

The problem with the code

// In login page
Session["UserName"] = txtUserName.text;

//In logged_in page
label1.text = Session["UserName"];

Is casting is missing it should be

label1.text = Session["UserName"].ToString();

Edit 1

As Session contains object and if you have something other than object then you will have to explicitly cast it in your require type.
Suppose you have array in you Session then you will have to cast it back to array.

String[] Names={"abc","def","ghi"};
Session["NamesCol"]=Names;

Then if you want to use it you will have to cast it as follow

String[] NewNames=(string[])Session["NamesCol"];
शेखर
  • 17,412
  • 13
  • 61
  • 117