Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
give me example how to connect to access database to asp.net(c#) with clear code
Posted
Comments
Anil Honey 206 26-Dec-11 6:26am    
you know the way how to take connectionstring of msacess?
kalyan1989 26-Dec-11 7:25am    
donno

Try this code gridview connect ms access database

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MSACCESSGV.aspx.cs" Inherits="MSACCESSGV" %>
<!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>Test MS ACCESS DATABASE CONNECTION</title>
</head>

<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="gvArticle" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Id" />
<asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Title" />
<asp:BoundField DataField="Published" HeaderText="Published" SortExpression="Visit" />
<asp:BoundField DataField="Modified" HeaderText="Modified" SortExpression="Modified" />
</Columns>
</asp:GridView>

</div>
</form>

</body>
</html>


Now you need to write code in server side to bind GridView gvArticle. First make connection string for your MS Access Database. And then read data from MS Access database. Complete server side code is given below:
using System;
using System.Data;
using System.Web.UI;
using System.Data.OleDb;

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

if (!IsPostBack)
{
string sFilePath = Server.MapPath("MSACCESS.mdb");
DataTable dt;
OleDbConnection Conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFilePath + ";");

using (Conn)
{
Conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM ARTICLE", Conn);
OleDbDataAdapter oDA = new OleDbDataAdapter(cmd);
dt = new DataTable();
oDA.Fill(dt);
}

gvArticle.DataSource = dt;
gvArticle.DataBind();

}
}
}
 
Share this answer
 
Comments
Anil Honey 206 26-Dec-11 6:31am    
Try this it works.......
kalyan1989 26-Dec-11 7:22am    
yeah,i have gone through this code
but it shows there is no datatable in inbuilt one
 
Share this answer
 
Go look at Google: http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=connect+to+access+database+to+asp.net(c%23)[^]
It gives loads of examples.

In future, please do at least basic research yourself, and stop wasting your time and ours.
 
Share this answer
 
Use OleDB Provider for connecting with Access database:
C#
Provider=Microsoft.Jet.OLEDB.4.0

=>Use this in Connection String and use oledbconnection defining your string.
 
Share this answer
 
v2
Comments
kalyan1989 26-Dec-11 7:23am    
can u tell me with clear example

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900