Here we are going to learn how to use select query in asp.net with c# using stored procedure,first we have to create table which we going to use in the stored procedure as shown below.We Are using 3 tier architecture
CREATE TABLE TBL_IEB_User
(
UserID INT IDENTITY(1,1) PRIMARY KEY,
FullName VARCHAR(250) NOT NULL,
EmailAddress VARCHAR(500),
UserName VARCHAR(50) NOT NULL,
Password VARCHAR(50)
)
CREATE Procedure SP_TBL_IEB_User
(
@UserName varchar(50),
@Password varchar(50)
)
AS
BEGIN
Select * From TBL_IEB_User Where UserName=@UserName AND Password=@Password
END
Presentation Layer:
C#:Login.cs:
protected void btnSubmit_Click(object sender, EventArgs e)
{
DataTable dtLogin = new DataTable();
LoginBL objloginBL = new LoginBL();
dtLogin = objloginBL.getLogIn(txtUserName.Text, txtPassword.Text);
if (dtLogin.Rows.Count > 0)
{
Session["UserID"] = dtLogin.Rows[0]["UserID"];
Session["UserName"] = dtLogin.Rows[0]["UserName"];
Session["FullName"] = dtLogin.Rows[0]["FullName"];
Session["EmailAddress"] = dtLogin.Rows[0]["Emailaddress"];
Response.Redirect("Home.aspx");
}
else {
lblErrorMsg.Text = "Invalid UserName/Password !";
}
}
bussiness Layer
LoginBL.cs:
public class LoginBL
{
public DataTable getLogIn(string UserName,string Password)
{
ExpensesDL objExpDL = new ExpensesDL();
return objExpDL.LogIn(UserName, Password);
}
}
LoginDL.cs:
public DataTable LogIn(string UserName, string Password)
{
using (SqlConnection objCon = new SqlConnection(new Common().GetConnectionString()))
{
objCon.Open();
DataSet ds = new DataSet();
using (SqlCommand objCmd = new SqlCommand("SP_TBL_IEB_User", objCon))
{
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = UserName;
objCmd.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = Password;
SqlDataAdapter Da = new SqlDataAdapter(objCmd);
Da.Fill(ds);
return ds.Tables[0];
}
}
}
Nice example...Please make it as user friendly code using different background colours........
ReplyDelete