You must Sign In to post a response.
  • Category: ASP.NET

    Redirect to another page with session

    Dear Developers
    Am creating a project.

    I want if the user enter username and password it is redirect to next page which is Userhome.aspx. But When am giving the username and password it is redirect only on the same page that is Login.aspx. what is the error on my code please tell me

    This is my Login.aspx page.
    <%@ Page Title="" Language="C#" MasterPageFile="~/SiteMaster.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="_Default" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <script type="text/javascript">
    function Login() {
    if (document.getElementById("<%=txtname.ClientID%>").value == "") {
    alert("Enter Your User Name !");
    document.getElementById("<%=txtname.ClientID%>").focus();
    return false;
    }
    if (document.getElementById("<%=txtpassword.ClientID%>").value == "") {
    alert("Enter Your Password !");
    document.getElementById("<%=txtpassword.ClientID%>").focus();
    return false;
    }
    }
    </script>
    <div style="color: #ffd800; font-size: 25px;">
    <marquee>
    We have been recognised as the best student start up venture in the country by TATA</marquee>

    </div>
    <center>
    <div>

    <table class="tblhome">
    <tr>
    <td><link href="text.css" rel="stylesheet" />

    <span class="heading">Login Page</span>

    <%-- <span style="color:#ffd800;font-size:20px;">Login Page</span>--%>
    </td>
    </tr>

    </table>
    <table>
    <tr>
    <td><br /></td>
    </tr>
    <tr>
    <td style="color: white;font-size:20px;">UserName:</td>
    <td></td>
    </tr>
    <tr>
    <td>
    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
    </td>
    <td>*</td>
    </tr>
    <tr>
    <td style="color: white;font-size:20px;">Password:</td>
    <td></td>
    </tr>
    <tr>
    <td>
    <asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
    </td>
    <td>*</td>
    </tr>
    <tr>
    <td>
    <br />
    </td>
    </tr>
    <tr>
    <td colspan="4" style="font-size:15px;">
    <asp:Button ID="btnsubmit" runat="server" Text="Submit" BackColor="Black" Height="30px" Width="85px" ForeColor="White" OnClick="btnsubmit_Click" OnClientClick="return Login();" />
    <asp:Button ID="btncancel" runat="server" BackColor="Black" ForeColor="White" Height="30px" Width="85px" Text="Cancel" OnClick="btncancel_Click"/>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="lblalert" runat="server" ForeColor="Red"></asp:Label>
    </td>
    </tr>
    </table>
    </div>
    </asp:Content>

    This is Login.aspx.cs Page

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.OleDb;
    using System.Configuration;

    public partial class _Default : System.Web.UI.Page
    {
    public static string constr = ConfigurationManager.ConnectionStrings["constr"].ToString();
    AdminClass objadmin = new AdminClass();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
    OleDbConnection con = Connection.DBconnection();
    DataSet ds = new DataSet();
    string userlogin = "select * from tbl_Registration where Emailid='" + txtname.Text.Trim() + "' or Mobile='" + txtname.Text.Trim() + "' and Pwd='" + txtpassword.Text.Trim() + "'";
    OleDbDataAdapter oda = new OleDbDataAdapter(userlogin, con);
    oda.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {
    Session["uid"] = txtname.Text.Trim();
    Response.Redirect("User/UserHome.aspx");
    }
    else if(Session["uid"]!= txtname.Text.Trim())
    {
    lblalert.Text = "Incorrect user name or Password";
    }
    }
    protected void btncancel_Click(object sender, EventArgs e)
    {
    txtname.Text = null;
    txtpassword.Text = null;
    }
    }

    And this is my next page UserHome.aspx.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class User_UserHome : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (Session["uid"] == null)
    {
    Response.Redirect("../Login.aspx");
    }

    }

    }

    Thanks with
    Paul.S
  • #762047

    Hi,

    I request you to please put a break point and check line by line.

    As per my guess the problem is
    1) When you click submit button if page is not refreshing the problem is validation, ( check the Login function may be it's return false).

    2) If page gets refresh then also you are facing this problem, I request you to please check the session object, may be it's gets expire because of that only this issue is arises.


    Hope this will helpful to you..


    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762050
    Hi

    Paul

    Check this line

    Response.Redirect("../Login.aspx");

    your login.aspx placed where? Contains some folder? i saw your source i think this is working but you can check login.aspx placed in right place?
    check it once.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762051
    Page is refreshing but redirect on the same page which is Login.aspx

  • #762052
    Hi

    Paul

    Check this line

    Response.Redirect("../Login.aspx");

    your login.aspx placed where? Contains some folder? i saw your source i think this is working but you can check login.aspx placed in right place?
    check it once.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762058

    Hello Paul,

    Update your code with this code :

    protected void Page_Load(object sender, EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    if (Session["uid"] == null)
    {
    Response.Redirect("~/Login.aspx");
    }
    }

    Still if it is not working then put breakpoint in your code and check whether your values is getting stored in session or not.


    Hope this will work for you.
    Still if you are facing the problem let me know.


    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762067

    Hi

    It works for me


    protected void Button1_Click(object sender, EventArgs e)
    {
    OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E://srid documents/mydocsssssss/products.accdb;User ID=admin");
    DataSet ds = new DataSet();
    string userlogin = "select * from newusers where Email='" + TextBox1.Text + "'" ;
    OleDbDataAdapter oda = new OleDbDataAdapter(userlogin, con);
    oda.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {
    Session["uid"] = TextBox1.Text;
    Response.Redirect("Home.aspx");
    }
    else
    {
    Label2.Text = "Incorrect user name or Password";
    }
    }


    in home.aspx page

    protected void Page_Load(object sender, EventArgs e)
    {
    if (Session["uid"] == null)
    {
    Response.Redirect("Login.aspx");
    }



    Regards

    Sridhar Thota.


    Sridhar Thota.
    Editor: DNS Forum.


Sign In to post your comments