1

I read How to convert query result to list. But it not work or I don't know how to use it in my case.

My scenario is I have login page when user write email and password, application fetch data based on email and password.

protected void lbtnSignIn_Click(object sender, EventArgs e)
{
    try
    {
         SiteDataContext dc = new SiteDataContext();
         Session["UserData"] = from data in dc.Users
                                  where data.Email.Equals(txtEmail.Text.ToString()) &&
                                      data.Password.Equals(txtPassword.Text.ToString())
                                  select data; 
        Response.Redirect("SignIn.aspx");
    }
    catch(Exception exp)
    {
         lblMessage.Visible = true;
         lblMessage.Text = exp.Message;
    }
}

I save LINQ query result in Session["UserData"] and redirect to another page. On second page I want to get session result and show user data on page.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        var data = Session["UserData"];
        lblName.Text = data.Name;
    }
}

I can't get data from object data. It gives me below error

Object does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'object' could be found (are you missing a directive or an assembly reference?)

Simply I want to show data of login user on another webpage. Please help.

Community
  • 1
  • 1
Salman Mushtaq
  • 341
  • 4
  • 23

2 Answers2

2

The things you store in Session are typed as Object. You need to help the compiler by telling it what type you're expecting by casting the list to the correct type. The linq query is also returning a list of users so calling .Name on the list makes no sense. Assuming that dc.Users contains a list of objects called User You retreive the list by using the following.

var data = (IEnumerable<User>)Session["UserData"];

data could then be iterated over using

foreach(var u in data) System.Console.WriteLine(u.Name);

However, looking at the code i guess that what you're actually trying to do is more like.

You want to store the FIRST user you find in the session variable. Then you need to change the linq code to.

Session["UserData"] = (from data in dc.Users
                                  where data.Email.Equals(txtEmail.Text.ToString()) &&
                                  data.Password.Equals(txtPassword.Text.ToString())
                                  select data).FirstOrDefault(); 

And the retreive it from session and write the name to the label using

var data = (User)Session["UserData"];
lblName.Text = data.Name;
fhogberg
  • 415
  • 3
  • 11
1

You should execute the query before storing into the session. Consider using HttpContext.Items instead. Items will last for ONE request. Bloating the session is not good.

You can execute the query by using .ToList() or maybe even .FirstOrDefault() depends on the results.

Session["UserData"] = (from data in dc.Users
                       where data.Email.Equals(txtEmail.Text.ToString()) &&
                           data.Password.Equals(txtPassword.Text.ToString())
                       select data).FirstOrDefault(); 

Please surround your context in a using block so your context gets disposed after use.

using(var dc = new SiteDataContext()) {
    Session["UserData"] = (from data in dc.Users
                           where data.Email.Equals(txtEmail.Text.ToString()) &&
                               data.Password.Equals(txtPassword.Text.ToString())
                           select data).FirstOrDefault(); 
}
l1e3e3t7
  • 124
  • 11