Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to login my webpage using database is sql server 2008 give me full code..


if may be possible give me separate code dl layer , bl layer then login.aspx.cs
code


thanks
Posted
Updated 19-Jun-13 23:50pm
v2

Login.aspx.cs page code =============================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using System.Data;
using System.Web.Administration;
using System.Web.UI.HtmlControls;
using System.Web.DynamicData;

public partial class Login : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{

}


//login code
protected void Button1_Click(object sender, EventArgs e)
{
clsBLayer c2 = new clsBLayer();

DataSet ds = new DataSet();
ds = c2.LoginToSystem(txtUserName.Text,txtPwd.Text);

if (!String.IsNullOrEmpty(txtUserName.Text))
{
// Session["UserName"] = txtUserName.Text.Trim();
// Response.Redirect("~/Admin/AdminDashboard.aspx");

if (ds.Tables[0].Rows.Count != 0)
{

Session["UserName"] = ds.Tables[0].Rows[0][1].ToString();
Session["UserName"] = txtUserName.Text.Trim();
Response.Redirect("~/Admin/indexAdmin.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}
}


protected void btnClear_Click(object sender, EventArgs e)
{
clear();
}

protected void clear() {

txtUserName.Text = "";
txtPwd.Text = "";
}


}




DLayer................... code ===================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DL
{

public class clsDLayer
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString.ToString());
public DataSet LoginToSystem(string UserName, string Passsword)
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from tbluser where UserName='" + UserName.ToString() + "' and Password = '" + Passsword.ToString() + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);

return ds;

if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}


BL Layer code: ===============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DL;
using System.Data;
using System.Data;
namespace BL
{
public class clsBLayer
{

clsDLayer obj = new clsDLayer();

public DataSet LoginToSystem(string UserName, string Password)
{
DataSet ds = obj2.LoginToSystem(UserName,Password);
return ds;


}
}
}


success full code i use this...
 
Share this answer
 
It is very easy way to make login functionality with different classes. Please refer below code it's helping you:

Create two class file for declaring properties and method to perform some operation:

Class1:-----------

C#
//declare properties as per your table defination
namespace _classDL
{
    public class Class1
    {
        public Class1()
        {

        }

        public int id { get; set; }
        public int username { get; set; }
        public int password { get; set; }
    }
}



Class2:-----------

using System.Data;
using System.Data.SqlClient;

namespace _classBL
{
public class Class2
{
SqlConnection cnn = new SqlConnection("Pass your connection string over here");
public Class2()
{

}

public DataSet LoginToSystem(string username, string passsword)
{
cnn.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from users where username='" + username.ToString() + "' and '" + passsword.ToString() + "'", cnn);
DataSet ds = new DataSet();
da.Fill(ds);

return ds;

if (cnn.State == ConnectionState.Open)
{
cnn.Close();
}
}
}
}

Login.aspx:---------

using System.Data;
using _classBL;
using _classDL;

public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnLogin_Click(object sender, EventArgs e)
{
Class1 c2 = new Class2();

DataSet ds = new DataSet();
ds = c2.LoginToSystem(txtusername.text, txtpassword.text);

if (ds.Tables[0].Rows.Count != 0)
{
Session["username"] = ds.Tables[0].Rows[0][1].ToString();
Response.Redirect("Default.aspx");
}
else
{
Response.Write("Sorry! Incorrect Username and Password.");
}
}
}
 
Share this answer
 
v2
Comments
santsho kumar 27-Jul-13 6:08am    
thank you sir ji...................
Chintan Desai1988 24-Aug-13 3:20am    
u welcome dear......:-)
 
Share this answer
 
Comments
Prasad_Kulkarni 20-Jun-13 6:00am    
Even better, +5!
Prasad Khandekar 20-Jun-13 6:10am    
Thank's Prasad!
Here you go[^]. The complete code with lot of options.
 
Share this answer
 
Comments
Prasad Khandekar 20-Jun-13 5:54am    
Nice answer. +5
Prasad_Kulkarni 20-Jun-13 6:00am    
Thank you Prasad! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900