Reputation: 123
I am new to ASP.net. I have been taking values from Session[] variables and storing into Session variables, but I am not able to store values obtained after running a SELECT query in ASP.net.
Example: Select Fname, Mname , Lname from employee
After running this query I want to store the value of "Fname", "Mname" and "Lname" in 3 separate session variables so that I can retrieve in all the further pages from these variables.
Heres my code to check whether its a valid user or not. What I want is, once it is authenticated, all its data be fetched from database and get store in the session variables so that they can be used on all pages directly.
`protected void Button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["UserDetailsForGridViewConnectionString"].ConnectionString); conn.Open();
string loginCheck = " select count(*) from USERDETAILS where EMPLOYEEID='" + TextBox1.Text + "' and PASSWORD='" + TextBox2.Text + "'";
SqlCommand myloginCheckcmd = new SqlCommand(loginCheck, conn);
int temp = Convert.ToInt32(myloginCheckcmd.ExecuteScalar().ToString());
if (temp == 1)
{
LoginStatus.Text = "SUCCESS !!";
temp = 0;
Session["EMPLOYEEID_sn"] = TextBox1.Text;
Session["IsLogin"] = "yesLogin";
//TextBox1.Text = "";
Response.Redirect("~/CustomPages/Lecturer_PersonalDetailsPage.aspx");
conn.Close();
}
else
{
LoginStatus.Text = "Invalid Username/Password";
temp = 0;
TextBox1.Text = "";
conn.Close();
}
}`
Upvotes: 0
Views: 9627
Reputation: 3779
You need to modify your select statement to bring back the fields you're looking for. I'm not familiar with your database, but it would be something like:
Select Fname, Mname , Lname from employee inner join USERDETAILS on employee.employeeid = userdetails.employeeid
Then instead of ExecuteScalar, use ExecuteReader like this:
var rdr = myloginCheckcmd.ExecuteReader();
if(rdr.HasRows)
{
while(rdr.Read())
{
Session("fname") = rdr[0];
Session("mname") = rdr[1];
Session("lname") = rdr[2];
}
LoginStatus.Text = "SUCCESS !!";
Response.Redirect("~/CustomPages/Lecturer_PersonalDetailsPage.aspx");
conn.Close();
}
else
{
LoginStatus.Text = "Invalid Username/Password";
TextBox1.Text = "";
conn.Close();
}
There are a lot of improvements you could make to this code (look into using a parameterized query), but this hopefully gets you what you're looking for.
Upvotes: 0
Reputation: 1
If you develop in C# you can use:
DataSet ds = new DataSet();
ds = result of query;
Session["fname"] = ds.Tables[0].Rows[0]["FNAME"];
Session["mname"] = ds.Tables[0].Rows[0]["MNAME"];
Session["lname"] = ds.Tables[0].Rows[0]["LNAME"];
If you develop in vb you can use:
Dim ds As New DataSet()
ds = return of query
Session("fname") = ds.Tables(0).Rows(0)("FNAME")
Session("mname") = ds.Tables(0).Rows(0)("MNAME")
Session("lname") = ds.Tables(0).Rows(0)("LNAME")
NOTE: you must be sure of number of results in query, because the query returns more than 1 result, the variable session only store the first
Upvotes: 0