locked
System.IndexOutOfRangeException: There is no row at position 0 RRS feed

  • Question

  • User773871938 posted

    hello,

    I have a gridview, I add a commandfield (select) to it when user click it, a div with some textboxes opens for edditing gridview contents. then when user select save button I want the gridview to updated. I used the below code but I encounter with the error:System.IndexOutOfRangeException: There is no row at position 0.

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            CssClass="admingrid" DataKeyNames="NewsID" 
            ShowHeader="False" Width="100%" onrowcommand="GridView1_RowCommand" 
            onrowdeleting="GridView1_RowDeleting" 
            onselectedindexchanged="GridView1_SelectedIndexChanged">
            <AlternatingRowStyle CssClass="altgridrow" />
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                    <div style="width:58%; float:right; height:150px; margin-top:10px;">
                            <table style="width:100%; " class="commenttable">
                                <tr>
                                    <td>
                                        <asp:Label ID="Label13" runat="server" CssClass="formlabel" Text="News Detail"></asp:Label>
                                    </td>
                                </tr>
                            </table>
                            <br />
                        <asp:TextBox ID="Txttext" runat="server" CssClass="admintextbox" Height="100px" 
                                Text='<%# Eval("NewsText") %>' TextMode="MultiLine" Width="98%"></asp:TextBox>
                             
                                
    
                        </div>
                        <table style="width:38%;margin-left:15px; margin-top:10px;" class="commenttable">
                            <tr>
                                <td>
                                    <asp:Label ID="Label1" runat="server" CssClass="formlabel" Text="News Headline"></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="Lblhead" runat="server" CssClass="text" 
                                        Text='<%# Eval("NewsTitr") %>'></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Label ID="Label2" runat="server" CssClass="formlabel" Text="News Summary"></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="Lblsum" runat="server" CssClass="text" 
                                        Text='<%# Eval("NewsDetail") %>'></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Label ID="Label3" runat="server" CssClass="formlabel" Text="Date"></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="Lbldate" runat="server" CssClass="text" 
                                        Text='<%# Eval("Date") %>'></asp:Label>
                                </td>
                            </tr>
                        </table>
                         
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                    <center>
                        <asp:Image ID="Image2" runat="server" Height="100px" 
                            ImageUrl='<%# Eval("Image") %>' Width="100px" />
                        </center>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CheckBoxField DataField="SetDefault" SortExpression="SetDefault" 
                    HeaderText="Show at Home Page" Text="Show at Home Page" >
                <ItemStyle CssClass="formlabel" />
                </asp:CheckBoxField>
              
                <asp:CommandField SelectImageUrl="~/img/edit.png" ShowSelectButton="True" 
                    ButtonType="Image" />
              
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
              
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="ImageButton1" runat="server" Height="20px" 
                            ImageUrl="~/img/delete.png" Width="20px" CommandName="delete" 
                            CommandArgument='<%# Eval("NewsID") %>' onclick="ImageButton1_Click" OnClientClick="return confirm('Are you sure you want to delete this News?');" />
                    </ItemTemplate>
                </asp:TemplateField>
              
            </Columns>
        </asp:GridView>
    and bind the gridview at page load by the below code:
            Dstnewsletter ds = new Dstnewsletter();
            NewsTableAdapter sda = new NewsTableAdapter();
                sda.Fill(ds.News);
                GridView1.DataSource = ds.News;
                GridView1.DataBind();
           



    and the code:

     protected void BtnEdit_Click(object sender, EventArgs e)
        {
            Dstnewsletter ds = new Dstnewsletter();
            NewsTableAdapter nta = new NewsTableAdapter();
            bool Success = nta.FillByNID(ds.News,Int32.Parse(GridView1.SelectedValue.ToString())) > 0;
            if (Success)
            {
                if (GridView1.SelectedIndex > -1)
                {
                    if (DropDownList1.SelectedItem.Value == "0")
                    {
                        int index = GridView1.SelectedIndex;
    
                        ds.News.Rows[GridView1.SelectedIndex][ds.News.NewsTitrColumn] = Txthead.Text.Trim();
                        ds.News.Rows[GridView1.SelectedIndex][ds.News.NewsDetailColumn] = Txtsum.Text.Trim();
                        ds.News.Rows[GridView1.SelectedIndex][ds.News.NewsTextColumn] = Txtdetail.Text;
                        ds.News.Rows[GridView1.SelectedIndex][ds.News.DateColumn] = Txtdate.Text;
                        ds.News.Rows[GridView1.SelectedIndex][ds.News.SetDefaultColumn] = CheckBox2.Checked;
                        if (FileUpload1.HasFile)
                        {
                            string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
    
                            if (fileExt.ToUpper() == ".JPEG" | fileExt.ToUpper() == ".JPG" | fileExt.ToUpper() == "PNG" | fileExt.ToUpper() == "GIF")
                            {
                                if (FileUpload1.PostedFile.ContentLength > 1000000)
                                {
                                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);
                                }
                                else
                                {
    
                                    FileUpload1.SaveAs(Server.MapPath("~/img/" + FileUpload1.FileName));
                                    //ds.News.Rows[GridView1.SelectedIndex][ds.News.ImageColumn];
                                    imgurl = "~/img/" + FileUpload1.FileName;
                                }
                            }
                            else { Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is not appropriate.')", true); }
    
                        }
    
                        nta.Update(ds.News);
                        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('The news Edited successfully.')", true);
                        BindGrid();
                        CheckBox2.Checked = false;
                        Txtsum.Text = "";
                        Txthead.Text = "";
                        Txtdetail.Text = "";
                        Txtdate.Text = "";
    
                    }
                }
            }
        }




    please correct me.

    Saturday, September 22, 2012 4:05 AM

Answers

  • User-457099533 posted

    You should be raising RowEditing event..similar to this (missing in your code)

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
      GridView1.EditIndex = e.NewEditIndex;
     //do something here
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 22, 2012 12:21 PM

All replies

  • User-1488931086 posted

    System.IndexOutOfRangeException: There is no row at position 53.

    Check whether ur rowindex is smaller than 0 or larger/equal than maxinum

    Saturday, September 22, 2012 4:13 AM
  • User-735357247 posted

    What it the type of "Dstnewsletter"? Can u pls post the code related to "Dstnewsletter"?

    Saturday, September 22, 2012 5:49 AM
  • User773871938 posted
    Dstnewsletter is a Dataset with 3 tables in it, it's code is below.
    <?xml version="1.0" encoding="utf-8"?> <!--<autogenerated> This code was generated by a tool to store the dataset designer's layout information. Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. </autogenerated>--> <DiagramLayout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ex:showrelationlabel="False" ViewPortX="0" ViewPortY="0" xmlns:ex="urn:schemas-microsoft-com:xml-msdatasource-layout-extended" xmlns="urn:schemas-microsoft-com:xml-msdatasource-layout"> <Shapes> <Shape ID="DesignTable:Newsletter" ZOrder="3" X="973" Y="79" Height="172" Width="300" AdapterExpanded="true" DataTableExpanded="true" OldAdapterHeight="0" OldDataTableHeight="0" SplitterPosition="102" /> <Shape ID="DesignTable:News" ZOrder="2" X="636" Y="71" Height="286" Width="300" AdapterExpanded="true" DataTableExpanded="true" OldAdapterHeight="0" OldDataTableHeight="0" SplitterPosition="159" /> <Shape ID="DesignTable:fanews" ZOrder="1" X="234" Y="60" Height="248" Width="300" AdapterExpanded="true" DataTableExpanded="true" OldAdapterHeight="0" OldDataTableHeight="0" SplitterPosition="159" /> </Shapes> <Connectors /> </DiagramLayout>

    Saturday, September 22, 2012 7:42 AM
  • User-457099533 posted

    You should be raising RowEditing event..similar to this (missing in your code)

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
      GridView1.EditIndex = e.NewEditIndex;
     //do something here
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 22, 2012 12:21 PM