ADO.net - Difference between SQL Command and SQL Command Builder
a) SQLCommand is used to execute all kind of SQL queries like DML(Insert update Delete) & DDL like(Create table drop table etc)
b)SQLCommandBuilder object is used to build & execute SQL (DML) queries like select insert update & delete.
remove.aspx
<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <html> <head> <title>Removing a Row of Data in a DataSet</title> <link rel="stylesheet" href="/aspxtreme/shared/netdemos.css"> <style type="text/css"> #zap { background:rgb ( 224,224,232 ); border:thin inset} </style> <script language="C#" runat="server"> DataSet myDataSet; DataTable myTable; DataRow thisRecord; SqlDataAdapter adapter; String id, msgDate, msgFrom, msgEmail, msgSubject, msgBody; protected void Page_Load ( object src, EventArgs e ) { SqlConnection myConn = new SqlConnection ( ConfigurationSettings.AppSettings [ "aspa" ] ); adapter = new SqlDataAdapter ( "SELECT * FROM teacher.aspx_messages order by MessageDate desc", myConn ); myDataSet = new DataSet ( ); adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; adapter.Fill ( myDataSet, "Messages" ); SqlCommandBuilder cmdBuilder = new SqlCommandBuilder ( adapter ); myTable = myDataSet.Tables [ "Messages" ]; if ( Request.QueryString [ "id" ] ==null || IsPostBack ) { if ( Request.QueryString [ "id" ] ==null ) BindGrid ( ); loadPanel.Visible = true; zapPanel.Visible = false; } else { int id = Int32.Parse ( Request.QueryString [ "id" ] ); thisRecord = myTable.Rows.Find ( id ); getMessage ( ); loadPanel.Visible = false; zapPanel.Visible = true; } } public void BindGrid ( ) { myGrid.DataSource = myTable.DefaultView; myGrid.DataBind ( ); } public void getMessage ( ) { msgDate = thisRecord [ "MessageDate" ].ToString ( ); msgFrom = thisRecord [ "MessageFrom" ].ToString ( ); msgEmail = thisRecord [ "Email" ].ToString ( ); msgSubject = thisRecord [ "MessageSubject" ].ToString ( ); msgBody = thisRecord [ "MessageBody" ].ToString ( ); zapPanel.DataBind ( ); } public void zapMessage ( object src, EventArgs e ) { int id = Int32.Parse ( Request.QueryString [ "id" ] ); thisRecord = myTable.Rows.Find ( id ); thisRecord.Delete ( ); adapter.Update ( myTable ); BindGrid ( ); } </script> </head> <body> <!-- #include virtual="~/shared/top.inc" --> <div class="header"><h3>ADO.NET Primer: <span class="hilite">Removing a Row of Data in a DataSet</span></h3></div> <hr size=1 width=90%> <center> <asp:panel id="loadPanel" runat="server"> <h5>Select a Record to Delete</h5> <asp:datagrid id="myGrid" runat="server" width=90% cellpadding=5 gridlines="vertical" bordercolor="black" borderwidth=1 font-size="8pt" headerstyle-backcolor="lightsteelblue" headerstyle-font-bold headerstyle-height=25 alternatingitemstyle-backcolor="whitesmoke" autogeneratecolumns=false> <columns> <asp:boundcolumn headertext="Date" datafield="MessageDate" dataformatstring="{0:d}" /> <asp:hyperlinkcolumn headertext="From" datanavigateurlfield="MessageID" datanavigateurlformatstring="remove.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="/aspxtreme/shared/top.gif" width=32></a></p> </asp:panel> <asp:panel id="zapPanel" runat="server"> <h5>To remove this record, press Delete.</h5> <form runat="server"> <table id="zap" width="85%" cellspacing=1 cellpadding=5 border=0> <col width=35% align="right"> <tr> <td>Date:</td> <td><b><%# msgDate %></b></td></tr> <tr> <td>Name:</td> <td><b><%# msgFrom %></b></td></tr> <tr> <td>Email:</td> <td><b><%# msgEmail %></b></td></tr> <tr> <td>Subject:</td> <td><b><%# msgSubject %></b></td></tr> <tr> <td>Message:</td> <td><b><%# msgBody %></b></td></tr> </table> <p><hr size=1 width=90%><input type=submit value="Delete" runat="server" onServerClick="zapMessage"> <input type=reset value="Cancel" onClick="self.location.replace ( 'remove.aspx' )"></p> </form> </asp:panel> </center> </body> </html>
edit.aspx
No comments :
Post a Comment