<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Removing a Row of Data (SQL)</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<style type="text/css">
#zap {
background:lightgreen; border:thin inset}
</style>
<script language="C#" runat="server" src="fetchData_sql.cs" />
<script language="C#" runat="server">
SqlConnection myConn = new SqlConnection ( getConnection ( "aspnet" ) );
SqlCommand myCmd = new SqlCommand ( );
String id, msgDate, msgFrom, msgEmail, msgSubject, msgBody;
protected void Page_Load ( object src, EventArgs e ) {
myCmd.Connection = myConn;
id = Request.QueryString [ "id" ];
if ( id == null || IsPostBack ) {
if ( id == null ) bindGrid ( );
loadPanel.Visible = true;
zapPanel.Visible = false;
}
else {
getMessage ( id );
loadPanel.Visible = false;
zapPanel.Visible = true;
}
}
public void bindGrid ( ) {
myCmd.CommandText = "SELECT * FROM aspx_messages ORDER BY MessageDate desc";
myConn.Open ( );
myGrid.DataSource = myCmd.ExecuteReader ( CommandBehavior.CloseConnection );
myGrid.DataBind ( );
myConn.Close ( );
}
public void getMessage ( String id ) {
myCmd.CommandText = "SELECT * FROM aspx_messages WHERE MessageId='" + id + "'";
myConn.Open ( );
listDetails.DataSource = myCmd.ExecuteReader ( CommandBehavior.SingleRow );
listDetails.DataBind ( );
myConn.Close ( );
}
public void zapMessage ( object src, EventArgs e ) {
myCmd.CommandText = "DELETE FROM aspx_messages where MessageID=" + id;
myConn.Open ( );
myCmd.ExecuteNonQuery ( );
myConn.Close ( );
bindGrid ( );
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc -->
<div class="header"><h2>ADO.NET Primer: <span class="hilite">Removing an Existing Record</span></h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<center>
<form runat="server">
<asp:panel id="loadPanel" runat="server">
<h5>Select a Record to Delete</h5>
<asp:datagrid id="myGrid" runat="server"
width=70% cellpadding=5 gridlines="vertical"
bordercolor="black" borderwidth=1
font-size="10pt"
backcolor="ghostwhite"
alternatingitemstyle-backcolor="lightgray"
autogeneratecolumns=false>
<headerstyle backcolor="darkslategray"
forecolor="khaki" height=25 font-bold />
<columns>
<asp:boundcolumn headertext="Date"
datafield="MessageDate"
dataformatstring="{0:D}" />
<asp:hyperlinkcolumn
headertext="From"
datanavigateurlfield="MessageID"
datanavigateurlformatstring="delete.aspx?id={0}"
datatextfield="MessageFrom" />
<asp:boundcolumn headertext="Subject"
datafield="MessageSubject" />
</columns>
</asp:datagrid>
<p align="center"><a href="#top"><img alt="Back to top" border=0 height=32 onmouseover="status='Back to top of page'" src="/shared/top.gif" width=32></a></p>
</asp:panel>
<asp:panel id="zapPanel" runat="server">
<h5>To remove this record, press Delete.</h5>
<asp:datalist id="listDetails" width="70%" runat="server">
<itemtemplate>
<table id="zap" width=100% cellspacing=1 cellpadding=5 border=0>
<col width=25% align="right">
<tr>
<td>Date:</td>
<td><b><%# Eval ( "messageDate", "{0:f}" ) %></b></td></tr>
<tr>
<td>Name:</td>
<td><b><%# Eval ( "messageFrom" ) %></b></td></tr>
<tr>
<td>Email:</td>
<td><b><%# Eval ( "Email" ) %></b></td></tr>
<tr>
<td>Subject:</td>
<td><b><%# Eval ( "messageSubject" ) %></b></td></tr>
<tr>
<td>Message:</td>
<td><b><%# Eval ( "messageBody" ) %></b></td></tr>
</table>
</itemtemplate>
</asp:datalist>
<p>
<input type=submit value="Delete" runat="server"
onServerClick="zapMessage">
<input type=reset value="Cancel"
onClick="self.location.replace ( 'delete.aspx' )"></p>
</asp:panel>
</form>
</center>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>