protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
if (string.IsNullOrEmpty(Session["LoginName"].ToString())) ->>> IM GETTING ERROR HERE ' Object reference not set to an instance of an object ,wat should i do :(,im redirecting user from masterpage to the other page using response.redirect in button
click...
developer27
Member
396 Points
205 Posts
if user not logged in redirect to login page
Mar 04, 2012 08:57 AM|LINK
Hiii ...
Please Check i need if user not logged in redirect to login page
below is my login page click event
public void Login_OnClick(object sender, EventArgs args)
{
ConnectionStringSettingsCollection cssc = ConfigurationManager.ConnectionStrings;
String _ConnString = cssc["GuardsConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(_ConnString);
conn.Open();
SqlCommand myCommand = new SqlCommand("SELECT * FROM [users] WHERE (userName ='" + UsernameTextbox.Text
+ "') AND (password = '" + PasswordTextbox.Text + "')", conn);
SqlDataReader dr = myCommand.ExecuteReader();
if (dr.Read())
{
Session["LoginName"] = UsernameTextbox.Text;
Response.Redirect("~/home.aspx");
}
else
{
Page.RegisterStartupScript("aa", "<script>alert('test')</script>");
}
}
Search Employe PAge....
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
if (string.IsNullOrEmpty(Session["LoginName"].ToString())) ->>> IM GETTING ERROR HERE ' Object reference not set to an instance of an object ,wat should i do :(,im redirecting user from masterpage to the other page using response.redirect in button click...
{
Response.Redirect("~/login.aspx");
}
}
}
sreejukg
All-Star
27495 Points
4095 Posts
Re: if user not logged in redirect to login page
Mar 04, 2012 09:11 AM|LINK
Session["LoginName"].ToString() will throw error when Session["LoginName"] is null.
so first check whether Session["LoginName"] is null
if(Session["LoginName"] != null)
{
if (string.IsNullOrEmpty(Session["LoginName"].ToString()))
{
///.... Redirect....
}
}
For login scenarios, it is better to use form authentication where the framework will handle the login and redirection...
http://msdn.microsoft.com/en-us/library/ff647070.aspx
My Blog
ashish-1983
Contributor
4879 Points
1257 Posts
Re: if user not logged in redirect to login page
Mar 04, 2012 09:42 AM|LINK
as per your current scenarion you check if seesion is NULL then redirect to Login Page else go to home page.
This part can be controlled globally by tweaking global.asax or create your custom httpmodule.
hope that helps
Gridview
Jquery
Asp.net
Fun !