Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
for (int i = 0; i < GridView1.Rows.Count; i++)
            {

                Label ID = (Label)GridView1.Rows[i].Cells[0].FindControl("ID");
                int lID = Convert.ToInt32(ID.Text);
                con.Open();
                using (SqlCommand command = new SqlCommand("select Vendor_Name from VandorDetail where ID=@ID", con))
                { // 2. define parameters used in command object 
                    SqlParameter param = new SqlParameter();
                    param.ParameterName = "@ID"; param.Value = lID; // 3. add new parameter to command object 
                    command.Parameters.Add(param); // // Read in the SELECT results. // 
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    { Session["Vendor_Name"] = Convert.ToString(reader["Vendor_Name"]); }
                    con.Close();
                }
            }
Posted
Updated 2-Apr-14 0:58am
v2
Comments
ArunRajendra 2-Apr-14 7:00am    
What's the issue?
Member 10578683 2-Apr-14 7:01am    
Session shows the last value
Member 10578683 2-Apr-14 7:03am    
When I am clicking the button of 1st row in my gridview in the seesion last row's vendor_Name is Appearing
King Fisher 2-Apr-14 7:07am    
if your query returns more than one value ,the session value will be overwrite by last value
Member 10578683 2-Apr-14 7:11am    
Query I have Written to get One value

1 solution

Well, yes...it would.
What you have done is the equivalent of:
C#
string myString = "";
for (int i = 0; i < 10; i++)
   {
   myString = i.ToString();
   }
And you are surprised that myString always equals "9"...
When you assign to the Session, it is the same as assigning to any other collections: you overwrite the previous content.
 
Share this answer
 
Comments
Member 10578683 2-Apr-14 7:07am    
But whast will i write Yaar to get exact value
OriginalGriff 2-Apr-14 7:12am    
How would I know? :laugh:
I don't know what you are trying to do - or what the rest of your code expects to find in the session variable!
If I tell you "use a list" and add each item to it, then set the Session to the whole list then I could break other code which depends on that variable being a single string!
Sorry, but you will have to either give more details, or work it out for yourself...

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