adonet
  1. adonet-webforms-example

WebForms Example - (ADO.NET Examples)

WebForms is a popular web application development framework based on ASP.NET. It provides a set of tools and components for creating interactive web pages and applications. ADO.NET is a part of the .NET platform that provides a set of classes for accessing and manipulating data from various data sources.

In this example, we will create a simple web application that uses ADO.NET to connect to a database, retrieve data, and display the data on a web page.

Syntax

WebForms applications are built using C# or VB.NET programming languages and use the ASP.NET class library and the ADO.NET class library to access data. A typical WebForms application consists of server-side scripts, HTML pages, and configuration files.

Example

Here's an example of WebForms application that displays a list of users stored in a database:

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

<html>
<head>
    <title>WebForms Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h2>List of Users:</h2>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="UserID" HeaderText="User ID" />
                    <asp:BoundField DataField="UserName" HeaderText="User Name" />
                    <asp:BoundField DataField="Email" HeaderText="Email Address" />
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

In the code-behind file (e.g., Default.aspx.cs), you need to define the database connection and query the database to retrieve data:

protected void Page_Load(object sender, EventArgs e)
{
    string connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;";
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand("SELECT * FROM Users", connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        GridView1.DataSource = reader;
        GridView1.DataBind();
        reader.Close();
    }
}

Output

When you run this web form, it displays a table with the user ID, user name, and email address for each user in the database.

Explanation

This WebForms application uses ADO.NET to connect to a SQL Server database, retrieve data using a SQL query, and display the data in a GridView control. The connection string specifies the database server, name, username, and password. The query retrieves all the user records from the Users table in the database. The data is then bound to the GridView control for display on the web page.

Use

WebForms is used to build various types of web applications, including:

  • E-commerce websites: Websites that allow users to purchase products and services online.
  • Social networking websites: Websites that allow users to connect and share information with others.
  • Content management system (CMS): Applications that allow users to publish, manage, and organize content.
  • Web applications: Web applications with business logic and complex database interactions.
  • And many more.

Important Points

  • WebForms is a popular web application development framework based on ASP.NET.
  • ADO.NET is a part of the .NET platform that provides a set of classes for accessing and manipulating data from various data sources.
  • This example uses ADO.NET to connect to a SQL Server database, retrieve data, and display the data in a GridView control.
  • WebForms applications are built using C# or VB.NET programming languages and use the ASP.NET class library and the ADO.NET class library.

Summary

In this page, we discussed the basics of WebForms and ADO.NET. We covered the syntax, example, output, explanation, use, and important points of WebForms and ADO.NET. This example demonstrated how to use ADO.NET to connect to a database, retrieve data, and display the data in a WebForms application. WebForms and ADO.NET are powerful tools for building interactive web applications with database connectivity.

Published on: