Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:
Line 83:       //  int CatId1 = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
Line 84:        // Label catid1 = ((Label)(gvDetails.Rows[e.RowIndex].FindControl("lb_id")));
Line 85:         int catid1 = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
Line 86:         string catname = gvDetails.DataKeys[e.RowIndex].Values["catname"].ToString();
Line 87:         TextBox txtname = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtname");


Source File: e:\project b2bchennai\Admin\CategoryDetail.aspx.cs    Line: 85 


Coding for View:

C#
protected void BindDetails()
    {
        //int catid=Convert.ToInt32(gvDetails.DataKeys.ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("select top 1 catname,catdesc from categorydetail order by catid desc", con);
       // SqlCommand cmd = new SqlCommand("select * from categorydetail order by catname,catdesc desc limit 1", con);
       // SqlCommand cmd = new SqlCommand("SELECT  TOP 1 catname,catdesc  FROM categorydetail", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
            int columncount = gvDetails.Rows[0].Cells.Count;
            gvDetails.Rows[0].Cells.Clear();
            gvDetails.Rows[0].Cells.Add(new TableCell());
            gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
            gvDetails.Rows[0].Cells[0].Text = "No Records Found";
        }
    }




 protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
         int catid1 = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
        string catname = gvDetails.DataKeys[e.RowIndex].Values["catname"].ToString();
        TextBox txtname = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtname");
        TextBox txtdesc = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtdesc");
        con.Open();
        SqlCommand cmd = new SqlCommand("update  categorydetail set catname='" + txtname.Text + "',catdesc='" + txtdesc.Text + "' where catid="+catid1,con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblresult.ForeColor = Color.Green;
        lblresult.Text = catname + " Details Updated successfully";
        gvDetails.EditIndex = -1;
        BindDetails();
    }
Posted
Updated 24-Aug-12 11:55am
v3
Comments
sjelen 24-Aug-12 9:35am    
This really depends on how you setup and initialize gvDetails (assume this is GridView).

Hi,

Actually you know the answer, if you read your error message then it says that on line number 85 there is an error.

your line 85 is,

Line 85: int catid1 = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());

So on that line the index of gvDetails.DataKeys is out of range. it means the index you are looking for is not exist in that gridview. you need to add one check condition before reading data.

You need to check if gvDetails.DataKeys.Count is covered in e.RowIndex.

Hope you got the solution.

Thanks
-Amit Gajjar
 
Share this answer
 
I don't need to read this massive code dump. The error means what it says. If you were to use your debugger, you could see what was going on. You have a collection. It has x elements in it. You are asking for mycollection[x+1]. Never iterate over a collection with random numbers, always make sure you know the index you're asking for, exists.
 
Share this answer
 
if you wana fill datagridview,number of index in datagridView must be equal to number of fields you send for datagridview.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900