使用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"
其它错误都没有,很奇怪啊
...全文
24 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhl00 2003-08-13
  • 打赏
  • 举报
回复
谢了,两位,终于搞定了!
greenclass 2003-08-13
  • 打赏
  • 举报
回复
message.text="错误:未能更新记录,请确保正确填写了字段" delete it
user lable
lableMessage.Text="错误:未能更新记录,请确保正确填写了字段";
liq1979 2003-08-13
  • 打赏
  • 举报
回复
1、我觉得你最好把自定义的错误去掉,这样就会知道详细出错在哪里了。
2、update语句好像有问题。是否应该加where条件,否则所有的id号都会更新。
3、我记得oracle里面的付值语句应该是 := 。
希望能帮上你
hhl00 2003-08-12
  • 打赏
  • 举报
回复
没人知道吗?

62,066

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧