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

Rumman Ansari   2019-03-04   Student   MS dot NET > fetch-data-from-database   777 Share

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