0
votes

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();

        }


    }`
2
you need to show your code so we can help you.prospector
since you are just starting out, I'd recommend you create a class for the values, with a property for each value, then fill the class with the values, and store the class in session. It'll require a little more learning now, but will really help you long term.tlbignerd
Thanks. Ya, i have just started development on ASP.net C#. I been assigning values in this way Session["EMPLOYEEID_sn"] = TextBox1.Text; But wanted to know how to store the resulted value in SessionuNIKx
@NikhilParmar Looks like you already know how to store things in session, but what you're really asking is how to retrieve data from a database? Correct me if I'm wrong.Matt M
@MattyM I know how to store in Session from just textboxes. I know how to write an SQL query, but i dont know how to store the values in session that will obtained from running a sql query. i have a form that stores Eid,FNAME, MNAME,LNAME and many details into database. What i want is, when i login with correct credentials, i want to obtain all the data of a perticular Eid and display Eid,Fname,Mname ,Lname on several pages. So i want to store the obtained values in sessions so that i just use it directly on all pages rather than fetching it from database.uNIKx

2 Answers

0
votes

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.

0
votes

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