使用datagrid修改数据问题(急啊!)
hhl00 2003-08-12 02:00:08 我使用以下代码实现数据修改,可是奇怪的是,当只有一条记录时,可以成功修改,但是当有两条以上时,就无法修改,老是出错,代码如下,帮我看看:
<%@ Page Language="VB" Debug="true" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.oledb" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>更新数据行</title>
<script language="VB" runat="server">
dim myconnection as oledbconnection
sub page_load(sender as object,e as eventargs)
myconnection=new oledbconnection("Provider=OraOLEDB.Oracle.1;User ID=scott; Password=tiger")
if not IsPostBack then
bindgrid()
end if
end sub
sub mydatagrid_edit(sender as object,e as datagridcommandeventargs)
mydatagrid.edititemindex=cint(e.item.itemindex)
bindgrid()
end sub
sub mydatagrid_cancel(sender as object,e as datagridcommandeventargs)
mydatagrid.edititemindex=-1
bindgrid()
end sub
sub mydatagrid_update(sender as object,e as datagridcommandeventargs)
dim ds as dataset
dim mycommand as oledbcommand
dim updatecmd as string="update student1 set 学号=:id,姓名=:name,班级=:class,性别=:sex,身体状况=:health"
mycommand=new oledbcommand(updatecmd,myconnection)
mycommand.parameters.add(new oledbparameter(":id",oledbtype.varchar,10))
mycommand.parameters.add(new oledbparameter(":name",oledbtype.varchar,8))
mycommand.parameters.add(new oledbparameter(":class",oledbtype.varchar,20))
mycommand.parameters.add(new oledbparameter(":sex",oledbtype.varchar,4))
mycommand.parameters.add(new oledbparameter(":health",oledbtype.varchar,8))
'获取要修改行的记录的ID号
mycommand.parameters(":id").value=mydatagrid.datakeys(cint(e.item.itemindex))
dim cols as string()={":id",":name",":class",":sex",":health"}
dim numcols as integer=e.item.cells.count
dim i as integer
for i=1 to numcols-1
dim currenttextbox as textbox
currenttextbox=e.item.cells(i).controls(0)
dim colvalue as string=currenttextbox.text
if i<5 and colvalue="" then
message.text="错误:学号、姓名、班级、性别和身体状况不允许使用空值!"
message.style("color")="red"
return
end if
mycommand.parameters(cols(i-1)).value=colvalue
next
mycommand.connection.open()
try
mycommand.executenonquery()
message.text="<b>已更新记录</b><br>"
mydatagrid.edititemindex=-1
catch exp as oledbexception
message.text="错误:未能更新记录,请确保正确填写了字段"
message.style("color")="red"
end try
mycommand.connection.close()
bindgrid()
end sub
sub bindgrid()
dim ds as dataset
dim mycommand as oledbdataadapter
mycommand=new oledbdataadapter("select * from student1",myconnection)
ds=new dataset()
mycommand.fill(ds,"student1")
mydatagrid.datasource=ds.tables("student1").defaultview
mydatagrid.databind()
end sub
</script>
</head>
<body style="font:10.5pt 宋体">
<form runat="server">
<h3><font face="宋体">更新数据行</font></h3>
<asp:label ID="message" EnableViewState="false" Font-Name="arial" Font-Size="10" runat="server"/><p>
<asp:datagrid BackColor="#ccccff" BorderColor="black" CellPadding="3" CellSpacing="0" DataKeyField="学号" Font-Name="verdana" Font-Size="10" HeaderStyle-BackColor="#aaaadd" ID="mydatagrid" runat="server" ShowFooter="false" ShowHeader="true" Width="750" OnUpdateCommand="mydatagrid_update" OnEditCommand="mydatagrid_edit" OnCancelCommand="mydatagrid_cancel">
<columns>
<asp:editcommandcolumn EditText="编辑" CancelText="取消" UpdateText="更新" ItemStyle-Wrap="false"/>
</columns>
</asp:datagrid>
</form>
</body>
</html>
出错就是我自定义的错误:catch exp as oledbexception
message.text="错误:未能更新记录,请确保正确填写了字段"
message.style("color")="red"
其它错误都没有,很奇怪啊