Login Page using MYSQL Database

Downloaded and installed the MySQL Connector/Net from the MySQL official website :

Download Connector/Net

Open Visual Studio and click on the new project and give the project a name; it will open the new project, then click on the solution explorer (F4); right-click on "Reference" to add a new reference into the project. Add a reference for those two .dll files to the project (MySql.dll (Win apps), MySql.web.dll (Web apps)).

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Mysql_login_page._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

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

<head id="Head1" runat="server">

    <title>Untitled Page</title>

</head>

<body>

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

    <div>

    <asp:Label ID="lb1" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label><br />

     <asp:Label ID="Label2" runat="server" Text="Name" Font-Bold="True"

            Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>

        <asp:TextBox ID="TextBox_user_name" runat="server" ForeColor="#993300" Width="100px"></asp:TextBox><br />

        <asp:Label ID="Label3" runat="server" Text="Password" Font-Bold="True"

            Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>

        <asp:TextBox ID="TextBox_password" runat="server" ForeColor="#CC6600"

            TextMode="Password" Width="100px"></asp:TextBox><br />

        <asp:Button ID="btn_login" runat="server" Text="Login" Font-Bold="True"

            BackColor="#CCFF99" onclick="btn_login_Click"  /><br />

    </div>

    </form>

</body>

</html> 

.CS File code

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using MySql.Data.MySqlClient;

namespace Mysql_login_page

{

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

    {

        string strcon = "Server=localhost;Database=test;Uid=root;Pwd=;";

        string str;

        MySqlCommand com ;

        object obj;

 

        protected void btn_login_Click(object sender, EventArgs e)

        {

            MySqlConnection con = new MySqlConnection(strcon);

            con.Open();

            str = "select count(*) from login where UserName=@UserName and Password =@Password";

            com = new MySqlCommand(str, con);

            com.CommandType = CommandType.Text;

            com.Parameters.AddWithValue("@UserName", TextBox_user_name.Text);

            com.Parameters.AddWithValue("@Password", TextBox_password.Text);

            obj = com.ExecuteScalar();

            if (Convert.ToInt32(obj) != 0)

            {

                Response.Redirect("Welcome.aspx");

            }

            else

            {

                lb1.Text = "invalid user name and password";

            }

            con.Close();

        }

    }

}

Next Recommended Reading Login Page In MVC Using Entity Framework