stored procedure for username,password and role output [Resolved]

Posted by reddysankark-13471 under Sql Server on 9/19/2012 | Points: 10 | Views : 2064 | Status : [Member] | Replies : 2
hi,

i will procedure my coding below how get varchar role from stored procedure

login.aspx.cs

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

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string str1 = null;
string[] UserName = null;
try
{
if (txtUserName.Text.Contains("@"))
{
string str = txtUserName.Text;
UserName = str.Split('@');
clsLogin.UserName = UserName[0].ToString();
str1 = UserName[0].ToString();
}
else
{
clsLogin.UserName = txtUserName.Text.Trim();
str1 = txtUserName.Text.Trim();
}
clsLogin.Password = txtPassword.Text.Trim();
string Role = objLogin.GetUserLogin();

if (Role == "NoUser")
lblMsg.Text = "User Name and password mismatch. Try again.";
else
if (Role == "Admin")
{
Session["UserName"] = str1;
Response.Redirect("~/Admin/AdminHome.aspx");
}
else if (Role == "Manager")
{
Session["UserName"] = str1;
Response.Redirect("~/HRManager/ManagerHome.aspx");
}
else if (Role == "Employee")
{
Session["UserName"] = str1;
Response.Redirect("~/Employee/EmployeeHome.aspx");

}

}

catch (Exception ex)
{
lblMsg.Text = ex.Message;

}
}
}

CLSLOGIN.cs

public class clsLogin:Connection
{
public clsLogin()
{
//
// TODO: Add constructor logic here
//
}
public static string UserName { get; set; }
public static string Password { get; set; }
public static string Role { get; set; }
DataSet ds = null;

public string GetUserLogin()
{
try
{
SqlParameter[] p = new SqlParameter[3];

p[0] = new SqlParameter("@UserName", UserName);
p[1] = new SqlParameter("@Password", Password );
p[2] = new SqlParameter("@Role", SqlDbType.VarChar, 50);
p[2].Direction = ParameterDirection.Output;


SqlHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "spLoginChecking", p);
Role = Convert.ToString(p[2].Value);
return Role;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
}

sankarreddy


Responses

Posted by: Vasanthmvp on: 9/20/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Create a Stored Procedure named "spLoginChecking" with the same parameters defined in cs.
For Eg: Here PersonalDetails is my table name.

Create Procedure spLoginChecking
@UserName varchar,
@Password varchar,
@Role varchar out
AS
BEGIN
If Exists (Select * From PersonalDetails where FirstName = @FirstName and LastName = @LastName)
begin
select @Role = Roles from PersonalDetails where FirstName = @FirstName and LastName = @LastName
end
end

Executing this returns the Role of the record.

Regards,

Awesome Coding !! :)

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: reddysankark-13471 on: 9/20/2012 [Member] Starter | Points: 25

Up
0
Down
hi

in this coding i will execute procedure.one more doubt in this it will not execute user name and password mismatch and user name not found code.how to execute this code when the user not found

sankarreddy

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response