简体   繁体   中英

DataGrid update with template column VB.net

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM