当出现:
索引超出范围。必须为非负值并小于集合大小。参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index
源错误:
|
源文件: C:\Inetpub\wwwroot\news\admin\adminucl\adduser.ascx.vb 行: 111
堆栈跟踪:
|
请添加:DataKeyField="typeid"
DATALIST
#######################################################################
datalist中的HTML代码区:
<asp:datalist id="DataList1" runat="server" DataKeyField="typeid">
<HeaderTemplate>
<table>
<tr>
<td>版块类型</td>
<td>修改</td>
<td>删除</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#databinder.eval(container.dataitem,"type")%></td>
<td>
<asp:Button CommandName="edit" Runat="server" Text="编辑此列" ID="button1"></asp:Button></td>
<td>
<asp:Button CommandName="delete" Runat="server" Text="删除" ID="Button2"></asp:Button></td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID=textbox1 Runat=server Text='<%#databinder.eval(container.dataitem,"type")%>'>
</asp:TextBox></td>
<td>
<asp:Button ID="cmdupdate" Runat="server" CommandName="update" Text="更新"></asp:Button>
<asp:Button ID="cmdcancel" Runat="server" CommandName="cancel" Text="取消"></asp:Button></td>
<td>
<asp:Button ID="cmddelete" Runat="server" CommandName="delete" Text="删除"></asp:Button></td>
</tr>
</EditItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:datalist>
################################################################
datalist中的vb.net代码:
Imports System.Data.SqlClient
Imports System.Configuration
Public Class admintype
Inherits System.Web.UI.UserControl
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents DataList1 As System.Web.UI.WebControls.DataList
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Dim conset As ConfigurationSettings
Dim connstr As String = conset.AppSettings("connstr")
Dim cn As New SqlConnection(connstr)
Dim dbcmd As New SqlCommand
Dim dbda As New SqlDataAdapter
Dim dbds As New DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Page.IsPostBack Then
Else
find()
End If
End Sub
Private Sub DataList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataList1.SelectedIndexChanged
find()
End Sub
Private Sub DataList1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.EditCommand
DataList1.EditItemIndex = e.Item.ItemIndex
find()
End Sub
Private Sub DataList1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.CancelCommand
DataList1.EditItemIndex = -1
find()
End Sub
Private Sub DataList1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.UpdateCommand
Dim item As String = (CType(e.Item.FindControl("textbox1"), TextBox)).Text
'(CType(e.Item.FindControl("textbox1"), TextBox)).Text ,
Dim typeid As Integer ' = CInt((e.Item.ItemIndex).ToString) 'Int32.Parse(e.Item.Cells(1).Text)
'typeid = CInt(e.CommandArgument)
typeid = CInt(DataList1.DataKeys(e.Item.ItemIndex))
update(item, typeid)
' find()
End Sub
Private Sub DataList1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.DeleteCommand
Dim typeid As Integer
typeid = (DataList1.DataKeys(e.Item.ItemIndex))
delete(typeid)
find()
End Sub
Function find()
Try
cn.Open()
dbda = New SqlDataAdapter("select * from article_type", cn)
dbda.Fill(dbds, "article_type")
Catch ex As Exception
Response.Write(ex.Message)
End Try
DataGrid1.DataSource = dbds.Tables("article_type")
DataGrid1.DataBind()
'DataGrid1.EditItemIndex = -1
DataList1.DataSource = dbds.Tables("article_type")
DataList1.DataBind()
cn.Close()
End Function
Function update(ByVal typestr As String, ByVal typeid As Integer) '
Try
cn.Open()
dbcmd = New SqlCommand("update article_type set type='" & typestr & "' where typeid=" & typeid, cn)
dbcmd.ExecuteNonQuery()
DataGrid1.EditItemIndex = -1
Response.Write("chenggong")
Catch ex As Exception
Response.Write(ex.Message)
End Try
DataList1.DataSource = dbds.Tables("article_type")
DataList1.DataBind()
cn.Close()
End Function
Function delete(ByVal typeid As Integer)
Dim str As String = "Delete from article_type where typeid=" & typeid
Dim cmd As New SqlCommand(str, cn)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
DataList1.DataBind()
End Function
End Class
#############################################################################
DATAGRID
###############################
DATAGRID中的HTML代码区:
<asp:datagrid id="DataGrid1" AutoGenerateColumns="False" runat="server" DataKeyField="typeid">
<Columns>
<asp:BoundColumn DataField="type" HeaderText="版块类型"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="修改" HeaderText="修改" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" HeaderText="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
###################################
DATAGRID1的vb.net代码:
Imports System.Data.SqlClient
Imports System.Configuration
Public Class admintype
Inherits System.Web.UI.UserControl
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Dim conset As ConfigurationSettings
Dim connstr As String = conset.AppSettings("connstr")
Dim cn As New SqlConnection(connstr)
Dim dbcmd As New SqlCommand
Dim dbda As New SqlDataAdapter
Dim dbds As New DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Page.IsPostBack Then
Else
find()
grid()
End If
End Sub
Dim com As New SqlDataAdapter("select * from article_type", cn)
Dim ds As DataSet = New DataSet
Sub grid()
com.Fill(ds, "abc")
DataGrid1.DataSource = ds.Tables(0).DefaultView
DataGrid1.DataKeyField = "typeid" '表明主键
DataGrid1.DataBind()
End Sub
Function find()
Try
cn.Open()
dbda = New SqlDataAdapter("select * from article_type", cn)
dbda.Fill(dbds, "article_type")
Catch ex As Exception
Response.Write(ex.Message)
End Try
DataGrid1.DataSource = dbds.Tables("article_type")
DataGrid1.DataBind()
cn.Close()
End Function
Function update(ByVal typestr As String, ByVal typeid As Integer) '
Try
cn.Open()
dbcmd = New SqlCommand("update article_type set type='" & typestr & "' where typeid=" & typeid, cn)
dbcmd.ExecuteNonQuery()
DataGrid1.EditItemIndex = -1
Catch ex As Exception
Response.Write(ex.Message)
End Try
cn.Close()
End Function
Function delete(ByVal typeid As Integer)
Dim str As String = "Delete from article_type where typeid=" & typeid
Dim cmd As New SqlCommand(str, cn)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
End Function
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
cn.Open()
'使用@变量名的方法书写更新语句
Dim tb As TextBox
tb = e.Item.Cells(0).Controls(0)
Dim item As String = Convert.ToString(tb.Text)
'Dim item1 As String = (CType(e.Item.FindControl("textbox2"), TextBox)).Text
Dim typeid1 As Integer = CInt(DataGrid1.DataKeys(e.Item.ItemIndex))
Dim com As SqlCommand = New SqlCommand("update article_type set type='" & item & "' where typeid=" & typeid1, cn)
Try
com.ExecuteNonQuery()
DataGrid1.EditItemIndex = -1
'DataGrid1.DataSource = dbds.Tables("article_type")
cn.Close()
Catch ex As Exception
Response.Write("更新失败<br>" & ex.ToString)
Finally
find()
End Try
End Sub
Private Sub DataGrid1_EditCommand1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex '可由事件对象返回哪行的编辑按钮被按下
grid()
End Sub
Private Sub DataGrid1_DeleteCommand1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
Dim typeid As Integer
typeid = (DataGrid1.DataKeys(e.Item.ItemIndex))
delete(typeid)
find()
End Sub
Private Sub DataGrid1_CancelCommand1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand
DataGrid1.EditItemIndex = -1
grid()
End Sub
End Class