The following are the steps involved in deletion of records from the database in ASP.Net:-


 




  1.  Instantiate the connection object.




  2.  Open a connection object, the connection object can be declared in the web.config file or in the .aspx file itself.




  3.  Pass the connection object to the command object.




  4.  Run the ExecuteNonQuery method of the command object.




  5.  Close the connection




  6.  Dispose the memory that is allocated to the connection and command object so that no memory leaks happened.




  7.  Use the try catch block for exception handling.




 


 


Example: Demonstrate Deleting record from the database using grid view control.


 


Web.config


 


<? xml version = " 1.0 " ?>


<!--


Note: As an alternative to hand editing this file you can use the


web admin tool to configure settings for your application. Use


the Website->Asp.Net Configuration option in Visual Studio.


A full list of settings and comments can be found in


machine.config.comments usually located in


\Windows\Microsoft.Net\Framework\v2.x\Config


-->


<configuration>


<appSettings/>


<connectionStrings>


<add name = " northwindConnectionString " connectionString = " DataSource=localhost;Initial Catalog=northwind;user id=sa;pwd=sa " />


</connectionStrings>


<system.web>


<!--


Set compilation debug="true" to insert debugging


symbols into the compiled page. Because this


affects performance, set this value to true only


during development.


-->


< compilation debug = " true " />


<!--


The <authentication> section enables configuration


of the security authentication mode used by


ASP.NET to identify an incoming user.


-->


< authentication mode = " Windows " />


<!--


The <customErrors> section enables configuration


of what to do if/when an unhandled error occurs


during the execution of a request. Specifically,


it enables developers to configure html error pages


to be displayed in place of a error stack trace.


 


<customErrors mode="RemoteOnly"


defaultRedirect="GenericErrorPage.htm">


<error statusCode="403" redirect="NoAccess.htm" />


<error statusCode="404" redirect="FileNotFound.htm" />


</customErrors>


-->


</system.web>


</configuration>


 


 


DeletingRecords.aspx


 


<% @ Page Language ="C#" AutoEventWireup ="true" CodeFile="DeletingRecords.aspx.cs" Inherits ="DeletingRecords" %>


 


<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


 


<html xmlns ="http://www.w3.org/1999/xhtml" >


<head runat ="server">


</head>


<body>


<form id ="form1" runat ="server">


<div>


<strong><em> Deleting Record from Database <br/>


</em></strong>


<br/>


<asp:GridView ID ="grdCategories" runat ="server" DataSourceID = "SQLNorthwind"


DataKeyNames = "CategoryID" AutoGenerateDeleteButton ="True" AutoGenerateEditButton ="True">


</asp:GridView>


<asp:SqlDataSource ID ="SQLNorthwind" runat ="server" ConnectionString =" <% $ ConnectionStrings:northwindConnectionString %> " ProviderName ="System.Data.SqlClient" SelectCommand ="SELECT [CategoryID], [CategoryName],[Description] FROM [Categories]" UpdateCommand ="UPDATE Categories SET CategoryName=@CategoryName,Description=@Description WHERE CategoryID=@CategoryID" DeleteCommand ="DELETE Categories WHERE CategoryID=@CategoryID">


</asp:SqlDataSource>


</div>


</form>


</body>


</html>


 


 


 

                    

Copyright © 2013 VisualBuilder. All rights reserved