[ASP.NET] 指定的轉換無效

看板Visual_Basic作者 (酷熱夏天)時間19年前 (2006/11/06 00:36), 編輯推噓3(300)
留言3則, 2人參與, 最新討論串1/3 (看更多)
小弟不才最近剛接觸練習用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

11/06 07:20, , 1F
確定 Controls(0) 是 TextBox嗎
11/06 07:20, 1F

11/06 09:01, , 2F
應該可以用 findcontrol 吧 比較不會出錯
11/06 09:01, 2F

11/06 12:47, , 3F
除了用 findcontrol 也要檢查是否有找到控制項才不會出錯
11/06 12:47, 3F
文章代碼(AID): #15JXC5jJ (Visual_Basic)
文章代碼(AID): #15JXC5jJ (Visual_Basic)