Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey guys, i have this problem. I have a DB which refers to User profiles (such as id,first name etc) and im making a simple search for this DB , so i go to the search page i type the name and i produce links with that name and when i click the link i get redirect to the users page.

The problem is how the session values are changed , if i have one result , everything is great.But when i have 2 results the session values inside the "foreach" loop keep the last found session values.

How am i gonna be able to have dedicated sessions to each link/user? i don't know what kind of solution this needs, im open to ideas and solutions.

Please post some code with your solutions or some sites i can read.

Thanks in advance , here's the search code:

C#
public partial class WebForm7 : System.Web.UI.Page
 {
     SqlConnection con = new SqlConnection("Data Source=MPAPASYMEON;Server=mpapasymeon;Database=LOGIN;Initial Catalog=LOGIN; User ID=nikolaossts; Password=aaa;Connect Timeout=240");
     string PID2;


     protected void Page_Load(object sender, EventArgs e)
           {


          }

     protected void Button1_Click1(object sender, EventArgs e)
     {

         DataTable PassRecord = new DataTable();
         String str = "select First_Name,Email_Account,Surname,id from ID where (First_Name like '%'+ @search +'%' ) OR (Surname like '%'+ @search +'%') OR (Email_Account like '%'+ @search +'%')";
         SqlCommand Srch = new SqlCommand(str, con);
         Srch.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text;
         if (TextBox1.Text != "")
         {

             con.Open();
             Srch.ExecuteNonQuery();
             SqlDataAdapter da = new SqlDataAdapter();
             da.SelectCommand = Srch;
             DataTable dt = new DataTable();
             DataSet ds = new DataSet();
             da.Fill(dt);





                     PID =(int)( Session["id"]);

                     int SaveTheFirst = PID;

             foreach (DataRow dr in dt.Rows)
             {

                 PID2 = dr["id"].ToString() ;
                 if (PID.ToString() != PID2 )
                 {

                     var field = "<a href='" + Page.ResolveUrl("~/PageView.aspx?Email=" + dr["id"].ToString()) + "'>" + (dr["First_Name"] + "").ToString() + "</a>";

                     Session["SurnameView_"] = dr["Surname"];
                     string check1 = dr["Surname"].ToString();
                     Session["idView_"] = dr["id"];
                     string check2 = dr["id"].ToString();
                     Session["EmailView_"] = dr["Email_Account"];
                     string check3 = dr["Email_Account"].ToString();



                     Response.Write(field);

                     HttpContext context = HttpContext.Current;

                     Response.Write("<br/>");
                 }
             }





             con.Close();


         }
         else
         {
             string display = " Not Valid Search Criteria!";
             ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);
         }
     }
     public string SN { get; set; }
     public string PS { get; set; }
     public string EM { get; set; }
     public int PID { get; set; }





 }
Posted
Updated 21-Oct-14 23:15pm
v3
Comments
Herman<T>.Instance 22-Oct-14 5:17am    
maybe change your SELECT statement so you only have the 1 row needed. In which case you expect more than 1 row?

You can store List of objects in Session variable, for example: create class with properties you need to store.
Then you can create List of your class and add items to that list from the search results. Finally add your newly created list to session variable.
when you need, you can cast session variable back to List and access all the search items.

adding to session:
C#
Session["SearchList"] = itemList;

take from session:
C#
List<yourclass> datafromsession= (List<yourclass>) Session["SearchList"];
 
Share this answer
 
v3
I have three different types of solutions for this issue, but you have to think about the performance to choose the right one :-

When UserProfile ID value unknown

In above two cases you need to know the ID value for the UserProfile to get data. If you don't have any ID value and you need to store and fetch all those records at a time in session then we can go like below by storing user list into session, but remember this way is good if no of records are not huge not expected to grow which needs to be placed in session.

I) Storing multiple UserProfile data into session.
C#
List<UserProfile> listUserProfile = new List<UserProfile>();

UserProfile objUserProfile1 = new UserProfile();
objUserProfile1.Id = Id1; // Id field value to supply
objUserProfile1.FirstName = firstname1; // firstname field value to supply
objUserProfile1.LastName = surname1; // surname field value to supply
objUserProfile1.Email = emailaccount1; // emailaccount field value to supply

UserProfile objUserProfile2 = new UserProfile();
objUserProfile2.Id = Id2; // Id field value to supply
objUserProfile2.FirstName = firstname2; // firstname field value to supply
objUserProfile2.LastName = surname2; // surname field value to supply
objUserProfile2.Email = emailaccount2; // emailaccount field value to supply

listUserProfile.Add(objUserProfile1);
listUserProfile.Add(objUserProfile2);
Session["UserProfileData"] = listUserProfile;


When UserProfile ID value known

II) Create a new Model class for each UseProfile holding all the properties and maintain the object of that class into session and the session key should be the UserProfile unique ID.

Ex :-
C#
UserProfile objUserProfile = new UserProfile();
objUserProfile.Id = Id; // Id field value to supply
objUserProfile.FirstName = firstname;// firstname field value to supply
objUserProfile.LastName = surname;// surname field value to supply
objUserProfile.Email = emailaccount;// emailaccount field value to supply

Session["UserData_" + objUserProfile.Id] = objUserProfile;


Se here i have concatenated the UserProfile-id value to the session key name so that it will maintain separate session for different users when multiple records are obtained.

III) Another way is to store all the data separately by concatenating field name and Id value to the key for the session to store values.
Ex:-
C#
Session["UserProfileData_ID_" + objUserProfile.Id] = objUserProfile;
Session["UserProfileData_FirstName_" + objUserProfile.Id] = objUserProfile;
Session["UserProfileData_LastName_" + objUserProfile.Id] = objUserProfile;
Session["UserProfileData_Email_" + objUserProfile.Id] = objUserProfile;


NOTE : The code as part of the above discussion are for example purposes only.

Hope this will definitely of help to you.
 
Share this answer
 
v7
Comments
frCake 22-Oct-14 6:33am    
thank you so much for the time and effort you put for this answer! i will need some time to practice and test those new stuff! i get the idea. Probably i will follow the solution with the class when i create the user. i shall come back with more questions but looks like the right way to work it around!
SRS(The Coder) 22-Oct-14 6:34am    
Welcome always..
frCake 23-Oct-14 4:13am    
I managed to use the class way , but as i understand when i go from the login page to the Default/Profile page, i have to Session the logged ID too right ? so i can write Session["UserData_"+ PID] to fill the data from the class?




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