.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

Login page by using login control in Asp.net

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 Login page by using login control in Asp.net.
 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Login_page_using_login_control._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:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate"  
           DestinationPageUrl="~/Home.aspx" BackColor="#FFFBD6" BorderColor="#FFDFAD"
           BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
           Font-Size="0.8em" ForeColor="#333333" TextLayout="TextOnTop">
         <TextBoxStyle Font-Size="0.8em" />
         <LoginButtonStyle BackColor="White" BorderColor="#CC9966" BorderStyle="Solid"
             BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#990000" />
         <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
         <TitleTextStyle BackColor="#990000" Font-Bold="True" Font-Size="0.9em"
             ForeColor="White" />
     </asp:Login>
    </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 System.Data.SqlClient;
namespace Login_page_using_login_control
{
    public partial class _Default : System.Web.UI.Page
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlCommand com;
        SqlDataAdapter sqlda;
        DataSet ds;
        string str;

        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            
            str = "select * from users where UserName='" + Login1.UserName.ToString() + "' and password='" + Login1.Password.ToString() + "'";
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                ds = new DataSet();
                sqlda = new SqlDataAdapter();
                conn.Open();
                com = new SqlCommand(str, conn);
                com.ExecuteNonQuery();
                sqlda.SelectCommand = com;
                sqlda.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    conn.Close();
                    Response.Redirect("Home.aspx");
                }
                else
                {
                    return;
                }
            }
        }
    }
}


     

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