Visual_Basic 板


LINE

小弟不才最近刚接触练习用ASP.NET的VB语法写了一个新增、删除、修改的退货表单 并用SQLserver2000 当资料库 不知为何在写修改部分(照书上模式打)ꔩ[王有礼老师的透视ASP.NET] 在画面修改完毕点选储存时 却出现"System.InvalidCastException指定的转换无效" 上网找资料 仍然毫无头绪 行 25: for i = 0 to e.Item.Cells.Count - 2 行 26:=>有问题的地方 strText = CType(e.Item.Cells(i).Controls(0), TextBox).text 行 27: if strText <> "" then 行 28: params(j) = strText 堆叠追踪: [InvalidCastException: 指定的转换无效。] ASP.C_back_aspx.UpdateDataStore(DataGridCommandEventArgs e) in F:\Project_A\C_back.aspx:26 ASP.C_back_aspx.b_form_Update(Object obj, DataGridCommandEventArgs e) in F:\Project_A\C_back.aspx:91 System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +109 System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +507 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26 System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138 System.Web.UI.Page.ProcessRequestMain() +1292 这是全部程式码 <%@ Page Language = "VB" Debug="true"%> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SQLClient" %> <html> <head> <title>客户退货</title> <script language="VB" runat="Server"> Dim check_day As Date = Today Dim Conn As new SQLConnection("server=localhost;uid=sa;pwd=;database=ICS") '修改资料 function UpdateDataStore( e as DataGridCommandEventArgs ) as Boolean dim i as integer dim j as integer = 0 dim params(5) as string dim strText as string dim blnGo as boolean = true for i = 0 to e.Item.Cells.Count - 2 strText = CType(e.Item.Cells(i).Controls(0), TextBox).text if strText <> "" then params(j) = strText j = j + 1 else blnGo = false lblMessage.Text = lblMessage.Text & "栏位不可空白<p>" end if next if not blnGo then return false exit function end if dim strSQL as string = "Update 退货单 set" & _ "退货单编号 = '" & params(0) & "'," & _ "退货商品编号 = '" & params(1) & "'," & _ "退货商品数量 = " & params(2) & "," & _ "退货商品售价 = " & params(3) & "," & _ " where 退货商品编号 ='" & CType(e.Item.Cells(2).Controls(0), TextBox).Text & "'" ExecuteStatement(strSQL) return blnGo end function sub Page_Load(obj as Object, e as EventArgs) if Not Page.IsPostBack then FillDataGrid2() end if end sub '显示修改 Sub FillDataGrid2(Optional EditIndex As integer = -1) dim objCmd2 as new SQLCommand("select * from 退货单", Conn) dim objReader as SQLDataReader try objCmd2.Connection.Open() objReader = objCmd2.ExecuteReader() catch ex as Exception lblMessage.Text = "错误的资料库运算" end try b_form.DataSource = objReader if not EditIndex.Equals(Nothing) then b_form.EditItemIndex = EditIndex end if b_form.DataBind() objReader.Close objCmd2.Connection.Close() End sub '修改资料 sub b_form_Edit(obj as object, e as DataGridCommandEventArgs) FillDataGrid2(e.Item.ItemIndex) end sub '修改更新 sub b_form_Update(obj as object, e as DataGridCommandEventArgs) if UpdateDataStore(E) then FillDataGrid2(-1) end if end sub '修改取消 sub b_form_Cancel(obj as object, e as DataGridCommandEventArgs) FillDataGrid2(-1) end sub '新增 Sub add_Click(Sender As object, e As EventArgs) if Page.IsValid then Dim strSQL as string = "Insert into 退货单 (退货单编号, 退货日期, 退货商品编号, 退货商品数量, 退货商品售价 )values " & _ "('" & textbox0.text & "','" & check_day & "', '"& textbox1.text & "', '" & textbox2.text & "'," & _ "'" & textbox3.text & "')" ExecuteStatement(strSQL) End If '显示表格 FillDataGrid() End Sub '清除 Sub clear_Click(Sender As object, e As EventArgs) textbox0.text=" " textbox1.text=" " textbox2.text=" " textbox3.text=" " End Sub '删除资料 Sub b_form_delete(obj As Object, e As DataGridCommandEventArgs) Dim strSQL As string = "Delete From 退货单 where 退货单编号 = '" & e.Item.Cells(0).Text & "' " ExecuteStatement(strSQL) FillDataGrid() End sub '显示退货表 Sub FillDataGrid(Optional EditIndex As integer = -1) Dim myCommand As new SQLDataAdapter("select * from 退货单 where 退货日期 = ('" & check_day & "') ", Conn) Dim dsC As DataSet = new DataSet() myCommand.Fill(dsC, "退货单") b_form.DataSource = dsC.Tables("退货单").DefaultView b_form.DataBind() End sub Function ExecuteStatement(strSQL) Dim objCmd as new SQLCommand(strSQL, Conn) try objCmd.Connection.Open() objCmd.ExecuteNonQuery() lblMessage.Text = "资料建档完成!" catch ex as Exception lblMessage.Text = "建档失败!" End try objCmd.Connection.Close() End function </script> </head> <body> <h2>客户退货</h2><hr> <form id="Form1" runat="Server"> <% Response.Write("退货日期 : " & check_day) %><p></p> <p> 退货单编号 : <asp:textbox id = "textbox0" Width="150px" runat="Server"/> <asp:RequiredFieldValidator ControlToValidate="textbox0" ErrorMessage="输入退货单编号" Type="string" runat="server"/> <p> 商品编号 : <asp:textbox id = "textbox1" Width="150px" runat="Server"/> <asp:RequiredFieldValidator ControlToValidate="textbox1" ErrorMessage="输入商品编号" Type="string" runat="server"/> <p> 商品数量: <asp:textbox id = "textbox2" width="100px" runat="server"/> <asp:RequiredFieldValidator ControlToValidate="textbox2" ErrorMessage="输入商品数量" Type="Integer" runat="server"/> <p> 商品售价 : <asp:textbox id = "textbox3" width="100px" runat="server"/> <asp:RequiredFieldValidator ControlToValidate="textbox3" ErrorMessage="输入商品售价" Type="Integer" runat="server"/> <p> <asp:button id = "add" Text="新增" OnClick="add_Click" runat="server"/> <asp:button id = "clear" Text="清除" OnClick="clear_Click" runat="server"/> <asp:Label ID="lblMessage" runat="server" /> <asp:DataGrid id="b_form" runat="Server" BorderColor="black" CellPadding="4" Font-Size="8pt" HeaderStyle-BackColor="#cccc99" ItemStyle-BackColor="#ffffff" AlternatingItemStyle-BackColor="#cccccc" AutoGenerateColumns="False" OnDeleteCommand="b_form_delete" OnEditCommand="b_form_Edit" OnCancelCommand="b_form_Cancel" OnUpdateCommand="b_form_Update"> <Columns> <asp:BoundColumn HeaderText="退货单编号" DataField="退货单编号"/> <asp:BoundColumn HeaderText="退货日期" DataField="退货日期"/> <asp:BoundColumn HeaderText="商品编号" DataField="退货商品编号"/> <asp:BoundColumn HeaderText="商品数量" DataField="退货商品数量"/> <asp:BoundColumn HeaderText="商品售价" DataField="退货商品售价"/> <asp:ButtonColumn ButtonType="PushButton" HeaderText="删除" Text="删除" CommandName="delete"/> <asp:EditCommandColumn EditText="修改" CancelText="还原" UpdateText="储存" HeaderText="编辑" ItemStyle-Wrap="False"/> </Columns> </asp:DataGrid> </form></body></html> 希望板上大大能帮小弟解惑 --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 60.248.81.160
1F:推 fumizuki:确定 Controls(0) 是 TextBox吗 11/06 07:20
2F:推 commiserate:应该可以用 findcontrol 吧 比较不会出错 11/06 09:01
3F:推 fumizuki:除了用 findcontrol 也要检查是否有找到控制项才不会出错 11/06 12:47







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:e-shopping站内搜寻

TOP