Create Login Form With SQL Database In ASP.NET Web Forms | ASP.NET

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Configuration;

using System.Data.SqlClient;


namespace LOGIN_FORM_ASP

{

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

    {

        string cs = ConfigurationManager.ConnectionStrings["dbcs2"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)

        {


        }


        protected void LoginButton_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection(cs);

            string query = "select * from login where username=@user and password=@pass";

            SqlCommand cmd = new SqlCommand(query,con);

            cmd.Parameters.AddWithValue("@user",UserTextBox.Text);

            cmd.Parameters.AddWithValue("@pass",PassTextBox.Text);

            con.Open();

            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.HasRows)

            {

                Session["user"] = UserTextBox.Text;

                //Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Login Successful !!')</script>");

                Response.Redirect("DASHBOARD.aspx");

            }

            else

            {

                Page.ClientScript.RegisterStartupScript(this.GetType(),"Script","<script>alert('Login Failed !!')</script>");

            }

            con.Close();

        }

    }

}

Above File is LOGIN_FORM_ASP\LOGIN.aspx.cs





using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


namespace LOGIN_FORM_ASP

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

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

            {

                Response.Write("Welcome to my site Mr/Mrs " + Session["user"].ToString());

            }

            else

            {

                Response.Redirect("LOGIN.aspx");

            }

        }


        protected void Button1_Click(object sender, EventArgs e)

        {

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

            {

                Session["user"] = null;

                Response.Redirect("LOGIN.aspx");

            }

        }

    }

}

Above File is LOGIN_FORM_ASP\LOGIN.aspx.cs
















Comments

Popular posts from this blog

AJAX Method Of jQuery AJAX In ASP.NET Web Forms | Learn ASP.NET Web Forms

List Box In ASP.NET Web Forms | ASP.NET ListBox | Web Forms Tutorial | ASP.NET

Display Auto Increment Value In Text Field After Insert To Database ASP.NET