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:-----------
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.");
}
}
}