locked
Index out of range in gridview RRS feed

  • Question

  • User-464762670 posted
    protected void LinkButton_Click(Object sender, EventArgs e)
            {
    
                String MyConnection2 = "Server=localhost;database=ovs;Uid=root;password=; Convert Zero Datetime=True";
                DateTime time = DateTime.Now;              // Use current time
                string format = "yyyy-MM-dd HH:mm:ss";
                string UserName4 = HttpContext.Current.User.Identity.Name;
                GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
                Label lblStudentId = (Label)grdrow.Cells[0].FindControl("lblID");
              Label lblvID = (Label)GridView2.Rows[0].FindControl("lblvID");
               string label1val = lblvID.Text;
    
                string studentId = lblStudentId.Text;
                //    string id =;
                //      string testing = this.Encrypt(id.Trim());
                String query = "insert into voting (CandidateStudentID,voterStudentID,DateTime)values ('" + lblStudentId.Text + "','" + Session["UserName"].ToString() + "','" + time.ToString(format) + "')";
               
                if (Session["UserName"].ToString().Equals(lblvID.Text))
                {
    
                    Label1.Text = "You voted beofre";
    
                }
                else
                {
                    MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                    MySqlCommand MyCommand2 = new MySqlCommand(query, MyConn2);
                    MySqlDataReader MyReader2;
                    MyConn2.Open();
                    MyReader2 = MyCommand2.ExecuteReader();
    
                    Label2.Text = "Thank you for You Vote";
    
    
                }
    
    
            }


            <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False" Font-Size="Medium">
    <Columns>
        <asp:TemplateField HeaderText="Student ID">
          <ItemTemplate>
             <asp:Label ID="lblID" runat="server"   Width="150px"  Text='<%#Eval("studentID") %>'/>
         </ItemTemplate>
     </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
          <ItemTemplate>
             <asp:Label ID="lblname" runat="server"   Width="150px"  Text='<%#Eval("Name") %>'/>
         </ItemTemplate>
     </asp:TemplateField>
      
        
        
          <asp:TemplateField HeaderText="">
                <ItemTemplate>                                     
                          <asp:LinkButton runat="server"  OnCommand="LinkButton_Click" Text=" Vote For This Candidate"> </asp:LinkButton>     
                 </ItemTemplate>
         </asp:TemplateField>
    
    </Columns>
         </asp:GridView>
    
           
      <asp:GridView ID="GridView2" runat="server"  AutoGenerateColumns="False" Font-Size="Medium">
                  <Columns>
                  <asp:TemplateField HeaderText="Student ID">
          <ItemTemplate>
             <asp:Label ID="lblvID" runat="server"   Width="150px"  Text='<%#Eval("voterStudentID") %>'/>
         </ItemTemplate>
     </asp:TemplateField>
    
                  </Columns>
               </asp:GridView>

    I'm getting the error message index out of range at  Label lblvID = (Label)GridView2.Rows[0].FindControl("lblvID"); and I have no idea how to slove it. I tried to change the code but it didn't work, I keep getting the same error

    Monday, October 19, 2020 10:17 AM

All replies

  • User-1330468790 posted

    Hi kwcys,

     

    The error message already told you that the problem is an "IndexOutOfRange" exception for the grid view rows collection.

    Have you bound the data to the control "GridView2"? 

     

    I suspect that the answer is no. Then the "Rows" contains 0 rows when you are trying to access the first element.

    It would be much helpful to target the problem if you could post the data bind method for the control "GridView2".

     

    Best regards,

    Sean

    Tuesday, October 20, 2020 3:45 AM