ASP.NET Sample Programs with Source Code

ASP.NET Sample Programs

ASP.NET is a web development framework that is used to build dynamic web pages, interactive web services, and robust data-driven web applications. It is a part of the Microsoft .NET framework. It stands for Active Server Pages. Integrated Development Environment (IDE) is an application used by coders to ease their software development tasks.

ASP.NET is one of the most famous frameworks for students and developers to develop their development projects. In 2002, Microsoft released the first version of the ASP.NET framework. It is based on the CLR (Common Language Runtime) which enables developers to build ASP.NET applications. Common Language Runtime includes the classes, interfaces and data types that support a wide range of technologies.

ASP.NET Features

i. Cross-platform & container support

ii. Cross-platform & container support

iii. Asynchronous via async/await

iv. Multiple environments and development mode

v. Unified MVC & Web API frameworks

vi. Cross-Site Request Forgery (CSRF) Protection.

vii. WebSockets & SignalR

ASP.NET Projects with Source Code

For your first sample ASP.NET projects, As you will see, ASP.NET is very easy. You will be up and running in no time at all.

1. Start a new document in either Visual Studio.NET or the text editor of your choice.

2. Enter the code into the document, and then go to File -> Save As and name it HelloWorld.ASPX in your Web root folder.

3. Launch your Web browser and enter the location of the new file (such as- localhost/helloworld.aspx).

 

File: Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 50px;
        }
        .style2
        {
            width: 100px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" BackColor="#0000CC" Font-Bold="True" 
            Font-Size="XX-Large" ForeColor="#3399FF" Text="PERSONAL INFO"></asp:Label>
        <br />
        <asp:Label ID="lblID" runat="server"></asp:Label>
        <br />
        <table style="width: 33%;">
            <tr>
                <td class="style2" nowrap="nowrap">
                    <asp:Label ID="Label2" runat="server" Text="First Name:"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtFname" runat="server"></asp:TextBox>
                </td>
                <td class="style1">
                    &nbsp;</td>
                <td align="left" rowspan="11" valign="top">
                    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
                        AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" 
                        DataSourceID="SqlDataSource2" Font-Size="Small" ForeColor="#333333" 
                        GridLines="None">
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:CommandField ShowSelectButton="True" />
                            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                                ReadOnly="True" SortExpression="ID" />
                            <asp:BoundField DataField="Fname" HeaderText="Fname" SortExpression="Fname" />
                            <asp:BoundField DataField="Mname" HeaderText="Mname" SortExpression="Mname" />
                            <asp:BoundField DataField="Lname" HeaderText="Lname" SortExpression="Lname" />
                            <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                            <asp:BoundField DataField="BDay" HeaderText="BDay" SortExpression="BDay" />
                            <asp:BoundField DataField="BPlace" HeaderText="BPlace" 
                                SortExpression="BPlace" />
                            <asp:BoundField DataField="Gender" HeaderText="Gender" 
                                SortExpression="Gender" />
                            <asp:BoundField DataField="ContactNum" HeaderText="ContactNum" 
                                SortExpression="ContactNum" />
                            <asp:BoundField DataField="Address" HeaderText="Address" 
                                SortExpression="Address" />
                        </Columns>
                        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" 
                            Wrap="True" />
                        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" Wrap="False" />
                        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                        <SortedAscendingCellStyle BackColor="#FDF5AC" />
                        <SortedAscendingHeaderStyle BackColor="#4D0000" />
                        <SortedDescendingCellStyle BackColor="#FCF6C0" />
                        <SortedDescendingHeaderStyle BackColor="#820000" />
                    </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                        ConflictDetection="CompareAllValues" 
                        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                        DeleteCommand="DELETE FROM [tblInfo] WHERE [ID] = @original_ID AND (([Fname] = @original_Fname) OR ([Fname] IS NULL AND @original_Fname IS NULL)) AND (([Mname] = @original_Mname) OR ([Mname] IS NULL AND @original_Mname IS NULL)) AND (([Lname] = @original_Lname) OR ([Lname] IS NULL AND @original_Lname IS NULL)) AND (([Age] = @original_Age) OR ([Age] IS NULL AND @original_Age IS NULL)) AND (([BDay] = @original_BDay) OR ([BDay] IS NULL AND @original_BDay IS NULL)) AND (([BPlace] = @original_BPlace) OR ([BPlace] IS NULL AND @original_BPlace IS NULL)) AND (([Gender] = @original_Gender) OR ([Gender] IS NULL AND @original_Gender IS NULL)) AND (([ContactNum] = @original_ContactNum) OR ([ContactNum] IS NULL AND @original_ContactNum IS NULL)) AND (([Address] = @original_Address) OR ([Address] IS NULL AND @original_Address IS NULL))" 
                        InsertCommand="INSERT INTO [tblInfo] ([Fname], [Mname], [Lname], [Age], [BDay], [BPlace], [Gender], [ContactNum], [Address]) VALUES (@Fname, @Mname, @Lname, @Age, @BDay, @BPlace, @Gender, @ContactNum, @Address)" 
                        OldValuesParameterFormatString="original_{0}" 
                        SelectCommand="SELECT * FROM [tblInfo] ORDER BY [ID]" 
                        UpdateCommand="UPDATE [tblInfo] SET [Fname] = @Fname, [Mname] = @Mname, [Lname] = @Lname, [Age] = @Age, [BDay] = @BDay, [BPlace] = @BPlace, [Gender] = @Gender, [ContactNum] = @ContactNum, [Address] = @Address WHERE [ID] = @original_ID AND (([Fname] = @original_Fname) OR ([Fname] IS NULL AND @original_Fname IS NULL)) AND (([Mname] = @original_Mname) OR ([Mname] IS NULL AND @original_Mname IS NULL)) AND (([Lname] = @original_Lname) OR ([Lname] IS NULL AND @original_Lname IS NULL)) AND (([Age] = @original_Age) OR ([Age] IS NULL AND @original_Age IS NULL)) AND (([BDay] = @original_BDay) OR ([BDay] IS NULL AND @original_BDay IS NULL)) AND (([BPlace] = @original_BPlace) OR ([BPlace] IS NULL AND @original_BPlace IS NULL)) AND (([Gender] = @original_Gender) OR ([Gender] IS NULL AND @original_Gender IS NULL)) AND (([ContactNum] = @original_ContactNum) OR ([ContactNum] IS NULL AND @original_ContactNum IS NULL)) AND (([Address] = @original_Address) OR ([Address] IS NULL AND @original_Address IS NULL))">
                        <DeleteParameters>
                            <asp:Parameter Name="original_ID" Type="Int32" />
                            <asp:Parameter Name="original_Fname" Type="String" />
                            <asp:Parameter Name="original_Mname" Type="String" />
                            <asp:Parameter Name="original_Lname" Type="String" />
                            <asp:Parameter Name="original_Age" Type="Int32" />
                            <asp:Parameter Name="original_BDay" Type="String" />
                            <asp:Parameter Name="original_BPlace" Type="String" />
                            <asp:Parameter Name="original_Gender" Type="String" />
                            <asp:Parameter Name="original_ContactNum" Type="String" />
                            <asp:Parameter Name="original_Address" Type="String" />
                        </DeleteParameters>
                        <InsertParameters>
                            <asp:Parameter Name="Fname" Type="String" />
                            <asp:Parameter Name="Mname" Type="String" />
                            <asp:Parameter Name="Lname" Type="String" />
                            <asp:Parameter Name="Age" Type="Int32" />
                            <asp:Parameter Name="BDay" Type="String" />
                            <asp:Parameter Name="BPlace" Type="String" />
                            <asp:Parameter Name="Gender" Type="String" />
                            <asp:Parameter Name="ContactNum" Type="String" />
                            <asp:Parameter Name="Address" Type="String" />
                        </InsertParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="Fname" Type="String" />
                            <asp:Parameter Name="Mname" Type="String" />
                            <asp:Parameter Name="Lname" Type="String" />
                            <asp:Parameter Name="Age" Type="Int32" />
                            <asp:Parameter Name="BDay" Type="String" />
                            <asp:Parameter Name="BPlace" Type="String" />
                            <asp:Parameter Name="Gender" Type="String" />
                            <asp:Parameter Name="ContactNum" Type="String" />
                            <asp:Parameter Name="Address" Type="String" />
                            <asp:Parameter Name="original_ID" Type="Int32" />
                            <asp:Parameter Name="original_Fname" Type="String" />
                            <asp:Parameter Name="original_Mname" Type="String" />
                            <asp:Parameter Name="original_Lname" Type="String" />
                            <asp:Parameter Name="original_Age" Type="Int32" />
                            <asp:Parameter Name="original_BDay" Type="String" />
                            <asp:Parameter Name="original_BPlace" Type="String" />
                            <asp:Parameter Name="original_Gender" Type="String" />
                            <asp:Parameter Name="original_ContactNum" Type="String" />
                            <asp:Parameter Name="original_Address" Type="String" />
                        </UpdateParameters>
                    </asp:SqlDataSource>
                </td>
            </tr>
            <tr>
                <td class="style2" nowrap="nowrap">
                    <asp:Label ID="Label3" runat="server" Text="Middle Name:"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtMname" runat="server"></asp:TextBox>
                </td>
                <td class="style1">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2" nowrap="nowrap">
                    <asp:Label ID="Label4" runat="server" Text="Last Name:"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtLname" runat="server"></asp:TextBox>
                </td>
                <td class="style1">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2" nowrap="nowrap">
                    <asp:Label ID="Label5" runat="server" Text="Age:"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                        ControlToValidate="txtAge" ErrorMessage="Number Only" Font-Size="Smaller" 
                        ForeColor="Red" ValidationExpression="^[0-9]*\.?[0-9]*$"></asp:RegularExpressionValidator>
                </td>
                <td class="style1">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2" nowrap="nowrap">
                    <asp:Label ID="Label6" runat="server" Text="Birth Date:"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtBdate" runat="server"></asp:TextBox>
                </td>
                <td class="style1">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2" nowrap="nowrap">
                    <asp:Label ID="Label7" runat="server" Text="Birth Place:"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtBplace" runat="server"></asp:TextBox>
                </td>
                <td class="style1">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2" nowrap="nowrap">
                    <asp:Label ID="Label8" runat="server" Text="Gender:"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtGender" runat="server"></asp:TextBox>
                </td>
                <td class="style1">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2" nowrap="nowrap">
                    <asp:Label ID="Label9" runat="server" Text="Contact No.:"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtContact" runat="server"></asp:TextBox>
                </td>
                <td class="style1">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2" nowrap="nowrap">
                    <asp:Label ID="Label10" runat="server" Text="Address:"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine"></asp:TextBox>
                </td>
                <td class="style1">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2" nowrap="nowrap">
                    <asp:Label ID="Label11" runat="server" Font-Size="Smaller" ForeColor="Red"></asp:Label>
                </td>
                <td>
                    <asp:Button ID="btnSave" runat="server" Text="Save" />
                    <asp:Button ID="btnEdit" runat="server" Text="Edit" />
                    <asp:Button ID="btnDelete" runat="server" Text="Delete" />
                    <asp:Button ID="btnClear" runat="server" Text="Clear" />
                </td>
                <td class="style1">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2" nowrap="nowrap">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td class="style1">
                    &nbsp;</td>
            </tr>
        </table>
        <br />
        <br />
    
    </div>
    </form>
</body>
</html>

 

File: web.config

 

<configuration>
    <connectionStrings>
        <add name="dbSQLConnectionString" connectionString="Data Source=ANONYMOUS-PC;Initial Catalog=dbSQL;Integrated Security=True"
            providerName="System.Data.SqlClient" />
        <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbSQL.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
        <compilation debug="false" strict="false" explicit="true" targetFramework="4.0" />
    </system.web>

</configuration>

 

File: Default.aspx.vb

Imports System.Data
Imports System.Data.Sql
Imports System.Configuration
Imports System.Data.SqlClient

Imports System.Web
Imports System.Web.UI
Imports System.Web.Security
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls.WebParts

Partial Class _Default
    Inherits System.Web.UI.Page

    Public cn As New SqlConnection
    Public cmd As New SqlCommand
    Public da As SqlDataAdapter
    Public dr As SqlDataReader
    Public ds As DataSet = New DataSet
    Public sql As String = Nothing
    'Public ConString As String = ("Data Source=ANONYMOUS-PC;Initial Catalog=dbSQL;Integrated Security=True") 'this is connected to the server
    Public ConString As String = ("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbSQL.mdf;Integrated Security=True;User Instance=True")

    Public Sub MyCn()
        If cn.State = Data.ConnectionState.Open Then cn.Close()
        cn.ConnectionString = ConString
        cn.Open()
    End Sub

    Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
        MyCn()
        GridView1.DataBind()
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        
        If txtFname.Text = "" Or txtMname.Text = "" Or txtLname.Text = "" Or txtAge.Text = "" Or txtBdate.Text = "" Or txtBplace.Text = "" Or txtGender.Text = "" Or txtContact.Text = "" Or txtAddress.Text = "" Then
            Label11.Text = "Insuficient Data!!"
        Else
            AddInfo()
            ClearTextBox(Me)
        End If

    End Sub

    Public Sub ClearTextBox(ByVal root As Control)
        For Each ctrl As Control In root.Controls
            ClearTextBox(ctrl)
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).Text = String.Empty
                lblID.Text = ""
                GridView1.DataBind()
            End If
        Next ctrl
    End Sub
    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        ClearTextBox(Me)
        Label11.Text = ""
    End Sub
    Private Function AddInfo()
        Try
            Dim xSQL As New System.Text.StringBuilder
            xSQL.AppendLine("INSERT INTO tblInfo")
            xSQL.AppendLine("(Fname,Mname,Lname,Age,BDay,BPlace,Gender,ContactNum,Address)")
            xSQL.AppendLine("VALUES")
            xSQL.AppendLine("(@fnam,@mnam,@lnam,@ag,@bda,@bpla,@gen,@cont,@add)")
            Using cn As New SqlConnection(ConString)
                cn.Open()
                Dim cmd As New SqlCommand(xSQL.ToString, cn)
                cmd.Parameters.AddWithValue("@fnam", txtFname.Text)
                cmd.Parameters.AddWithValue("@mnam", txtMname.Text)
                cmd.Parameters.AddWithValue("@lnam", txtLname.Text)
                cmd.Parameters.AddWithValue("@ag", txtAge.Text)
                cmd.Parameters.AddWithValue("@bda", txtBdate.Text)
                cmd.Parameters.AddWithValue("@bpla", txtBplace.Text)
                cmd.Parameters.AddWithValue("@gen", txtGender.Text)
                cmd.Parameters.AddWithValue("@cont", txtContact.Text)
                cmd.Parameters.AddWithValue("@add", txtAddress.Text)
                cmd.ExecuteNonQuery()
                GridView1.DataBind()
                Label11.Text = "Added Info Success!"
            End Using
            Return True
        Catch ex As Exception
            Label11.Text = "ERROR" & ex.Message.ToString
            Return False
        End Try
    End Function

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

        lblID.Text = GridView1.SelectedRow.Cells(1).Text
        txtFname.Text = GridView1.SelectedRow.Cells(2).Text
        txtMname.Text = GridView1.SelectedRow.Cells(3).Text
        txtLname.Text = GridView1.SelectedRow.Cells(4).Text
        txtAge.Text = GridView1.SelectedRow.Cells(5).Text
        txtBdate.Text = GridView1.SelectedRow.Cells(6).Text
        txtBplace.Text = GridView1.SelectedRow.Cells(7).Text
        txtGender.Text = GridView1.SelectedRow.Cells(8).Text
        txtContact.Text = GridView1.SelectedRow.Cells(9).Text
        txtAddress.Text = GridView1.SelectedRow.Cells(10).Text

    End Sub

    Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        EditData()
        ClearTextBox(Me)
    End Sub

    Private Function EditData()
        Try
            Dim xSQL As New System.Text.StringBuilder
            xSQL.AppendLine("UPDATE tblInfo SET")
            xSQL.AppendLine("Fname=@fnam, Mname=@mnam, Lname=@lnam, Age=@ag, BDay=@bda, BPlace=@bpla, Gender=@gen, ContactNum=@cont, Address=@add")
            xSQL.AppendLine("WHERE ID = " & lblID.Text.Trim)
            Using cn As New SqlConnection(ConString)
                cn.Open()
                Dim cmd As New SqlCommand(xSQL.ToString, cn)
                cmd.Parameters.AddWithValue("@fnam", txtFname.Text)
                cmd.Parameters.AddWithValue("@mnam", txtMname.Text)
                cmd.Parameters.AddWithValue("@lnam", txtLname.Text)
                cmd.Parameters.AddWithValue("@ag", txtAge.Text)
                cmd.Parameters.AddWithValue("@bda", txtBdate.Text)
                cmd.Parameters.AddWithValue("@bpla", txtBplace.Text)
                cmd.Parameters.AddWithValue("@gen", txtGender.Text)
                cmd.Parameters.AddWithValue("@cont", txtContact.Text)
                cmd.Parameters.AddWithValue("@add", txtAddress.Text)
                cmd.ExecuteNonQuery()
                GridView1.DataBind()
                Label11.Text = "Edited Info Success!"
            End Using
            Return True
        Catch ex As Exception
            Label11.Text = ex.Message.ToString
            Return False
        End Try
    End Function

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If lblID.Text = Nothing Then
            Label11.Text = "ID not Selected!"
        Else
            DeleteItem()
            ClearTextBox(Me)
        End If
    End Sub

    Private Function DeleteItem()
        Try
            Dim xSQL As New System.Text.StringBuilder
            xSQL.AppendLine("DELETE FROM tblInfo")
            xSQL.AppendLine("WHERE ID = " & lblID.Text.Trim)
            Using cn As New SqlConnection(ConString)
                cn.Open()
                Dim cmd As New SqlCommand(xSQL.ToString, cn)
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                GridView1.DataBind()
                Label11.Text = "Data Deleted Successfully!"
            End Using
            Return True
        Catch ex As Exception
            Label11.Text = ex.Message.ToString
            Return False
        End Try
    End Function
End Class

 

 

Output:

ASP.NET Sample Programs with Source Code