Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hello everyone can you help me please when I Press button to publish the file this error was show

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

Note:I do GridView2 inside GridView1 and GridView1 have a CommandName and GridView2 also have CommandName, the error happen in GridView2.

the code for define index for GridView1:

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "publish")
        {
            int index = Convert.ToInt32(e.CommandArgument);

            GridViewRow row = GridView1.Rows[index];

            int artid = Convert.ToInt32(((Label)GridView1.Rows[index].FindControl("lblartid")).Text);
            bool pub = ((CheckBox)GridView1.Rows[index].FindControl("chkpub")).Checked;
.
.
.
.


the code for define the index for GridView2 and where the error located I mark it by do under line, but I thing the error in the Definition of index :

C#
  protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "publish1")
        {
            int index = Convert.ToInt32(e.CommandArgument);
           
            GridViewRow row =GridView1.Rows[index];

            GridView grd = new GridView();

            grd = ((GridView)GridView1.Rows[index].FindControl("GridView2"));
     

          int flsid = Convert.ToInt32(((Label)grd.Rows[index].FindControl("lblflsid")).Text);
           
           bool pub = ((CheckBox)grd.Rows[index].FindControl("chkpub1")).Checked;
.
.
.
.


Please help me Quickly
Posted
Updated 6-Nov-12 10:17am
v2

what you main when you say are you sure that this second GridView has at least index+1 rows?
if you main if the GridView2 have some rows inside it the answer yes but I thing my problem is how I can define new index for GridView2 where it is inside GridView1 when I do this the below new code the Problem is finish but the code not work the web page reload but No difference happen:

C#
 protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "publish1")
        {
          int index1 = Convert.ToInt32(e.CommandArgument);

          if (e.CommandName == "publish")
           {
              int index = Convert.ToInt32(e.CommandArgument);

                GridViewRow row = GridView1.Rows[index];

                GridView grd = new GridView();

                grd = ((GridView)GridView1.Rows[index].FindControl("GridView2"));

                int flsid = Convert.ToInt32(((Label)grd.Rows[index1].FindControl("lblflsid")).Text);

                bool pub = ((CheckBox)grd.Rows[index1].FindControl("chkpub1")).Checked;


can you help me
 
Share this answer
 
Comments
Matt T Heffron 6-Nov-12 20:10pm    
The nested if statement structure you show here cannot possibly work because if the first 'if' succeeds then the second CANNOT be true (e.CommandName cannot be equal to BOTH "publish1" and "publish"). The body of the second 'if' will never be executed.

Without seeing your page structure, I can only guess at this, but it looks like you are using the GridView1_RowCommand to select a row in the "outer" GridView, and then use the GridView2_RowCommand to perform the actual "publish" action. If this is supposed to be happening with two different user interactions, then I don't think you can do this, because the "state" of the selected GridView1 row is not preserved on the server to be available for GridView2_RowCommand.

Also, the "correct" way to provide more information about your problem is to use the "Improve question" command above, not to create a "Solution" with your additional information.
mr. csharpbd2 how I can try use 0 replace index variable in this code

C#
int flsid = Convert.ToInt32(((Label)grd.Rows[index].FindControl("lblflsid")).Text);


I'm sorry but I'm Beginner in programming
 
Share this answer
 
Comments
csharpbd 6-Nov-12 22:45pm    
in those line of code you need to following change. change is below:
your code:
int flsid = Convert.ToInt32(((Label)grd.Rows[index].FindControl("lblflsid")).Text);

bool pub = ((CheckBox)grd.Rows[index].FindControl("chkpub1")).Checked;
.
.
.
.


and changed code:
int flsid = Convert.ToInt32(((Label)grd.Rows[0].FindControl("lblflsid")).Text);

bool pub = ((CheckBox)grd.Rows[0].FindControl("chkpub1")).Checked;
.
.
.
.
It looks like you are using index as the index into two different collections: GridView1.Rows and the Rows of the GridView2 that is in the row GridView1.Rows[index] (i.e., grd). Are you sure that this second GridView has at least index+1 rows?
Also, you're creating and throwing away an instance of GridView in the declaration of grd. Simplify this line and the next into one line, like this:
C#
GridView grd = ((GridView)GridView1.Rows[index].FindControl("GridView2"));
 
Share this answer
 
when you initialize gridview grd using this code
C#
grd = ((GridView)GridView1.Rows[index].FindControl("GridView2"));

gridview grd got just 1 row with index=0, then you try to get value of gridview grd using variable index. at that time you get this error.

now you can try use 0 replace index variable in this code
C#
int flsid = Convert.ToInt32(((Label)grd.Rows[index].FindControl("lblflsid")).Text);


i think it works fine...
 
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