ASP.
NET Web Application using Data Controls with SQL Server (CRUD)
Aim
To design and implement an ASP.NET Web Application that performs CRUD (Create, Read,
Update, Delete) operations on student records stored in a SQL Server database using the
GridView Data Control.
Procedure / Steps
1. Create a database (StudentDB) and table (Students) in SQL Server Management Studio.
2. Open Visual Studio and create a new ASP.NET Web Application project.
3. Add a Web Form (Default.aspx) and design the page using GridView and SqlDataSource
controls.
4. Configure Web.config with a SQL Server connection string.
5. Implement Insert, Update, and Delete operations using SqlDataSource.
6. Add a FormView control for inserting new records.
7. Run the application and verify CRUD operations in the browser.
Program
Web.config
<connectionStrings>
<add name="StudentDBConnectionString"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=StudentDB;Integrated
Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Default.aspx
<asp:GridView ID="GridView1" runat="server"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="False"
DataKeyNames="StudentID"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true">
<Columns>
<asp:BoundField DataField="StudentID" HeaderText="ID" ReadOnly="true" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Course" HeaderText="Course" />
<asp:BoundField DataField="Marks" HeaderText="Marks" />
</Columns>
</asp:GridView>
<asp:FormView ID="FormView1" runat="server"
DataKeyNames="StudentID"
DataSourceID="SqlDataSource1"
DefaultMode="Insert">
<InsertItemTemplate>
Name: <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'
/><br />
Course: <asp:TextBox ID="txtCourse" runat="server" Text='<%# Bind("Course") %>'
/><br />
Marks: <asp:TextBox ID="txtMarks" runat="server" Text='<%# Bind("Marks") %>'
/><br />
<asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert"
Text="Add Student" />
</InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:StudentDBConnectionString %>"
SelectCommand="SELECT * FROM Students"
InsertCommand="INSERT INTO Students (Name, Course, Marks) VALUES (@Name,
@Course, @Marks)"
UpdateCommand="UPDATE Students SET Name=@Name, Course=@Course,
Marks=@Marks WHERE StudentID=@StudentID"
DeleteCommand="DELETE FROM Students WHERE StudentID=@StudentID">
</asp:SqlDataSource>
Default.aspx.cs
using System;
namespace WebAppDataControls
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
Output
Screenshot 1: SQL Server database with Students table.
Screenshot 2: GridView displaying initial records.
Screenshot 3: Insert form for adding new student.
Screenshot 4: Editing a student record in GridView.
Screenshot 5: Updated record displayed in GridView.
Screenshot 6: Record deleted from GridView.
Screenshot 7: SQL Server table showing updated data.
Result
Thus, an ASP.NET Web Application was successfully developed to perform CRUD operations
on student records stored in SQL Server using the GridView Data Control.