"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index & quot;" error occurred when using the GridView control in. NET.

Source: Internet
Author: User

In. when you use the GridView control to edit data online, the "Index was out of range. must be non-negative and less than the size of the collection. parameter name: index "I have checked a lot of questions about the incorrect data index value on the Internet. I don't feel like there are any articles that direct point out the solutions to the problem. Let's summarize them first.

In fact, this problem requires great attention during operations. It is not in the RowEditing or RowUpdating method of the GridView control, but needs to specify the primary key of the GridView control in the class for obtaining data, the background code is as follows:

 

                               Page_Load(                            (!                                                               str_conn = ConfigurationManager.AppSettings[                 SqlConnection myConn =                                                                                  SqlConnection myConn =                    sql_Str =                   SqlDataAdapter myda =                  DataSet myDs =                    GridView1.DataSource =                                                                                                                                                  GridView1_RowEditing(              GridView1.EditIndex =                                                                  GridView1_RowUpdating(                                                  stuNum =                  newName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[                  SqlConnection myConn =                                                        sql_str =                  SqlCommand myCmd =                  myCmd.Parameters.Add(, SqlDbType.NVarChar).Value =                 myCmd.Parameters.Add(, SqlDbType.VarChar).Value =                   (myCmd.ExecuteNonQuery() >                       Response.Write(                                        Response.Write(                                    textbox1.Text =  + stuNum +  + newName +  + sql_str +  +                    GridView1.EditIndex = -                                                                                       GridView1_RowsCancelingEdit(              GridView1.EditIndex = -                      }
135   }
136 }

 

As for the front-end page, it is nothing more than binding related events. No code will be posted here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.