Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Design a Simple Application using which a User can Login, Surf and Logout from the application using ASP .NET

Here we have designed a application using the ASP .NET technology. In this application, a user can login with the appropriate credentials and after successful login the user can surf on the application for as many as time the user wants and then the user can also logout from the application.

Code - 
WebForm1.aspx - 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Ex9.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <center>
                <h1>Login User</h1>
                <hr />
                <fieldset>

            <table>
                <tr>
                    <td>UserName :</td>
                    <td><asp:TextBox ID="TxtUsername" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><asp:TextBox ID="Txtpwd" runat="server"></asp:TextBox></td>
                 
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="ButLogin" runat="server" Text="Login" OnClick="ButLogin_Click" /></td>
                    <td>
                        <asp:Literal ID="LitMsg" runat="server"></asp:Literal></td>
                    
                </tr>
            </table>
                    </fieldset>
                </center>
        </div>
    </form>
</body>
</html>


WebForm1.aspx.cs - 

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.SqlClient;

using System.Configuration;

 

namespace Ex9

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void ButLogin_Click(object sender, EventArgs e)

        {

            string maniconn = ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString;

           SqlConnection sqlconn = new SqlConnection(maniconn);

            SqlCommand sqlcomm = new SqlCommand("select * from [dbo].[Table] where Email=@Email and Password=@Password", sqlconn);

            sqlcomm.Parameters.AddWithValue("Email", TxtUsername.Text);

            sqlcomm.Parameters.AddWithValue("Password", Txtpwd.Text);

            SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);

            DataTable dt = new DataTable();

            sda.Fill(dt);

            sqlconn.Open();

            sqlcomm.ExecuteNonQuery();

            sqlconn.Close();

            if(dt.Rows.Count>0)

            {

                Session["id"] = TxtUsername.Text;

                Response.Redirect("Welcome.aspx");

                Session.RemoveAll();

            }

            else

            {

                LitMsg.Text = "Username & Password are wrong !";

            }

 

        }

    }

}


Web.config - 

<?xml version="1.0" encoding="utf-8"?>

<!--

  For more information on how to configure your ASP.NET application, please visit

  https://go.microsoft.com/fwlink/?LinkId=169433

  -->

<configuration>

            <connectionStrings>

                        <add name="Myconnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DHANSHRI\Documents\UserLogin.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient"/>

            </connectionStrings>

  <system.web>

    <compilation debug="true" targetFramework="4.7.2" />

    <httpRuntime targetFramework="4.7.2" />

  </system.web>

  <system.codedom>

    <compilers>

      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />

      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />

    </compilers>

  </system.codedom>

</configuration>

**Important Note :- Ignore the code inside the <compiler></compiler> tag it will be autogenerated in the Web.config file. The only change you have to do in this code is, you have to add your Connection String  of the database where the user data will be added and stored, without adding your own connection string this code will not run. 

How to get the connection String - 

  1. Open the Visual Studio IDE and then go to View > Server Explorer. 
  2. The Server Explorer will appear to the left side panel in Visual Studio IDE.
  3. If you don’t have your own database connected, you can click on “Connect to Database”.
  4. Select your Data Source Name, which is (Microsoft SQL Server Database File (SqlClient)), then add a name to your database and click on OK. 
  5. Now you have successfully created and connected to  your own database.
  6. Now under the Server Explorer panel, you will be able to see your database name, then Right-Click on it.
  7. Click on Properties, inside the properties you will be able to see your database Connection String copy it and paste it inside the <connectionString></connectionString> tag in the (add name = " ") tag.
  8. That's it.
Welcome.aspx - 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="Ex9.Welcome" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <h1>Welcome User!<asp:Label ID="Labusrname" runat="server"></asp:Label></h1><br />

            <asp:LinkButton ID="ButLogout" runat="server" OnClick="ButLogout_Click">Logout</asp:LinkButton>

        </div>

    </form>

</body>

</html>


Welcome.aspx.cs - 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace Ex9

{

    public partial class Welcome : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if(Session["id"]!=null)

            {

                Labusrname.Text = Session["id"].ToString();

 

            }

            else

            {

                Response.Redirect("WebForm1.aspx");

            }

        }

 

        protected void ButLogout_Click(object sender, EventArgs e)

        {

            Session.RemoveAll();

            Session.Abandon();

            Response.Redirect("WebForm1.aspx");

 

        }

    }

}

**Remember:- You have to add the data (means Username and Password) in the database manually then login in the application.


Output - 

Login page - 


Entering the Username and Password which is in the Database(Username: manthan, Password: 123456) -


After successful Login –

If the username and password is wrong the following error will be generated –


After clicking on the logout button the user will be logged out and redirected to the login page –


If you have any queries feel free to comment. Happy Coding:)

Post a Comment

0 Comments

Ad Code

Responsive Advertisement