Question

I have to catch the username entered in the login page and get corresponding profileid(the column in DB) and show it in another page my DAL code is

public DataTable getall()
   {
       SqlConnection conn1 = Generic.DBConnection.OpenConnection();
       DataTable dt=new DataTable();
       try
       {
             string sql = "Select * from Profile_Master";
             SqlCommand cmds = new SqlCommand(sql,conn1);
             SqlDataAdapter sqlDa = new SqlDataAdapter(cmds);
           sqlDa.Fill(dt);


       }
       catch (Exception ex)
       {

           throw ex;
       }
       return dt;
   }

My UI

DataTable dt1 = new DataTable();
                ProfileMasterDAL dal = new ProfileMasterDAL();
                dt1 = dal.getall();
                if (dt1.Rows.Count > 0)
                {
                    Session["sdds"] = dt1.Rows[0]["FirstName"].ToString();
                    Session["EmailId"] = dt1.Rows[0]["EmailID"].ToString();
                    Session["pid"] = dt1.Rows[0]["NewidColumn"].ToString();
                    Response.Redirect("~/Myhome.aspx");

but i am able to get the first value in the DB not the corresponding value for the username?

Was it helpful?

Solution

Your select statement has 10 rows. and the required row for you is in 5th position. when you say dt1.Rows[0]["FirstName"] you will get only the first record but not the 5th record.

If you want to make this work, add where condition to your select statement, which returns exactly one required row for you.

something like..

"select * from Profile_Master PM Where PM.UserName = @username"

where @username is your input from login page. And is unique in the database Table

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top