Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7090269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:01:43+00:00 2026-05-28T08:01:43+00:00

I try an update but I get an errorThe Index was out limits.I shouldn’t

  • 0

I try an update but I get an error”The Index was out limits.I shouldn’t be negative and should be smaller than size of the collection” This concern this line in debug “Dim courseId As String = gridViewCourse.DataKeys(e.RowIndex).Values(“CourseId”).ToString()”

This my grid asp code

 <asp:GridView ID="gridViewCourse" 
  runat="server" 
  AutoGenerateColumns="False" 
  onrowcancelingedit="gridViewCourse_RowCancelingEdit" 
  onrowdeleting="gridViewCourse_RowDeleting" onrowediting="gridViewCourse_RowEditing" 
  onrowupdating="gridViewCourse_RowUpdating" 
  onrowcommand="gridViewCourse_RowCommand"          
  ShowFooter="True">


   <Columns>
   <asp:TemplateField>
   <EditItemTemplate>
   <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
    <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
     </EditItemTemplate>
     <ItemTemplate>
         <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" />
         <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
      </ItemTemplate>
      <FooterTemplate>
      <asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Images/AddNewitem.jpg" CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
    </FooterTemplate>
     </asp:TemplateField>

     <asp:TemplateField HeaderText="Id Cours">
     <EditItemTemplate>
     <asp:Label ID="lbleditId" runat="server" Text='<%#Eval("CourseId") %>'/>
     </EditItemTemplate>
      <ItemTemplate>
      <asp:Label ID="lblitemId" runat="server" Text='<%#Eval("CourseId") %>'/>
       </ItemTemplate>
      <FooterTemplate>
           <asp:TextBox ID="txtftrId" runat="server"/>
           <asp:RequiredFieldValidator ID="rfvId" runat="server" ControlToValidate="txtftrId" Text="*" ValidationGroup="validaiton"/>
                    </FooterTemplate> 
               </asp:TemplateField>


               <asp:TemplateField HeaderText="Cours">
                    <EditItemTemplate>
                           <asp:TextBox ID="txtCours" runat="server" Text='<%#Eval("CourseName") %>'/>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblCours" runat="server" Text='<%#Eval("CourseName") %>'/>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtftrCours" runat="server"/>
                    <asp:RequiredFieldValidator ID="rfvCours" runat="server" ControlToValidate="txtftrCours" Text="*" ValidationGroup="validaiton"/>
                    </FooterTemplate>
               </asp:TemplateField>


               <asp:TemplateField HeaderText="Prix">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtPrix" runat="server" Text='<%#Eval("Price") %>'/>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblPrix" runat="server" Text='<%#Eval("Price") %>'/>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtftrPrix" runat="server"/>
                    <asp:RequiredFieldValidator ID="rfvPrix" runat="server" ControlToValidate="txtftrPrix" Text="*" ValidationGroup="validaiton"/>
                    </FooterTemplate>
              </asp:TemplateField>

              <asp:TemplateField HeaderText="Tuteur">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtTuteur" runat="server" Text='<%#Eval("Tutor") %>'/>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblTuteur" runat="server" Text='<%#Eval("Tutor") %>'/>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtftrTuteur" runat="server"/>
                    <asp:RequiredFieldValidator ID="rfvTuteur" runat="server" ControlToValidate="txtftrTuteur" Text="*" ValidationGroup="validaiton"/>
                    </FooterTemplate>
              </asp:TemplateField>
        </Columns>
     </asp:GridView>

This is my method to update

     Protected Sub gridViewCourse_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)

    Dim courseId As String = gridViewCourse.DataKeys(e.RowIndex).Values("CourseId").ToString()
    Dim courseName As TextBox = DirectCast(gridViewCourse.Rows(e.RowIndex).FindControl("CourseName"), TextBox)
    Dim price As TextBox = DirectCast(gridViewCourse.Rows(e.RowIndex).FindControl("Price"), TextBox)
    Dim tutor As TextBox = DirectCast(gridViewCourse.Rows(e.RowIndex).FindControl("Tutor"), TextBox)
    con.Open()
    Dim cmd As New SqlCommand(("update Courses set CourseName='" + courseName.Text & "',Price='" + price.Text & "',Tutor='") + tutor.Text & "' where CourseID=" & courseid, con)
    cmd.ExecuteNonQuery()
    con.Close()
    lblresult.ForeColor = Color.Green
    lblresult.Text = " Details Updated successfully"
    gridViewCourse.EditIndex = -1
    BindCoursesDetails()
End Sub

So this line cause the error I mentioned above

      Dim courseId As String = gridViewCourse.DataKeys(e.RowIndex).Values("CourseId").ToString()

This line is suppose to get the value of CourseId to use it later for the update

Thanks Frank

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:01:44+00:00Added an answer on May 28, 2026 at 8:01 am

    You need to set DataKeyNames attribute of GridView control.

    <asp:GridView ID="gridViewCourse" 
      runat="server" 
      AutoGenerateColumns="False" 
      onrowcancelingedit="gridViewCourse_RowCancelingEdit" 
      onrowdeleting="gridViewCourse_RowDeleting" onrowediting="gridViewCourse_RowEditing" 
      onrowupdating="gridViewCourse_RowUpdating" 
      onrowcommand="gridViewCourse_RowCommand" 
      datakeynames="CourseId"
      ShowFooter="True">
    

    EDIT: (from the comment)

    Your update statement is invalid.

    Dim sql="UPDATE Courses set CourseName=@CourseName,Price=@Price,Tutor=@Tutor Where CourseID=@CourseID"
    
    cmd=New SqlCommand(sql,con)
    cmd.Parameters.Add("@CourseName",SqlDbType.VarChar,30).Value=courseName.Text
    cmd.Parameters.Add("@Price",SqlDbType.VarChar,20).Value=price.Text
    cmd.Parameters.Add("@Tutor",SqlDbType.VarChar,30).Value=tutor.Text
    cmd.Parameters.Add("@CourseID",SqlDbType.VarChar,10).Value=courseid
    
    con.Open()
    cmd.ExecuteNonQuery()
    cn.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to update a table using another table. I get the error number
I'm familiar with 4-part naming, but I get an error every time I try
I read this tutorial . I try, but i get error. We have Mysql
I continually get these errors when I try to update tables based on another
I'm trying to parse an XML file using PHP, but I get an error
I just prepare small update for my android app, but I get this strange
I try to update my profile image and use this code: $connection->post('account/update_profile_image', array('image' =>
I try to update a project from SVN (I have a fresh install of
I have this code for update: public Boolean update() { try { data.put(ContactsContract.Groups.SHOULD_SYNC, true);
I have a DAO class which I'm using to try select/update/insert with hibernate and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.