主题:菜鸟_请教:DataGrid 数据编辑 问题
做了一个 DataGrid 数据编辑的小程序,但是在点击"更新"是 却出现了一个错误
"至少一个参数没有被指定值"
源代码如下:
<%@import namespace=system.data%>
<%@import namespace=system.data.oledb%>
<script language=vb runat=server>
sub data()
dim connstring as string="provider=microsoft.jet.oledb.4.0;"& _
"data source="&server.mappath("Grades.mdb")
dim cmdobject as new oledbdataadapter("select *from 成绩单",connstring)
dim ds as new dataset()
cmdobject.fill(ds,"成绩单")
dg.datasource=ds.tables("成绩单")
dg.databind()
end sub
sub page_load(sender as object,e as eventargs)
if not ispostback then
data()
end if
end sub
sub on_edit(sender as object ,e as datagridcommandeventargs)
dg.edititemindex=e.item.itemindex
data()
end sub
sub on_cancel(sender as object, e as datagridcommandeventargs)
dg.edititemindex=-1
data()
end sub
sub on_update(sender as object, e as datagridcommandeventargs)
dim name as string =ctype(e.item.cells(1).controls(0),textbox).text
dim account as string =ctype(e.item.cells(2).controls(0),textbox).text
dim statistic as string=ctype(e.item.cells(3).controls(0),textbox).text
dim strsql as string
strsql ="update 成绩单 set 姓名=' "& name &" ', 会计="& account & _
", 统计="& statistic &" where "& dg.datakeyfield &" =' "& _
dg.datakeys(e.item.itemindex) & "'"
Dim objconn As new oledbConnection()
objConn.connectionString = "provider=microsoft.jet.oledb.4.0;"& _
"data source="&server.mappath("Grades.mdb")
objconn.open()
Dim objCmd As New oledbcommand(strsql, objConn)
objCmd.ExecuteNonQuery()
dg.edititemindex = -1
data()
end sub
</script>
<html>
<body>
<form runat="server">
<asp:datagrid runat="server" id="dg" showheader="true" datakeyfield="学号" cellpadding=4 cellspacing=2
headerstyle-backcolor=red itemstyle-backcolor=yellow
horizontalalign=center
oneditcommand="on_edit"
onupdatecommand="on_update"
oncancelcommand="on_cancel" >
<columns>
<asp:editcommandcolumn headertext="功能" edittext="编辑" updatetext="更新" canceltext="取消"/>
</columns>
</asp:datagrid>
</form>
</body>
</html>
"至少一个参数没有被指定值"
源代码如下:
<%@import namespace=system.data%>
<%@import namespace=system.data.oledb%>
<script language=vb runat=server>
sub data()
dim connstring as string="provider=microsoft.jet.oledb.4.0;"& _
"data source="&server.mappath("Grades.mdb")
dim cmdobject as new oledbdataadapter("select *from 成绩单",connstring)
dim ds as new dataset()
cmdobject.fill(ds,"成绩单")
dg.datasource=ds.tables("成绩单")
dg.databind()
end sub
sub page_load(sender as object,e as eventargs)
if not ispostback then
data()
end if
end sub
sub on_edit(sender as object ,e as datagridcommandeventargs)
dg.edititemindex=e.item.itemindex
data()
end sub
sub on_cancel(sender as object, e as datagridcommandeventargs)
dg.edititemindex=-1
data()
end sub
sub on_update(sender as object, e as datagridcommandeventargs)
dim name as string =ctype(e.item.cells(1).controls(0),textbox).text
dim account as string =ctype(e.item.cells(2).controls(0),textbox).text
dim statistic as string=ctype(e.item.cells(3).controls(0),textbox).text
dim strsql as string
strsql ="update 成绩单 set 姓名=' "& name &" ', 会计="& account & _
", 统计="& statistic &" where "& dg.datakeyfield &" =' "& _
dg.datakeys(e.item.itemindex) & "'"
Dim objconn As new oledbConnection()
objConn.connectionString = "provider=microsoft.jet.oledb.4.0;"& _
"data source="&server.mappath("Grades.mdb")
objconn.open()
Dim objCmd As New oledbcommand(strsql, objConn)
objCmd.ExecuteNonQuery()
dg.edititemindex = -1
data()
end sub
</script>
<html>
<body>
<form runat="server">
<asp:datagrid runat="server" id="dg" showheader="true" datakeyfield="学号" cellpadding=4 cellspacing=2
headerstyle-backcolor=red itemstyle-backcolor=yellow
horizontalalign=center
oneditcommand="on_edit"
onupdatecommand="on_update"
oncancelcommand="on_cancel" >
<columns>
<asp:editcommandcolumn headertext="功能" edittext="编辑" updatetext="更新" canceltext="取消"/>
</columns>
</asp:datagrid>
</form>
</body>
</html>