none
.Net Development RRS feed

  • Question

  • Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Security;
    
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq; 
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    namespace gridview1
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            private SqlConnection conn = new SqlConnection(@"Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=task;User ID=sa;Password=ups");  
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) {  
                gvbind();  
            }
            }
             protected void gvbind() {  
            conn.Open();  
            SqlCommand cmd = new SqlCommand("Select * from Customers", conn);  
            SqlDataAdapter da = new SqlDataAdapter(cmd);  
            DataSet ds = new DataSet();  
            da.Fill(ds);  
            conn.Close();  
            if (ds.Tables[0].Rows.Count > 0) {  
                GridView1.DataSource = ds;  
                GridView1.DataBind();  
            } else {  
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                GridView1.DataSource = new toString();
                GridView1.DataBind();  
                
              
            }  
        }  
    
             protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {  
            GridViewRow row = (GridViewRow) GridView1.Rows[e.RowIndex];  
            Label lbldeleteid = (Label) row.FindControl("lblID");  
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete FROM Customers where userid='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", conn);  
            cmd.ExecuteNonQuery();  
            conn.Close();  
            gvbind();  
        }  
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {  
           
            GridView1.EditIndex = e.NewEditIndex;
            gvbind();  
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblID");
              
            TextBox textName = (TextBox)row.Cells[0].Controls[0];
            TextBox textadd = (TextBox)row.Cells[1].Controls[0];
            TextBox textc = (TextBox)row.Cells[2].Controls[0];
    
            GridView1.EditIndex = -1;
            GridView1.DataBind();
            conn.Open();
    
            SqlCommand cmd = new SqlCommand("update detail set name='" + textName.Text + "',address='" + textadd.Text + "',country='" + textc.Text + "'where userid='" + userid + "'", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            gvbind();
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {  
            GridView1.PageIndex = e.NewPageIndex;  
            gvbind();  
        }  
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {  
            GridView1.EditIndex = -1;  
            gvbind();  
    
    
    
        }
    
    
        }
    }

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="gridview1.WebForm2" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                OnPageIndexChanging="GridView1_PageIndexChanging" 
                OnRowCancelingEdit="GridView1_RowCancelingEdit" 
                OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" 
                OnRowUpdating="GridView1_RowUpdating" ShowHeaderWhenEmpty="True" 
                Height="107px" Width="277px">
                <Columns>
                     <asp:BoundField DataField="userid" HeaderText="S.No." />  
                            <asp:BoundField DataField="name" HeaderText="Name" />  
                            <asp:BoundField DataField="address" HeaderText="address" />  
                            <asp:BoundField DataField="country" HeaderText="Country" />  
                            <asp:CommandField ShowEditButton="true" />  
                            <asp:CommandField ShowDeleteButton="true" /> 
                </Columns>
           
                      </asp:GridView>
        </div>
         <div>  
                    <asp:Label ID="lblresult" runat="server"></asp:Label>  
                </div>
        </form>
    </body>
    </html>
    

    • Edited by Divya Diya Tuesday, January 30, 2018 5:44 AM row_updating and row_deleting cause error
    • Moved by Fei Hu Wednesday, January 31, 2018 8:54 AM ASP.NET related
    Tuesday, January 30, 2018 5:43 AM

All replies

  • Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    A collection, an array,  a grid, a datatable etc., ect. any data container that can be iterated over using an index, those type of containers that hold data items or rows have a lower and upper boundary.

    They are zero based that means you cannot set the index to a number that is less than 0,  and the index number cannot be more that the total number of rows/items in this type of data container.

    You go below 0 on the index for the container, like setting the index to a -1, then the index is below the range of the first item/row in the container.

    If you have two rows in the container that is 0 based, the 0 is the first item/row and 1 is the second item/row. You set the index to 2, then it's above the total count of number of items/rows in the container. 

    So in either case of index number below 0 and index number above the total number of items/rows in the container, then it's Index was out of range.

    Capiche?

    Tuesday, January 30, 2018 6:28 AM
  • Please post questions related to using GridView and ASP.NET for development in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 30, 2018 2:55 PM
  • Hi Divya,

    According to your question is more related to ASP.Net, You could repost a new thread to ASP.Net forum for suitable support.

    If you have some grammar or code errors in using C#, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 31, 2018 8:53 AM