Error "Index was out of range" in ASP.Net application
I am making an application ASP.Net / C # where I have a Gridview with checkbox in each row.
The problem is that some of them by selecting and clicking the "Save" the following error occurs:
Index was out of range. Must be non-negative and less than the size of the collection. \ r \ nParameter name: index.
And this error开发者_运维百科 seems to be occurring only in the last row of the Gridview. The follow is the snippet of code and properties window Gridview:
C#
protected void btnOKVulsProj_Click(object sender, EventArgs e)
{
//Variables
for (int cont = 0; cont < grdListaVulsProj.Rows.Count; cont++)
{
idVul = Convert.ToInt32((grdListaVulsProj.Rows[cont].Cells[0]).Text);
string strQueryInsert = ("INSERT TO DATABASE");
if (((CheckBox)grdListaVulsProj.Rows[idVul].FindControl("chkSelecionaItem")).Checked) <<< WHERE THE ERROR OCCURS
{
Conexao.Inserir(strQueryInsert); //Method that performs the insertion
}
};
//Other instructions
}
Gridview Properties's
http://i56.tinypic.com/33f3hqf.jpg
Note: Gridview aims to register on the database all rows that are selected.
Note2: I searched extensively for a Web solution, but all presented, none of them solved my problem.
Thank you!
[ ]'s
your line should be:
if (((CheckBox)grdListaVulsProj.Rows[cont].FindControl("chkSelecionaItem")).Checked) <<< WHERE THE ERROR OCCURS
I'd guess your problem is here:
if (((CheckBox)grdListaVulsProj.Rows[idVul]...
Is idVul
really the index into the rows collection, or did you mean to use cont
there?
The problem I usually run into is that row 0 is actually the header and the last row is the footer.
Try a compare like this:
if (grdListaVulsProj.Rows[cont].RowType == DataControlRowType.DataRow)
{
// your code here
}
精彩评论