Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

ASP.NET Web Page Data Base:


In ASP.NET, you can connect your web application to a database to store and retrieve data. Let's create a simple example of connecting to a SQL Server database and performing basic CRUD (Create, Read, Update, Delete) operations using ASP.NET Web Forms.

Example: Connecting to a SQL Server Database in ASP.NET Web Forms

  1. Set Up Your Project:

    • Open Visual Studio and create a new ASP.NET Web Application project.
    • Choose Web Forms as the project template.
  2. Add a Database to Your Project:

    • Right-click on your project in Solution Explorer.
    • Select "Add" > "New Item".
    • Choose "SQL Server Database" and name it SampleDB.mdf.
    • This will create a local SQL Server database file in your project.
  3. Design Your Web Page:

    • Open Default.aspx in the Visual Studio designer.
    • Add HTML elements and ASP.NET server controls to display data from the database and interact with it.
  4. Example

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

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>ASP.NET Web Page with Database</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    </form>
    </body>
    </html>

  5. Write Server-side Code to Retrieve Data from the Database:
    • Open Default.aspx.cs (code-behind file) in Visual Studio.
    • Write C# code to connect to the database, execute a SQL query, and bind the results to the GridView control.
  6. Example

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

    namespace YourProjectName
    {
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    // Connect to the database
    string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\SampleDB.mdf;Integrated Security=True";
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
    // Define the SQL query
    string query = "SELECT * FROM Users";

    // Create a SqlCommand object
    using (SqlCommand command = new SqlCommand(query, connection))
    {
    // Open the database connection
    connection.Open();

    // Execute the SQL query and retrieve data into a DataTable
    DataTable dt = new DataTable();
    using (SqlDataReader reader = command.ExecuteReader())
    {
    dt.Load(reader);
    }

    // Bind the DataTable to the GridView control
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    }
    }
    }
    }
    }

  7. Run Your Application:
    • Build and run your ASP.NET web application.
    • Navigate to the Default.aspx page in your browser.
    • You should see the GridView control populated with data retrieved from the database.

That's it! You've created a simple ASP.NET web page connected to a SQL Server database. This example demonstrates the basic concept of accessing data from a database and displaying it in a web application using ASP.NET Web Forms.


Advertisement
Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java