Click here to Skip to main content
15,885,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
If i consider a table of name "students", if a student logs in his/her account inside which he/she can see only and only his/her data which are linked to their respective column is to be displayed. for example, if studid and password are taken in login and table also comprises of their first name,last name and address, how can a particular studid: 1 can see only his/her data linked which is his/her full name, address and so on. I have tried session, but it displays only name everywhere whose code is given below :

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["StudId"].ToString();
        Label2.Text = Session["StudFirstName"].ToString();
        Label17.Text = Session["FatherName"].ToString();
        Label18.Text = Session["StudLastName"].ToString();
        Label3.Text = Session["PhoneNo"].ToString();
        Label4.Text = Session["Gender"].ToString();
        Label5.Text = Session["Address"].ToString();
        Label6.Text = Session["EmailId"].ToString();
        Label7.Text = Session["DateOfBirth"].ToString();
        Label8.Text = Session["BranchId"].ToString();
        Label9.Text = Session["SemesterId"].ToString();
        Label10.Text = Session["FatherName"].ToString();
        Label11.Text = Session["FatherPhoneNo"].ToString();
        Label12.Text = Session["MotherName"].ToString();
        Label13.Text = Session["MotherPhoneNo"].ToString();
        Label14.Text = Session["Library"].ToString();
        Label15.Text = Session["Hostel"].ToString();
        Label16.Text = Session["Bus"].ToString();
    }
}


whose session i have created on my login page as below code :

protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           if (Request.Cookies["UName"] != null)
               TextBox1.Text = Request.Cookies["UName"].Value;
           if (Request.Cookies["PWD"] != null)
               TextBox2.Attributes["value"] = Request.Cookies["PWD"].Value;
           if (Request.Cookies["UName"] != null && Request.Cookies["PWD"] != null)
               CheckBox1.Checked = true;
       }
   }
   protected void Button1_Click1(object sender, EventArgs e)
   {

        if (DropDownList1.SelectedItem.Value == "1")
       {
           SqlConnection con = new SqlConnection(strcon);
           SqlCommand cmd = new SqlCommand("Select StudFirstName from Student where StudId=@sid and Password=@pw", con);

           cmd.Parameters.AddWithValue("@sid", TextBox1.Text);
           cmd.Parameters.AddWithValue("@pw", TextBox2.Text);
           con.Open();

            string name = Convert.ToString(cmd.ExecuteScalar());

           string gender = Convert.ToString(cmd.ExecuteScalar());
           string dob = Convert.ToString(cmd.ExecuteScalar());
           string pno = Convert.ToString(cmd.ExecuteScalar());
           string address = Convert.ToString(cmd.ExecuteScalar());
           string branch = Convert.ToString(cmd.ExecuteScalar());
           string library = Convert.ToString(cmd.ExecuteScalar());
           string bus = Convert.ToString(cmd.ExecuteScalar());
           string hostel = Convert.ToString(cmd.ExecuteScalar());
           string semester = Convert.ToString(cmd.ExecuteScalar());
           string fname = Convert.ToString(cmd.ExecuteScalar());
           string mname = Convert.ToString(cmd.ExecuteScalar());
           string fpno = Convert.ToString(cmd.ExecuteScalar());
           string mpno = Convert.ToString(cmd.ExecuteScalar());
           string email = Convert.ToString(cmd.ExecuteScalar());
           string img = Convert.ToString(cmd.ExecuteScalar());
           con.Close();

           con.Open();
           string lastname = Convert.ToString(cmd.ExecuteScalar());
           con.Close();

           if (String.IsNullOrEmpty(name))
               Label1.Text = "Sorry! Invalid User ID or Password!";
           else
           {
               if (CheckBox1.Checked)
               {
                   Response.Cookies["UName"].Value = TextBox1.Text;
                   Response.Cookies["PWD"].Value = TextBox2.Text;
                   Response.Cookies["UName"].Expires = DateTime.Now.AddMonths(2);
                   Response.Cookies["PWD"].Expires = DateTime.Now.AddMonths(2);
               }

               Session.Add("StudId", TextBox1.Text);
               Session.Add("StudFirstName", name);
               Session.Add("StudLastName", lastname);
               Session.Add("Gender", gender);
               Session.Add("DateOfBirth", dob);
               Session.Add("PhoneNo", pno);
               Session.Add("Address", address);
               Session.Add("BranchId", branch);
               Session.Add("Library", library);
               Session.Add("Bus", bus);
               Session.Add("Hostel", hostel);
               Session.Add("SemesterId", semester);
               Session.Add("FatherName", fname);
               Session.Add("MotherName", mname);
               Session.Add("FatherPhoneNo", fpno);
               Session.Add("MotherPhoneNo", mpno);
               Session.Add("EmailID", email);
               Session.Add("StudImg", img);
               FormsAuthentication.RedirectFromLoginPage(name, false);
               Response.Redirect("Default.aspx");
               Debug.Write(Session["studfirstname"].ToString());
           }
Posted

1 solution

Don't try to save every thing in sessions, rather than save the UserId or StudentId in session and using this session id, try to retrieve the data from the database.

For example, you have a table named Students with column name StudentID
so, now write a method to get the data which will take a integer of studentID as a parameter and retrieve all the data based on the StudentID and bind it to the data grid or what every may be.

but never save all the data in sessions, this is not a good programming practice.

and I am unable to understand your code also, why you need to call "ExecuteScalar()" method those many times, why can't use some ExecuteReader() method to retrieve all the fields and traverse through the reader like this

SqlDataReader dr = new SqlDataReader(....//pass necessary params....);
dr = cmd.ExecuteDataReader();

while(dr.Read())
{
//Populate your students data into a student class rather than into a individual variables
}

you might be a beginner to the programming world, so, that's the reason why you are unable to get a good way of approach.

I hope you understand what I mean to say, if not post your query here again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900