Skip to content

Asp.NET Stored Procedures

stored procedures
Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!

Asp.NET Stored Procedures

ASP.NET is a popular web application framework used to build dynamic websites, web applications, and web services. One of the most powerful features of ASP.NET is its ability to interact with databases using stored procedures. Stored procedures are precompiled SQL statements that are stored in a database and can be called from an application. In this article, we will explore the benefits of using stored procedures in ASP.NET applications, and provide a step-by-step guide on how to set up and run an ASP.NET application that uses stored procedures.

 

Benefits of Using Stored Procedures in ASP.NET

  • Improved Performance: Stored procedures are precompiled, which means that they execute faster than dynamic SQL queries. This is because the database server does not have to parse and compile the SQL statement each time it is executed.
  • Better Security: Stored procedures can be used to control access to the database. You can grant execute permission on a stored procedure to a specific user or role, while denying access to the underlying tables.
  • Easier Maintenance: By separating the SQL code from the application code, you can make changes to the database schema without affecting the application code. This makes maintenance easier and reduces the risk of introducing bugs.

Let’s take a look at how to set up and run an ASP.NET application that uses stored procedures.

Step-by-Step Guide

Step 1: Set up a Database

First, you need to create a database and tables for your application. You can use a tool like SQL Server Management Studio to create the database and tables. For this example, we will create a database called EmployeeDB and a table called Employees with columns for EmployeeID, FirstName, and LastName.

Step 2: Create a Stored Procedure

Next, you need to create a stored procedure that retrieves data from the Employees table. Here’s an example of a stored procedure that retrieves all employees:

CREATE PROCEDURE GetEmployees
AS
SELECT * FROM Employees

 

Step 3: Set up a Connection String

In your ASP.NET application, you need to set up a connection string that points to your database. You can do this in the appsettings.json file, like this:

"ConnectionStrings": {
    "BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=HR;Trusted_Connection=True;"
}

 

Step 4: Create a Data Access Layer

In your ASP.NET application, you need to create a data access layer that interacts with the database. Install the `System.Data.SqlClient` Nuget package and here’s an example of a data access class that calls the “GetEmployees” stored procedure:

 

using System.Data;
using System.Data.SqlClient;

public class EmployeeDAL
{
    public DataTable GetEmployees()
    {
        using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["EmployeeDBConnectionString"].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("GetEmployees", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    return dt;
                }
            }
        }
    }
}

 

 

Step 5: Create a Presentation Layer

In your ASP.NET application, you need to create a presentation layer that displays the data to the user. Here’s an example of a web form that displays the employee data:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET Stored Procedures Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Employee List</h1>
<asp:GridView ID="gvEmployees" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>

 

Bind the Data: Finally, you need to bind the data to the GridView control in the web form. Here’s an example of the code in the code-behind file that binds the data:

using System;

public partial class _Default : System.Web.UI.Page
   {
      protected void Page_Load(object sender, EventArgs e)
   {
if (!IsPostBack)
   {
      EmployeeDAL employeeDAL = new EmployeeDAL();
      gvEmployees.DataSource = employeeDAL.GetEmployees();
      gvEmployees.DataBind();
   2}
}

 

Once you have completed these steps, you can run the ASP.NET application and see the employee data displayed in the GridView control. You can also modify the stored procedure to filter or sort the data, or create additional stored procedures to perform other database operations.

Conclusion

Stored procedures are a powerful tool that can be used to improve the performance, security, and maintainability of ASP.NET applications. By separating the SQL code from the application code, you can make changes to the database schema without affecting the application code. This makes maintenance easier and reduces the risk of introducing bugs. By following the steps outlined in this article, you can set up and run an ASP.NET application that uses stored procedures to interact with a database.

 

 

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide