views:

270

answers:

2

Hi im new to asp.net. . . im doing my final year project.How to use session variables in application using VC# and I also need to check with data base?

A: 

A session variable you can treat like an array of key value pairs. For example

Session["key"] = "Hello World";
string tmp = (String)Session["key"];

That is it. Pretty simple.

Edit: From feedback

public class User 
{
    public string firstname;
    public string lastname;
}

if (DoesUserExist(username, password)
{
    User newUser = new User();
    newUser.firstname = "Joe";
    newUser.lastname = "Soap";
    Session["LoggedInUser"] = newUser;
}

Then on other pages

User currentUser = Session["LoggedInUser"];
if (currentUser == null) 
{
    Response.Redirect("login.aspx");
}

This code is no where near perfect but you get the idea.

uriDium
i just need to store the informatiom of the current user and to check whether the user already exist in database or not –
shalu
A: 

I might be a little confused, but why do you want to use Session in a Windows Forms application? If it's for credentials, you can store credentials in an class exposed to your entire application. That is, if you are referring to "Windows Forms" and not "Web Forms".

PieterG
i just need to store the informatiom of the current user and to check whether the user already exist in database or not
shalu