C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Capture Role from database into Session


Posted Date: 05 Sep 2008      Total Responses: 2

Posted By: samir dutta       Member Level: Gold     Points: 1



My Developer Friends

my Login table have 3 field username, userpass, userrole.

From Login Page I would like to store Session["Role"], once I capture Role (Admin/User) then I will populate Treeview based on the Role

so, pls help me how to capture Role thru session.


private bool ValidateLogin(string strUserName, string strPassword)
{
bool blnValidLogin = false;
SqlDataReader sqlDr = null;

SqlConnection sqlConn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
string strSQL = "Select username, userpass, userrole From login Where username = '" + strUserName + "' And userpass = '" + strPassword + "'";

try
{
sqlConn.Open();
SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn);
sqlDr = sqlCmd.ExecuteReader();

if (sqlDr.HasRows)
{

blnValidLogin = true;
Session["LoggedIn"] = "LoggedIn";

//string role = (string)sqlDr[2];
//Session["Role"] = "role";
}
return blnValidLogin;
}
catch (SqlException ex)
{
ctrlLogin.FailureText = ex.Message;
return false;
}
finally
{
sqlConn.Close();
}
}






Responses

Author: ANIL PANDEY    05 Sep 2008Member Level: DiamondRating:     Points: 6

hi,

please refer the following code..


protected void btnLogin_Click(object sender, ImageClickEventArgs e)
{
string strLoginID = string.Empty;
string strPassword = string.Empty;
string strQuery;
string strUname;
string strPwd;
SqlDataReader drd;
SqlConnection con = new SqlConnection(strConStr);
SqlCommand cmd = new SqlCommand();

strLoginID = txtLoginID.Text;
strPassword = txtPassword.Text;
strQuery = "Select Sno,UserName,Pwd from tbUsers where UserName='" + txtLoginID.Text.Trim() + "' and Ac_Status='E'";
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
drd = cmd.ExecuteReader();

if (drd.HasRows)
{
while( drd.Read())
{
if ((drd["UserName"].ToString() == txtLoginID.Text.Trim()) && (drd["Pwd"].ToString() == txtPassword.Text.Trim()))
{
Session["UID"] = drd[0].ToString();
Session["UName"] = drd[1].ToString();
if(check status)
{
Session["UType"] = Convert.ToInt32("1"); //Admin
Response.Redirect("adminhome.aspx");
}
else
{
Session["UType"] = "2"; //User
Response.Redirect("attsheet.aspx");
}
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Invalid login ID or password.";
txtLoginID.Text = "";
txtPassword.Text = "";
txtLoginID.Focus();
}
}
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "User does not exist.";
txtLoginID.Text = "";
txtPassword.Text = "";
txtLoginID.Focus();
}
}


Regards
Anil Pandey

Thanks & Regards
Anil Kumar Pandey



Author: samir dutta    05 Sep 2008Member Level: GoldRating:     Points: 1

Thanx Anil for your reply. but it not solved my problem.
I want to know how can i store UserRole into session variable from the following sql query

pls help




Post Reply
You must Sign In to post a response.
Next : crystal reports
Previous : getting the spaces in between two lines while insert content in word document
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use