Get or fetch data from database and bind or display in gridview in ASP.NET(c# & vb.net)

Aman Sharma
0
This article will be helpful for beginners. In this article I have explained how to fetch data from database and display in gridview using ASP.NET(c# and VB.NET ).

Implementation: Follow following steps
1.     Create a database i.e. “Blog”.  Then create a table “Student_Info”.
Column Name
Datatype
Student_id
Int(Primary Key. So set Is Identity=True)
Student_Name
Varchar(500)
Age
int
Class
Varchar(50)

2.     Now insert some data in this table using “insert” command.

INSERT INTO student_info(Student_Name,Age,Class) VALUES('Akhsay',12, 'first')
INSERT INTO student_info(Student_Name,Age,Class) VALUES('Raghav',25, 'M.Sc')
INSERT INTO student_info(Student_Name,Age,Class) VALUES('aksht', 23, 'Bsc')
INSERT INTO student_info(Student_Name,Age,Class) VALUES('ankit', 23, 'MBA')
INSERT INTO student_info(Student_Name,Age,Class) VALUES('anil', 23, 'MCA')

3.     Create Connection: Now create connection in  webcofig file as given below.

<connectionStrings>
    <add name="con" connectionString="Data Source=localhost; Initial Catalog= Blog; Integrated Security=true;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

4.     ASP.Net Page design: Add gridview control on the webpage

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
            CellPadding="3" Width="270px">
            <Columns>
                <asp:BoundField DataField="Student_name" HeaderText="Student Name" />
                <asp:BoundField DataField="Age" HeaderText="Age" />
                <asp:BoundField DataField="Class" HeaderText="Class" />
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#007DBB" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#00547E" />
        </asp:GridView>


5.     ASP.NET code behind File using C#:

In code behind, add following code:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

Now Fetch data and bIND TO GRIDVIEW

public partial class gridview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind_grid();
        }
    }
    //Fetch data from database and bind to gridview
    public void Bind_grid()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        DataTable dt = new DataTable();
        SqlDataAdapter dataadapater = new SqlDataAdapter("Select * from student_info", con);
        dataadapater.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

VB.NET code behind file:
In code behind, add following code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Now Fetch data and bIND TO GRIDVIEW

Partial Public Class gridview
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        If Not IsPostBack Then
            Bind_grid()
        End If
    End Sub
    'Fetch data from database and bind to gridview
    Public Sub Bind_grid()
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)
        If con.State = ConnectionState.Open Then
            con.Close()
        End If
        con.Open()
        Dim dt As New DataTable()
        Dim dataadapater As New SqlDataAdapter("Select * from student_info", con)
        dataadapater.Fill(dt)
        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub
End Class

Demo: 
Use this code, create your own application and check result. You can use this code in any application by customizing it. If you have any doubt, you can ask by commenting in comment section.

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !