the connectionStrings in Web.config ....
<connectionStrings>
<add name="UserLoginSystem" connectionString="Data Source=KNIGHT-PC;Initial Catalog=dbLogin;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
then I have created a class and named it DBGateway which will be used as a base class of all class for db connection.
Codes of the DBGateway class.
using System.Configuration;
using System.Data.SqlClient;
namespace UserLoginSystem.DAL.Gateway
{
public class DBGateway
{
private SqlConnection sqlConnection;
public SqlCommand SqlCommand;
public DBGateway()
{
sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["UserLoginSystem"].ConnectionString);
SqlCommand =new SqlCommand();
}
public SqlConnection SqlConnectionObj
{
get
{
return sqlConnection;
}
}
public SqlCommand SqlSqlcommandObj
{
get
{
SqlCommand.Connection = sqlConnection;
return SqlCommand;
}
}
}
}
now codes of Entity class .... I named the class LoginSys and declared two properties UserName and Password and used auto property.
namespace UserLoginSystem.DAL.DAO
{
public class LoginSys
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
now codes of Gateway class ... I named the class LoginGateway and used DBGateway as base class for db connection.
using System;
using System.Data.SqlClient;
using UserLoginSystem.DAL.DAO;
namespace UserLoginSystem.DAL.Gateway
{
public class LoginGateway:DBGateway
{
public LoginSys loginSys;
public bool GetLoginInfoSys(string username, string password)
{
try
{
SqlConnectionObj.Open();
string query = "";
query = "SELECT * FROM dbo.tbl_user_login WHERE
username='" + username + "' and
password='" + password + "'";
SqlCommand = new SqlCommand(query, SqlConnectionObj);
SqlDataReader sqlDataReader = SqlCommand.ExecuteReader();
if (sqlDataReader!=null)
{
return sqlDataReader.HasRows;
}
}
catch (Exception)
{
throw new Exception("Username not found");
}
finally
{
if (SqlConnectionObj != null && SqlConnectionObj.State == ConnectionState.Open)
{
SqlConnectionObj.Close();
}
}
return false;
}
}
}
public class LoginGateway:DBGateway .... [here DBGateway is base class and LoginGateway is sub class] and finally I closed the connection that's very important.
now codes of Default.aspx page .... First of all I have taken two textbox, one label, one button and I named those as usernameTextBox.Text, passwordTextBox.Text, lblmsg.Text and loginButton.
using System;
using UserLoginSystem.DAL.DAO;
using UserLoginSystem.DAL.Gateway;
namespace UserLoginSystem
{
public partial class _Default : System.Web.UI.Page
{
private LoginSys loginSys;
private LoginGateway loginGateway;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void loginButton_Click(object sender, EventArgs e)
{
try
{
loginGateway = new LoginGateway();
if (loginGateway.GetLoginInfoSys(usernameTextBox.Text, passwordTextBox.Text) == true)
{
loginSys = new LoginSys();
loginSys.UserName = usernameTextBox.Text;
Session["username"] = loginSys.UserName;
Response.Redirect("Hello.aspx");
}
else
{
lblmsg.Text = "Username/Password wrong";
}
}
catch (Exception exception)
{
lblmsg.Text = exception.Message;
}
}
}
}
I have used the Session variable to get the user name in the redirected page.
loginSys = new LoginSys();
loginSys.UserName = usernameTextBox.Text;
Session["username"] = loginSys.UserName;
and finally the codes of redirected page ...
using System;
namespace UserLoginSystem
{
public partial class Hello : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblmsg.Text = "Hi " + Session["username"];
}
}
}
}
used a lebel lblmsg.Text to show the username and caught the data using Session variable Session["username"]
Never hasitate to comment or ask me.
Happy Codding .......
<connectionStrings>
<add name="UserLoginSystem" connectionString="Data Source=KNIGHT-PC;Initial Catalog=dbLogin;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
then I have created a class and named it DBGateway which will be used as a base class of all class for db connection.
Codes of the DBGateway class.
using System.Configuration;
using System.Data.SqlClient;
namespace UserLoginSystem.DAL.Gateway
{
public class DBGateway
{
private SqlConnection sqlConnection;
public SqlCommand SqlCommand;
public DBGateway()
{
sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["UserLoginSystem"].ConnectionString);
SqlCommand =new SqlCommand();
}
public SqlConnection SqlConnectionObj
{
get
{
return sqlConnection;
}
}
public SqlCommand SqlSqlcommandObj
{
get
{
SqlCommand.Connection = sqlConnection;
return SqlCommand;
}
}
}
}
now codes of Entity class .... I named the class LoginSys and declared two properties UserName and Password and used auto property.
namespace UserLoginSystem.DAL.DAO
{
public class LoginSys
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
now codes of Gateway class ... I named the class LoginGateway and used DBGateway as base class for db connection.
using System;
using System.Data.SqlClient;
using UserLoginSystem.DAL.DAO;
namespace UserLoginSystem.DAL.Gateway
{
public class LoginGateway:DBGateway
{
public LoginSys loginSys;
public bool GetLoginInfoSys(string username, string password)
{
try
{
SqlConnectionObj.Open();
string query = "";
query = "SELECT * FROM dbo.tbl_user_login WHERE
username='" + username + "' and
password='" + password + "'";
SqlCommand = new SqlCommand(query, SqlConnectionObj);
SqlDataReader sqlDataReader = SqlCommand.ExecuteReader();
if (sqlDataReader!=null)
{
return sqlDataReader.HasRows;
}
}
catch (Exception)
{
throw new Exception("Username not found");
}
finally
{
if (SqlConnectionObj != null && SqlConnectionObj.State == ConnectionState.Open)
{
SqlConnectionObj.Close();
}
}
return false;
}
}
}
public class LoginGateway:DBGateway .... [here DBGateway is base class and LoginGateway is sub class] and finally I closed the connection that's very important.
now codes of Default.aspx page .... First of all I have taken two textbox, one label, one button and I named those as usernameTextBox.Text, passwordTextBox.Text, lblmsg.Text and loginButton.
using System;
using UserLoginSystem.DAL.DAO;
using UserLoginSystem.DAL.Gateway;
namespace UserLoginSystem
{
public partial class _Default : System.Web.UI.Page
{
private LoginSys loginSys;
private LoginGateway loginGateway;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void loginButton_Click(object sender, EventArgs e)
{
try
{
loginGateway = new LoginGateway();
if (loginGateway.GetLoginInfoSys(usernameTextBox.Text, passwordTextBox.Text) == true)
{
loginSys = new LoginSys();
loginSys.UserName = usernameTextBox.Text;
Session["username"] = loginSys.UserName;
Response.Redirect("Hello.aspx");
}
else
{
lblmsg.Text = "Username/Password wrong";
}
}
catch (Exception exception)
{
lblmsg.Text = exception.Message;
}
}
}
}
I have used the Session variable to get the user name in the redirected page.
loginSys = new LoginSys();
loginSys.UserName = usernameTextBox.Text;
Session["username"] = loginSys.UserName;
and finally the codes of redirected page ...
using System;
namespace UserLoginSystem
{
public partial class Hello : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblmsg.Text = "Hi " + Session["username"];
}
}
}
}
used a lebel lblmsg.Text to show the username and caught the data using Session variable Session["username"]
Never hasitate to comment or ask me.
Happy Codding .......
No comments:
Post a Comment