Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

In my code i have used an arraylist for storing my usernames which are retrieved from database. I have an role gridview. that contains several role name and image user button. when the imagebutton is clicked the corresponding usernames associate with it should display. In one role there are many users are associated. but my code displays only one user. How all the users associated with the role will be display on imagebutton click please help.
My code is as follows:-
C#
if (e.CommandName == "ImageButton2_Click")
{
   int rowindex = Convert.ToInt32(e.CommandArgument);
   //Get Row
   GridViewRow gvr = GridView2.Rows[rowindex];
   {
      Label ID = (Label)GridView2.Rows[rowindex].Cells[1].FindControl("Role_ID");
      string Role_ID = ID.Text;
      con.Open();

      using (SqlCommand comm = new SqlCommand("select [UserName] from Login where Role_ID =@Role", con))
      {
         // 2. define parameters used in command object
         SqlParameter para = null;
         para = new SqlParameter();
         para.ParameterName = "@Role";
         para.Value = Role_ID;
         comm.Parameters.Add(para);
         SqlDataReader reade = comm.ExecuteReader();
         ImageButton button =
            (ImageButton)GridView2.Rows[rowindex].Cells[5].FindControl("ImageButton2");
         button.Visible = false;
                   
         ArrayList yourList = new ArrayList();
         if (reade.HasRows)
         {
            if (reade != null)
            {
               while (reade.Read())
               {
                  //ImageButton button =(ImageButton)GridView2.Rows[rowindex].Cells[5].FindControl("ImageButton2");
                  button.Visible = true;
                   
                  yourList.Add(Convert.ToString(reade[0]));
                  Session["UserName"] = yourList[0];
               }
            }
         }
      }
      if (Session["UserName"].ToString() == null)
      {
         ImageButton button =
            (ImageButton)GridView2.Rows[rowindex].Cells[5].FindControl("ImageButton2");
         button.Visible = false;
      }
      else
      {
         ImageButton button =
            (ImageButton)GridView2.Rows[rowindex].Cells[5].FindControl("ImageButton2");
         button.Visible = true;

         ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + Session["UserName"].ToString() + "');", true);

         con.Close();
      }
   }
}
Posted
Updated 18-May-14 21:57pm
v3
Comments
Bh@gyesh 19-May-14 3:28am    
Hi,
Here, First of all, debug your code and identify howmany records fetched in reader?
second thing. You are storing element in list but where do u assign list items to grid?
Member 10578683 19-May-14 3:36am    
It is only one record is fetching from database
Member 10578683 19-May-14 3:37am    
I have to display the elements in the list in a dialog box after clicking the image button in gridview
[no name] 19-May-14 7:46am    
Your code is doing exactly what you told it to do, Session["UserName"] = yourList[0] will only assign one and only one name to your session variable.

1 solution

You need to start looking at your code yourself: we can't do much from here because we can't run it under the same conditions you can. For example, we do not have access to your database, so we can't even begin to duplicate your conditions.

So, use the debugger as has been suggested and start looking at what the code does and what data it is showing.

For example, you say it "returns one row" - how do you know that? What did you look at to decide that? The reader? The client display?
Which user does it return? The first? The last? A real user? Or is it an "Odd" name, like "System.Collections.ArrayList" which is what I would expect from your code?

Think about what you are doing, and use the debugger - because that is the only way you are going to solve this, and we can't do it for you!
 
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