作者Lamfree (酷热夏天)
看板Visual_Basic
标题[ASP.NET] 指定的转换无效
时间Mon Nov 6 01:00:23 2006
小弟不才最近刚接触练习用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