.NET中使用GridView控件輸入數據時出現“ Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"的問題


在.NET中使用GridView控件的在線編輯數據時,出現了“ Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"的關於數據索引值錯誤的問題,在網上查了許多,感覺都沒有什么文章是直接指出解決問題的方法,先就總結下吧

其實,這個問題在操作時是需要非常注意的,它並不在GridView控件的RowEditing或者RowUpdating方法中,而是需要在獲取數據的類中指定GridView控件的主鍵,后台代碼如部分如下:

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Web.UI;
  6 using System.Web.UI.WebControls;
  7 using System.Data;
  8 using System.Data.SqlClient;
  9 using System.Configuration;
 10 
 11 namespace UI
 12 {
 13     public partial class _Default : System.Web.UI.Page
 14     {
 15         protected void Page_Load(object sender, EventArgs e)
 16         {
 17             //判斷是否第一次加載Bind()類
 18             if (!IsPostBack)
 19             {
 20                 Bind();
 21             }
 22         }
 23 
 24         /// <summary>創建返回數據庫連接的公共類
 25         /// </summary>
 26         /// <returns>返回連接數據庫的類</returns>
 27         public SqlConnection GetConnection()
 28         {
 29                 string str_conn = ConfigurationManager.AppSettings["myConStr"].ToString();
 30                 SqlConnection myConn = new SqlConnection(str_conn);
 31                 return myConn;
 32         }
 33 
 34         /// <summary>將查詢的數據綁定到GridView控件中
 35         /// </summary>
 36         public void Bind()
 37         {     
 38             try
 39             {
 40                 SqlConnection myConn = GetConnection();
 41                 myConn.Open();
 42 
 43                 string sql_Str = "select * from stuInfo order by stuAge desc";
 44 
 45                 SqlDataAdapter myda = new SqlDataAdapter(sql_Str, myConn);
 46                 DataSet myDs = new DataSet();
 47                 myda.Fill(myDs);
 48 
 49                 GridView1.DataSource = myDs;
 50 
 51                 //為GridView控件設置主鍵,此處必須有否則會產生如下代碼中的溢出錯誤
 52                 /*
 53                  * Index was out of range. Must be non-negative and less than the size of the collection.
 54                   Parameter name: index 
 55                  */
 56                 GridView1.DataKeyNames = new string[] { "stuNo" };//GridView控件設置主鍵,此處最為關鍵,稍不留意就忘掉了,切記呀
 57                 GridView1.DataBind();
 58 
 59                 myDs.Dispose();
 60                 myda.Dispose();
 61                 myConn.Close();
 62             }
 63             catch (Exception ex)
 64             {
 65                 Response.Write(ex.Message);
 66             }
 67         }
 68 
 69         /// <summary>創建GridView控件的RowEditing事件類
 70         /// </summary>
 71         /// <param name="sender"></param>
 72         /// <param name="e"></param>
 73         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
 74         {
 75             GridView1.EditIndex = e.NewEditIndex;
 76             this.Bind();
 77         }
 78 
 79         /// <summary>創建GridView控件的RowUpdating事件類
 80         /// </summary>
 81         /// <param name="sender"></param>
 82         /// <param name="e"></param>
 83         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 84         {
 85             try
 86             {
 87                 //獲取在GridView控件里相關事件的鍵值
 88                 int stuNum = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
 89                 string newName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
 90 
 91                 SqlConnection myConn = GetConnection();
 92                 myConn.Open();
 93 
 94                 /*
 95                  * string sql_str = "update stuInfo set stuName='" + newName + "' where stuNo='" + stuNum + "'";
 96                  *與上句相比,以下使用帶參數的SQL語句在一定程度上可防止SQL注入攻擊
 97                 */
 98                 string sql_str = "update stuInfo set stuName=@newName where stuNo=@stuNum";
 99                 SqlCommand myCmd = new SqlCommand(sql_str, myConn);
100                 myCmd.Parameters.Add("@newName", SqlDbType.NVarChar).Value = newName;
101                 myCmd.Parameters.Add("@stuNum", SqlDbType.VarChar).Value = stuNum;
102 
103                 if (myCmd.ExecuteNonQuery() > 0)
104                 {
105                     Response.Write("<script type='text/javascript'>alert('更新成功');</script>");
106                 }
107                 else
108                 {
109                     Response.Write("<script type='text/javascript'>alert('更新失敗');</script>");
110                 }
111 
112                 //調試用
113                 textbox1.Text = "學號:" + stuNum + " , 姓名:" + newName + ", " + sql_str + ", ExecuteNonQuery狀態:" + myCmd.ExecuteNonQuery();
114 
115                 myCmd.Dispose();
116                 myConn.Close();
117                 GridView1.EditIndex = -1;
118                 this.Bind();
119             }
120             catch (Exception ex)
121             {
122                 Response.Write(ex.Message);
123             }
124         }
125 
126         /// <summary>創建GridView控件的RowCancelingEdit事件類
127         /// </summary>
128         /// <param name="sender"></param>
129         /// <param name="e"></param>
130         protected void GridView1_RowsCancelingEdit(object sender, GridViewCancelEditEventArgs e)
131         {
132             GridView1.EditIndex = -1;
133             this.Bind();
134         }
135   }
136 }

 

至於前台頁面,無非就是綁定相關的事件了,在這就不貼代碼了 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM