Fetch Data from Database using .net and view on a Grid | YourSite

Fetch Data from Database using .net and view on a Grid

MS Dot NET 1059 views

In this blog we will see how I can fetch the data from the database and view on a data grid

File Name: ViewCabDetails.aspx

This is a .aspx page. where we are using out grid

Note: User Interface Layer


<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="ViewCabDetails.aspx.cs" Inherits="TaxiManagementSystem.ViewCabDetails" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainContent" runat="server">
<form runat="server">
    <asp:GridView ID="GridView1Awesome" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
          <asp:BoundField DataField="BookingReferenceNumber1" HeaderText="Booking ReferenceNumber " />
            <asp:BoundField DataField="PassengerName1" HeaderText="Passenger Name " />
            <asp:BoundField DataField="FromLocation1" HeaderText="From Location " />
            <asp:BoundField DataField="ToLocation1" HeaderText="To Location " />
            <asp:BoundField DataField="DepartureTime1" HeaderText="Departure Date Time " />
            <asp:BoundField DataField="DistanceinKm1" HeaderText="Distance in Km " />
            <asp:BoundField DataField="EstimatedAmount1" HeaderText="Estimated Amount " />
            <asp:BoundField DataField="CabType1" HeaderText="Cab Type " />
        </Columns>
                <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>

    </form>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
</asp:Content>


File Name: ViewCabDetails.aspx.cs

This is a .aspx page. where we are using out grid

Note: User Interface Layer (Code Behind file)


using blTaxiManagementSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ulTaxiManagementSystem;

namespace TaxiManagementSystem
{
    public partial class ViewCabDetails : System.Web.UI.Page
    {

        

        protected void Page_Load(object sender, EventArgs e)
        {
            bindData();
        }
        public void bindData()
        {
            BookCabDBOperation cabBookDB = new BookCabDBOperation();
            List<BookCabClass> cabbookList;
            cabbookList = cabBookDB.GetCabDetails();
            GridView1Awesome.DataSource = cabbookList;
            GridView1Awesome.DataBind();
        }
    }
}

File Name: BookCabDBOperation.cs

You can save this function or method inside the Data access layer in, thus is the data base operation file

class name BookCabDBOperation

Note: Data Access Layer

cab.BookingReferenceNumber1 = Convert.ToInt32(reader["BookingReferenceNumber"]);

In the above code BookingReferenceNumber1 this is a properties of the Class and BookingReferenceNumber this is a database feild name


        public List<BookCabClass> GetCabDetails()
        {
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd = new SqlCommand("select_cab_details_1637935", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader reader = cmd.ExecuteReader();
            List<BookCabClass> cabDetails = new List<BookCabClass>();
            while (reader.Read())
            {
                BookCabClass cab = new BookCabClass();
                cab.BookingReferenceNumber1 = Convert.ToInt32(reader["BookingReferenceNumber"]);
                cab.PassengerName1 =  reader["PassengerName"].ToString();
                cab.FromLocation1 = reader["FromLocation"].ToString();
                cab.ToLocation1 = reader["ToLocation"].ToString();
                cab.DepartureTime1 = reader["DepartureTime"].ToString();
                cab.DistanceinKm1 = reader["DistanceinKm"].ToString();
                cab.EstimatedAmount1 = reader["EstimatedAmount"].ToString();
                cab.CabType1 = reader["CabType"].ToString();

                cabDetails.Add(cab);
            }

            return cabDetails;

        }



This is out Properties class

Note: This is my code for the class and properties details


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace blTaxiManagementSystem
{
    public class BookCabClass
    {

        int BookingReferenceNumber;
        string PassengerName;
        string FromLocation;
        string ToLocation;
        string DepartureTime;
        string DistanceinKm;
        string EstimatedAmount;
        string CabType;

        public BookCabClass()
        {

        }

        public BookCabClass(string passengerName, string fromLocation, string toLocation, string departureTime, string distanceinKm, string estimatedAmount, string cabType)
        {
            // BookingReferenceNumber1 = BookingReferenceNumber;
            PassengerName = passengerName;
            FromLocation = fromLocation;
            ToLocation = toLocation;
            DepartureTime = departureTime;
            DistanceinKm = distanceinKm;
            EstimatedAmount = estimatedAmount;
            CabType = cabType;
        }

        public int BookingReferenceNumber1 { get => BookingReferenceNumber; set => BookingReferenceNumber = value; }
        public string PassengerName1 { get => PassengerName; set => PassengerName = value; }
        public string FromLocation1 { get => FromLocation; set => FromLocation = value; }
        public string ToLocation1 { get => ToLocation; set => ToLocation = value; }
        public string DepartureTime1 { get => DepartureTime; set => DepartureTime = value; }
        public string DistanceinKm1 { get => DistanceinKm; set => DistanceinKm = value; }
        public string EstimatedAmount1 { get => EstimatedAmount; set => EstimatedAmount = value; }
        public string CabType1 { get => CabType; set => CabType = value; }
    }
}


SQL Procedure



CREATE PROCEDURE select_cab_details_1637935   
AS   
BEGIN  
SELECT * FROM EtaxiSystem_1937935   
END


🚀 More Blogs You Might Like

Explore more articles and keep learning

WB Gram Panchayat Mock Test 2026 | Free Practice Test for Exam Preparation
youth-society
WB Gram Panchayat Mock Test 2026 | Free Practice Test for Exam Preparation

Practice free WB Gram Panchayat Mock Test online for 2026 exam preparation. Improve your speed, accuracy, and ...

👁 4 2026-06-14
Read More →
What is Bounce Rate in SEO? Complete Guide for Beginners
search-engine-optimization
What is Bounce Rate in SEO? Complete Guide for Beginners

Learn what bounce rate is in SEO, how it is calculated, why it matters, common causes of high bounce rates, an...

👁 28 2026-05-24
Read More →
Comprehensive Interviewer Guide - Detailed Article
skill
Comprehensive Interviewer Guide - Detailed Article

Learn how to conduct effective interviews with this comprehensive interviewer guide. Explore hiring strategies...

👁 43 2026-05-22
Read More →
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)
skill
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)

Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)...

👁 39 2026-05-19
Read More →