2014-10-22 79 views
0
protected void grd_RowDeleting(object sender, GridViewDeleteEventArgs e) 
    { 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString()); 
     con.Open(); 
     string query = "delete from details where sno='"+grd.DataKeys[e.RowIndex].Value+"'";-----(getting error like Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index) 
     SqlCommand cmd = new SqlCommand(query,con); 
     cmd.ExecuteNonQuery(); 
     filldata(); 
     con.Close(); 
    } 

像索引gettnig错误超出范围。必须大于收集在网格视图行删除

+0

你的问题是什么? – Izzy 2014-10-22 10:35:26

回答

1

{

访问
 SqlConnection con=newSqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString()); 
     con.Open(); 
     int Sno = Convert.ToInt32(grd.DataKeys[e.RowIndex].Values[0].ToString()); 
     string query="delete from Details where Sno="+Sno; 
     SqlCommand cmd = new SqlCommand(query, con); 
     int result = cmd.ExecuteNonQuery(); 
     con.Close(); 

}

+0

我不得不使用Grid1.Rows [Grid1.SelectedIndex] .Cells [1] .Text来让它为我工作。但是你的帖子确实指出了我的正确方向 – nate 2014-12-10 17:43:25

0

该错误是从下面的语句

grd.DataKeys[e.RowIndex] 

到来,意味着e.RowIndex值为元素的范围之外grd.DataKeys阵列中的大小非负和少。

要进一步调试,您需要找出e.RowIndex的值,并查看grd.DataKeys阵列中有多少元素。

0

你应该在grd_RowDeleting方法在.aspx页面中添加DataKeyNames="your_id_column"属性的GridView

然后你就可以grd.DataKeys[e.RowIndex].Valuegrd.DataKeys[e.RowIndex].Values["your_id_column"]

而且它更好地剖析结果字符串