[.NET] datagrid刪除資料的問題

看板Visual_Basic作者 (小環)時間16年前 (2008/06/13 22:12), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/5 (看更多)
前言: 我的資料庫叫db2.mdb 資料表是guestbook datagird的id叫dg 想加上刪除資料的功能卻一直改失敗 所以想來請教版上的強者 如果可以的話請告訴我我錯的地方和改正的方法 謝謝<(_ _)> ---------------------------------------------------------------------- <%@import namespace="system.data"%> <%@import namespace="system.data.oledb"%> <script language="vb" runat="server"> sub data() dim con as oledbconnection dim constr as string constr = "provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("db2.mdb") con=new oledbconnection(constr) dim ada as oledbdataadapter dim ds as dataset con.open() dim sql as string sql = "select * from guestbook order by 時間 desc" ada=new oledbdataadapter(sql,con) ds=new dataset() ada.Fill(ds, "guestbook") dg.DataSource = ds.Tables("guestbook") dg.databind() con.close() end sub sub page_load(sender as object,e as eventargs) if not ispostback then data() end if end sub sub edit(sender as object,e as datagridcommandeventargs) dg.edititemindex=e.item.itemindex data() end sub sub upd(sender as object,e as datagridcommandeventargs) dim con as oledbconnection dim com as oledbcommand dim sqlstr as string dim provider="provider=microsoft.jet.oledb.4.0" Dim database = "data source=" & Server.MapPath("db2.mdb") con = new oledbconnection( provider & ";" & database ) con.open() sqlstr = "update guestbook set 標題=@標題,留言=@留言 where 標題=@key" com=new oledbcommand(sqlstr, con) com.Parameters.Add(New OleDbParameter("@標題", OleDbType.Char, 15)) com.Parameters.Add(New OleDbParameter("@留言", OleDbType.Char, 30)) com.parameters.add(new oledbparameter("@key",oledbtype.char)) Dim tx(4) As TextBox tx(2) = e.Item.Cells(2).Controls(0) tx(3) = e.Item.Cells(3).Controls(0) com.parameters("@key").value=dg.datakeys(e.item.itemindex) com.Parameters("@標題").Value = tx(2).Text com.Parameters("@留言").Value = tx(3).Text com.executenonquery() con.close() dg.edititemindex = -1 data() end sub sub can(sender as object,e as datagridcommandeventargs) dg.edititemindex=-1 data() end sub Protected Sub m_click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Redirect("leaveword.aspx") End Sub Protected Sub dg_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg.DeleteCommand Dim con As OleDbConnection Dim com As OleDbCommand Dim provider = "provider=microsoft.jet.oledb.4.0" Dim database = "data source=" & Server.MapPath("db2.mdb") con = New OleDbConnection(provider & ";" & database) con.Open() com.CommandText = "delete from guest where 標題 =" & dg.DataKeys(e.Item.ItemIndex) com.ExecuteNonQuery() con.Close() data() End Sub </script> <html> <body> <form id="Form1" runat="server"> <asp:datagrid id="dg" runat="server" headerstyle-backcolor="lightblue" headerstyle-forecolor="blue" bordercolor="#E7E7FF" cellpadding=3 itemstyle-backcolor="yellow" horizontalalign="Center" oneditcommand="edit" onupdatecommand="upd" oncancelcommand="can" datakeyfield="標題" autogeneratecolumns="False" BackColor="White" BorderStyle="None" BorderWidth="1px" GridLines="Horizontal" Width="550px" OnDeleteCommand="dg_DeleteCommand"> <columns> <asp:editcommandcolumn headertext="編輯" buttontype="PushButton" edittext="編輯" updatetext="更新" canceltext="取消" /> <asp:ButtonColumn ButtonType="PushButton" CommandName="Delete" HeaderText="刪除" Text="刪除"> </asp:ButtonColumn> <asp:boundcolumn datafield="姓名" headertext="姓名" readonly="True"/> <asp:boundcolumn datafield="標題" headertext="標題"/> <asp:boundcolumn datafield="留言" headertext="留言"/> <asp:boundcolumn datafield="時間" headertext="時間" readonly="True"/> </columns> <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" /> <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" /> <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" Mode="NumericPages" /> <AlternatingItemStyle BackColor="#F7F7F7" /> <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" /> <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" /> </asp:datagrid> <hr style="position: relative" /> <asp:Button ID="b1" runat="server" OnClick="m_click" Style="left: 499px; position: relative; top: 6px" Text="繼續留言" Width="80px" /> </form> </body> </html> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.216.18.167
文章代碼(AID): #18Ke1JtO (Visual_Basic)
討論串 (同標題文章)
文章代碼(AID): #18Ke1JtO (Visual_Basic)