.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to do a login page using Mysql database.

Posted By:satyapriyanayak       Posted Date: October 01, 2014    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

In this Code Snippets we will know how to do a login page using Mysql database.
 

Downloaded and installed the MySQL Connector/Net from the MySQL official website
http://dev.mysql.com/downloads/connector/net/6.1.html

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 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="Label1" 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="Label2" 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>




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();
        }
    }
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend