0
我嘗試更新,但我得到一個錯誤的DataGrid更新「的索引超出limits.I不應該是負值,應該比集合的大小更小的」這種擔心這條線在調試「暗淡courseId作爲字符串= gridViewCourse.DataKeys(e.RowIndex).Values( 「CourseId」)。的ToString()」用模板列VB.net
這是我的網格ASP代碼
<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>
這是我的方法來更新
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
所以這條線將造成T他的錯誤,我上面
Dim courseId As String = gridViewCourse.DataKeys(e.RowIndex).Values("CourseId").ToString()
提到這條線是假設得到CourseId的價值以後用它來更新
感謝弗蘭克
我得到一個錯誤,這絲毫 「CMD作爲新的SqlCommand(((」 更新課程設置CourseName = ' 「+ courseName.Text&」',價格= ' 「)+ price.Text&」',導師='「)+ tutor.Text&」'其中CourseID =「&courseId,con)」 – FrankSharp
錯誤是關於對象引用未定義爲對象的實例? – FrankSharp
對不起相同的錯誤現在絲毫 「cmd.Parameters.Add(」 @ CourseName 「SqlDbType.VarChar,30)。價值= courseName.Text」 – FrankSharp