Wednesday, 4 June 2014

Binding Gridview By Access DataBase Using C#.Net in Asp.Net

6/04/2014 - By Pranav Singh 0

Whenever we create an asp.net application on that we always come in a situation where we needed to display the data in the tabular form. So asp.net has provided so many controls like GridView, DataGrid for representing the data in tabular format.
So in this article i will show you how you can bind a grid view using C#.Net and VB.Net in asp.net.

Now for this article first we will create table in our data base.



Now add the Access data base in your App_Data folder.

Now we will create a new asp.net application and add a .aspx form . Now in this form we will a GridView Control.

Now from Edit template option we will add Bind column in our grid view.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Binding Gridview By Access DataBase Using C#.Net in Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <h3>Binding Gridview By Access DataBase Using C#.Net in Asp.Net</h3>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        EmptyDataText="There are no data records to display." BorderStyle="Solid">
        <Columns>
            <asp:BoundField DataField="author_name" HeaderText="NAME" />
            <asp:BoundField DataField="publisher_name" HeaderText="PUBLISHER NAME" />
            <asp:BoundField DataField="publication_year" HeaderText="PUBLISH YEAR" />
            <asp:BoundField DataField="retail_price" HeaderText="PRICE" />
        </Columns>
        <HeaderStyle BackColor="#66CCFF" />
    </asp:GridView>
    </form>
</body>
</html>

Now in your .cs page add the below mention code.

C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
namespace ProjectDemo_Asp.et
{
    public partial class Default : System.Web.UI.Page
    {
        public string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\bookstore.mdb;Persist Security Info=False;";
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable _objdt = new DataTable();
            _objdt = GetDataFromDataBase();
            if (_objdt.Rows.Count > 0)
            {
                GridView1.DataSource = _objdt;
                GridView1.DataBind();
            }
        }

        /// <summary>
        /// Function for binding retribing the data from database
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataFromDataBase()
        {
            DataTable _objdt = new DataTable();
            string querystring = "select * from Books;";
            OleDbConnection _objcon=new OleDbConnection(connectionstring);
            OleDbDataAdapter _objda = new OleDbDataAdapter(querystring, _objcon);
            _objcon.Open();
            _objda.Fill(_objdt);
            return _objdt;
        }
    }
}


VB.Net 

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data.OleDb
Imports System.Data
Namespace ProjectDemo_Asp.et
    Partial Public Class [Default]
        Inherits System.Web.UI.Page
        Public connectionstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\bookstore.mdb;Persist Security Info=False;"
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim _objdt As New DataTable()
            _objdt = GetDataFromDataBase()
            If _objdt.Rows.Count > 0 Then
                GridView1.DataSource = _objdt
                GridView1.DataBind()
            End If
        End Sub

        ''' <summary>
        ''' Function for binding retribing the data from database
        ''' </summary>
        ''' <returns></returns>
        Public Function GetDataFromDataBase() As DataTable
            Dim _objdt As New DataTable()
            Dim querystring As String = "select * from Books;"
            Dim _objcon As New OleDbConnection(connectionstring)
            Dim _objda As New OleDbDataAdapter(querystring, _objcon)
            _objcon.Open()
            _objda.Fill(_objdt)
            Return _objdt
        End Function
    End Class
End Namespace


Now we are ready to view the result.


DOWNLOAD 

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top