Introduction

This application shows how to do Create, Read, Update and Delete (CRUD) operations on a BOOKS table using a DLL and a Stored Procedure. ASP.NET pages access methods from the DLL that contains the DAL in it and Stored Procedures in the SQL Server database to do the actual operations on the BOOKS table.

Open Visual Studio








Open Microsoft Visual Studio 2013 and create an Empty Web Application with any suitable name.

Database Structure

Open SQL Server to create a database (we have used database as the name of our database in SQL) with any suitable name and then create table and Stored Procedure for the CRUD operations.

Table Structure

  1. create Table books
  2. (
  3. bookid int identity(1,1) primary Key,
  4. title varchar(50) null,
  5. authors varchar(200) null,
  6. price money null,
  7. publisher varchar(50),
  8. )

Stored Procedure

  1. CREATE PROCEDURE dbo.GetBooks
  2. AS
  3. select * from books
  1. CREATE PROCEDURE dbo.GetBook(@bookid int)
  2. AS
  3. select * from books where bookid = @bookid
  1. CREATE PROCEDURE dbo.AddBook( @title varchar(50), @authors varchar(200), @price money, @publisher varchar(50) )
  2. AS
  3. insert into books (title,authors,price,publisher)
  4. values(@title,@authors,@price,@publisher)
  1. CREATE PROCEDURE dbo.DeleteBook (@bookid int)
  2. AS
  3. delete from books where bookid = @bookid
  4. if @@rowcount <> 1
  5. raiserror('Invalid Book Id',16,1)

  1. CREATE PROCEDURE dbo.UpdateBook( @bookid int, @title varchar(50), @authors varchar(200), @price money, @publisher varchar(50) )
  2. AS
  3. update books set title= @title, authors = @authors, price = @price, publisher = @publisher
  4. where bookid = @bookid;
  5. if @@rowcount <> 1
  6. raiserror('Invalid Book Id',16,1)
Class Library Structure
Now create a Class Library with the name BooksCrud.


Now add class files to create a Data Access Layer (DAL). We will add the following three class files:

  1. Book.cs (to set get, set Properties).
  2. BookDal.cs (to make the DAL).
  3. DataBase.cs (to import a connection string from Web.Config and use this file name the same as our database file name).






Connection String

Open the Web.Config file to add a connection string.

  1. <configuration>
  2. <connectionStrings>
  3. <add name="database" connectionString="Data Source=ServerName; Initial Catalog=Database; User Id=User; Password=Password;" providerName="System.Data.SqlClient"/>
  4. </connectionStrings>
  5. <system.web>
  6. <compilation debug="true" targetFramework="4.5" />
  7. <httpRuntime targetFramework="4.5" />
  8. </system.web>
  9. </configuration>

If we have made our database on Windows Authentication mode then we need to add "Integrated Security=true".

Class Files

Import the connection string into the DataBase.cs file as in the following:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Configuration;
  6. namespace BooksCrud
  7. {
  8. public class DataBase
  9. {
  10. static public String ConnectionString
  11. {
  12. get
  13. {
  14. return ConfigurationManager.ConnectionStrings["database"].ConnectionString;
  15. }
  16. }
  17. }
  18. }

Now prepare the get and set properties in the Book.cs file as in the following:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace BooksCrud
  6. {
  7. public class Book
  8. {
  9. public int Bookid { get; set; }
  10. public string Title { get; set; }
  11. public string Authors { get; set; }
  12. public string Publishers { get; set; }
  13. public double Price { get; set; }
  14. }
  15. }
Create the DAL in BookDal.cs as in the following:
  1. using System;
  2. using System.Data.SqlClient;
  3. using System.Data;
  4. using System.Configuration;
  5. namespace BooksCrud
  6. {
  7. public class BookDal
  8. {
  9. public static DataSet GetBooks()
  10. {
  11. SqlConnection con = new SqlConnection(DataBase.ConnectionString);
  12. SqlDataAdapter da = new SqlDataAdapter("getbooks", con);
  13. da.SelectCommand.CommandType = CommandType.StoredProcedure;
  14. DataSet ds = new DataSet();
  15. da.Fill(ds, "books");
  16. return ds;
  17. }
  18. public static Book GetBook(int bookid)
  19. {
  20. SqlConnection con = new SqlConnection(DataBase.ConnectionString);
  21. try
  22. {
  23. con.Open();
  24. SqlCommand cmd = new SqlCommand("getbook", con);
  25. cmd.CommandType = CommandType.StoredProcedure;
  26. cmd.Parameters.AddWithValue("@bookid", bookid);
  27. SqlDataReader dr = cmd.ExecuteReader();
  28. if (dr.Read())
  29. {
  30. Book b = new Book();
  31. b.Title = dr["title"].ToString();
  32. b.Authors = dr["authors"].ToString();
  33. b.Price = Double.Parse(dr["price"].ToString());
  34. b.Publishers = dr["publisher"].ToString();
  35. return b;
  36. }
  37. else
  38. return null;
  39. }
  40. catch (Exception ex)
  41. {
  42. return null;
  43. }
  44. finally
  45. {
  46. con.Close();
  47. }
  48. }
  49. public static string AddBook(string title, string authors, double price, string publisher)
  50. {
  51. SqlConnection con = new SqlConnection(DataBase.ConnectionString);
  52. try
  53. {
  54. con.Open();
  55. SqlCommand cmd = new SqlCommand("addbook", con);
  56. cmd.CommandType = CommandType.StoredProcedure;
  57. cmd.Parameters.AddWithValue("@title", title);
  58. cmd.Parameters.AddWithValue("@authors", authors);
  59. cmd.Parameters.AddWithValue("@price", price);
  60. cmd.Parameters.AddWithValue("@publisher", publisher);
  61. cmd.ExecuteNonQuery();
  62. return null; // success
  63. }
  64. catch (Exception ex)
  65. {
  66. return ex.Message; // return error message
  67. }
  68. finally
  69. {
  70. con.Close();
  71. }
  72. }
  73. public static string DeleteBook(int bookid)
  74. {
  75. SqlConnection con = new SqlConnection(DataBase.ConnectionString);
  76. try
  77. {
  78. con.Open();
  79. SqlCommand cmd = new SqlCommand("deletebook", con);
  80. cmd.CommandType = CommandType.StoredProcedure;
  81. cmd.Parameters.AddWithValue("@bookid", bookid);
  82. cmd.ExecuteNonQuery();
  83. return null; // success
  84. }
  85. catch (Exception ex)
  86. {
  87. return ex.Message; // return error message
  88. }
  89. finally
  90. {
  91. con.Close();
  92. }
  93. }
  94. public static string UpdateBook(int bookid, string title, string authors, double price, string publisher)
  95. {
  96. SqlConnection con = new SqlConnection(DataBase.ConnectionString);
  97. try
  98. {
  99. con.Open();
  100. SqlCommand cmd = new SqlCommand("updatebook", con);
  101. cmd.CommandType = CommandType.StoredProcedure;
  102. cmd.Parameters.AddWithValue("@bookid", bookid);
  103. cmd.Parameters.AddWithValue("@title", title);
  104. cmd.Parameters.AddWithValue("@authors", authors);
  105. cmd.Parameters.AddWithValue("@price", price);
  106. cmd.Parameters.AddWithValue("@publisher", publisher);
  107. cmd.ExecuteNonQuery();
  108. return null; // success
  109. }
  110. catch (Exception ex)
  111. {
  112. return ex.Message; // return error message
  113. }
  114. finally
  115. {
  116. con.Close();
  117. }
  118. }
  119. }
  120. }
Build the Class Library


After a successful build, now the BookCrud.dll is ready for use in our Web Project.

Now add a reference to our web project.





Web Forms Structure

menu.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>CRUD Application using .dll, DAL and Stored Procedures</title>
  5. <style>
  6. a {
  7. font-weight: 700;
  8. color: red;
  9. font-size: 12pt;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <h2>CRUD Application using DLL and Stored Procedure</h2>
  15. This application shows how to perform Create, Read , Update and Delete (CRUD) operations.
  16. ASP.NET pages access methods in DAL (Data Access Layer),which call stored procedures in
  17. Sql Server Database to perform the actual operations on BOOKS table.
  18. <a href="addbook.aspx">Add New Book</a>
  19. <p />
  20. <a href="updatebook.aspx">Update Book</a>
  21. <p />
  22. <a href="deletebook.aspx">Delete Book</a>
  23. <p />
  24. <a href="listbook.aspx">List Books</a>
  25. </body>
  26. </html>

addbook.aspx

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head id="Head1" runat="server">
  4. <title>Add Book</title>
  5. </head>
  6. <body>
  7. <form id="form1" runat="server">
  8. <h2>Add New Book</h2>
  9. <table>
  10. <tr>
  11. <td>Book Title</td>
  12. <td><asp:TextBox ID="txtTitle" runat="server"></asp:TextBox></td>
  13. </tr>
  14. <tr>
  15. <td>Authors</td>
  16. <td><asp:TextBox ID="txtAuthors" runat="server"></asp:TextBox></td>
  17. </tr>
  18. <tr>
  19. <td>Price</td>
  20. <td><asp:TextBox ID="txtPrice" runat="server"></asp:TextBox></td>
  21. </tr>
  22. <tr>
  23. <td>Publisher</td>
  24. <td><asp:TextBox ID="txtPublisher" runat="server"></asp:TextBox></td>
  25. </tr>
  26. </table>
  27. <br />
  28. <asp:Button ID="btnAdd" runat="server" Text="Add Book" OnClick="btnAdd_Click" /><br />
  29. <br />
  30. <asp:Label ID="lblMsg" runat="server" EnableViewState="False"></asp:Label><br />
  31. <p />
  32. <a href="menu.html">Go Back To Menu</a>
  33. </form>
  34. </body>
  35. </html>

addbook.aspx.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using BooksCrud;
  10. namespace BooksView
  11. {
  12. public partial class addbook : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. }
  17. protected void btnAdd_Click(object sender, EventArgs e)
  18. {
  19. string msg = BookDal.AddBook(txtTitle.Text, txtAuthors.Text, Double.Parse(txtPrice.Text), txtPublisher.Text);
  20. if (msg == null)
  21. lblMsg.Text = "Book Has Been Added Successfully!";
  22. else
  23. lblMsg.Text = "Error -> " + msg;
  24. }
  25. }
  26. }

deletebook.aspx

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head id="Head2" runat="server">
  4. <title>Delete Book</title>
  5. </head>
  6. <body>
  7. <form id="form2" runat="server">
  8. <h2>Delete Book</h2>
  9. Enter Book Id :
  10. <asp:TextBox ID="txtBookid" runat="server"></asp:TextBox>
  11. <p />
  12. <asp:Button ID="btnDelete" runat="server" Text="Delete Book" OnClick="btnDelete_Click"/>
  13. <p />
  14. <asp:Label ID="lblMsg" runat="server" EnableViewState="False"></asp:Label>
  15. <p />
  16. <a href="menu.html">Go Back To Menu</a>
  17. </form>
  18. </body>
  19. </html>
deletebook.aspx.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using BooksCrud;
  10. namespace BooksView
  11. {
  12. public partial class deletebook : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. }
  17. protected void btnDelete_Click(object sender, EventArgs e)
  18. {
  19. string msg = BookDal.DeleteBook(Int32.Parse(txtBookid.Text));
  20. if (msg == null)
  21. lblMsg.Text = "Book Has Been Deleted Successfully!";
  22. else
  23. lblMsg.Text = "Error -> " + msg;
  24. }
  25. }
  26. }

listbook.aspx

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head id="Head4" runat="server">
  4. <title>List Books</title>
  5. </head>
  6. <body>
  7. <form id="form4" runat="server">
  8. <h2>List Of Books</h2>
  9. <asp:GridView ID="GridView1" runat="server" Width="100%">
  10. <HeaderStyle BackColor="Red" Font-Bold="True" ForeColor="White" />
  11. </asp:GridView>
  12. <br />
  13. <a href="menu.html">Go Back To Menu</a>
  14. </form>
  15. </body>
  16. </html>

listbook.aspx.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using BooksCrud;
  10. namespace BooksView
  11. {
  12. public partial class listbook : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. GridView1.DataSource = BookDal.GetBooks();
  17. GridView1.DataBind();
  18. }
  19. }
  20. }

updatebook.aspx

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head id="Head3" runat="server">
  4. <title>Update Book</title>
  5. </head>
  6. <body>
  7. <form id="form3" runat="server">
  8. <h2>Update Book</h2>
  9. <table>
  10. <tr>
  11. <td>Book ID</td>
  12. <td><asp:TextBox ID="txtBookid" runat="server"></asp:TextBox>
  13. <asp:Button ID="btnGetDetails" runat="server" Text="Get Details" OnClick="btnGetDetails_Click" />
  14. </td>
  15. </tr>
  16. <tr>
  17. <td>Book Title</td>
  18. <td><asp:TextBox ID="txtTitle" runat="server"></asp:TextBox></td>
  19. </tr>
  20. <tr>
  21. <td>Authors</td>
  22. <td><asp:TextBox ID="txtAuthors" runat="server"></asp:TextBox></td>
  23. </tr>
  24. <tr>
  25. <td>Price</td>
  26. <td><asp:TextBox ID="txtPrice" runat="server"></asp:TextBox></td>
  27. </tr>
  28. <tr>
  29. <td>Publisher</td>
  30. <td><asp:TextBox ID="txtPublisher" runat="server"></asp:TextBox></td>
  31. </tr>
  32. </table>
  33. <br />
  34. <asp:Button ID="btnUpdate" runat="server" Text="Update Book" Enabled="false" OnClick="btnUpdate_Click" /><br />
  35. <br />
  36. <asp:Label ID="lblMsg" runat="server" EnableViewState="False"></asp:Label><br />
  37. <p />
  38. <a href="menu.html">Go Back To Menu</a>
  39. </form>
  40. </body>
  41. </html>

updatebook.aspx.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using BooksCrud;
  10. namespace BooksView
  11. {
  12. public partial class updatebook : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. }
  17. protected void btnGetDetails_Click(object sender, EventArgs e)
  18. {
  19. Book b = BookDal.GetBook(Int32.Parse(txtBookid.Text));
  20. if (b != null)
  21. {
  22. txtTitle.Text = b.Title;
  23. txtAuthors.Text = b.Authors;
  24. txtPrice.Text = b.Price.ToString();
  25. txtPublisher.Text = b.Publishers;
  26. btnUpdate.Enabled = true;
  27. }
  28. else
  29. {
  30. lblMsg.Text = "Sorry! Book Id Not Found";
  31. btnUpdate.Enabled = false;
  32. }
  33. }
  34. protected void btnUpdate_Click(object sender, EventArgs e)
  35. { string msg = BookDal.UpdateBook(Int32.Parse(txtBookid.Text), txtTitle.Text, txtAuthors.Text, Double.Parse(txtPrice.Text), txtPublisher.Text);
  36. if (msg == null)
  37. lblMsg.Text = "Updated Book Details Successfully!";
  38. else
  39. lblMsg.Text = "Error -> " + msg;
  40. }
  41. }
  42. }
Summary

In this article we performed CRUD Operations but using a DLL. The new thing with our DLL is that this DLL file contains a Data Access Layer (DAL) in it, with Stored Procedures in SQL Server.