0

I'm trying to make so many session in order to retrieve information of an individual for whom session is created inside my website. for now the execute scalar gives me just one value for all the labels i have assigned to different sessions created here. i'm naive and trying to learn this but i couldn't understand the execute reader command very much...so please do write code if possible..

        SqlConnection con = new SqlConnection(strcon);
        SqlCommand cmd = new SqlCommand("Select * 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 lastname = 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();

        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);
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

1 Answers1

1

An example of sqldatareader is something like this :

      SqlConnection obj_con;
        SqlCommand obj_com;
        try
        {
            obj_con = new SqlConnection("your connection string");
            obj_com = new SqlCommand();
            obj_com.Connection = obj_con;
            obj_com.CommandText = ("Select * from Student where StudId=@sid and Password=@pw";)
            SqlParameter p_Id = new SqlParameter("@sid", SqlDbType.NVarChar);
            p_Id.Direction = ParameterDirection.Input;
            p_Id.Value = sid;
            obj_com.Parameters.Add(p_Id);
            SqlParameter p_pass = new SqlParameter("@pw", SqlDbType.NVarChar);
            p_pass.Direction = ParameterDirection.Input;
            p_pass.Value = pw;
            obj_com.Parameters.Add(p_pass);
            obj_con.Open();
            SqlDataReader obj_rd = obj_com.ExecuteReader();
            while (obj_rd.Read())
            {
                StudFirstName = obj_rd[0].ToString();
                StudLastName = obj_rd[1].ToString();
                StudId = obj_rd[2].ToString();
                Gender = obj_rd[3].ToString();
                Address = obj_rd[4].ToString();
                EmailId = obj_rd[5].ToString();
                BranchId = obj_rd[6].ToString();
                SemesterId = obj_rd[7].ToString();
            }
            obj_con.Close();
        }
        catch (Exception s)
        {

        }

if you have more that one result you can create a list of url list url and add each result to your list.obj_rd[0] refers to a first columns in my result.So you have more that one column so you can get the result of another column by :obj_rd[1],obj_rd[...]

Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • But dude...how to create session on basis of this ? and how should i declare obj_con ?? i tried string obj_con; ...but not valid...please edit ur answer. – user3447076 Apr 06 '14 at 06:21
  • Why you want to add this value to sessions? ,you can create a list like i said in my post and return the list to your page that you want to show these informations – Ehsan Akbar Apr 06 '14 at 06:26
  • I edit again ,some parameteres are undefine you should define that.like name ,family ,... and your Id and password in your query – Ehsan Akbar Apr 06 '14 at 06:45