How to retrieve data from Access Database using oledb Connection in asp.net C#

1 comment
Create an application in which user has to display records in the Grid View Control from Table created in access data base (With the Help of Oledb Classes or Access Data Source Control).

Design Page::


HTML SOURCE CODE:


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="g1" runat="server" AutoGenerateColumns="true" Style="z-index: 100; left: 139px;
            position: absolute; top: 183px; width: 391px; height: 206px;"
            EnableModelValidation="True">
        </asp:GridView>
   
    </div>

    </div>
    </form>
</body>
</html>

CS CODE


using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\Users\\VIJAY\\Desktop\\lab91\\App_Data\\vijay.mdb;");        OleDbCommand cmd = new OleDbCommand();        OleDbDataAdapter da = new OleDbDataAdapter();        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select * from t1";        cmd.Connection = con;        da.SelectCommand = cmd;        DataTable dt = new DataTable();        con.Open();        da.Fill(dt);        con.Close();        //Response.Write(dt.Rows.Count + "");        g1.DataSource = dt;        g1.DataBind();
    }
}


Database:
OUTPUT:


This is the example in which you can make oledb connection with access database and retrieve data from access database and bind them in gridview.

1 comment:

  1. Have a look at here
    Using Access Database with c# -- geeksprogrammings.blogspot.com
    http://geeksprogrammings.blogspot.com/2013/10/connect-access-database-with-c.html

    For Deletion Record
    http://geeksprogrammings.blogspot.com/2013/09/delete-record-from-access-database.html

    ReplyDelete