Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 10th, 2005, 04:09 PM
Registered User
 
Join Date: Jan 2005
Location: , , Ireland.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to CHen
Default Connecting ASP.Net to Access

I'm doing a project which is using asp.net and access.
Just wondering if anyone has a clue how I'd connect them as I'm having trouble with it.
Thanks a million,
Clare

Clare Hennessy
 
Old January 10th, 2005, 06:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

1. Create a WebForm
2. Add the following using directive to the top of the page to reference the OleDb versions of ADO.NET objects: using System.Data.OleDb
3. Add the following to the WebForms Page_Load event:

(I use C# but the VB.NET code is virtually identical)

private void Page_Load(object sender, System.EventArgs e)
        {
            //Set connection string.
            string connectString = "Provider=Microsoft.Jet.OLEDB.4.0;"
                + "Data Source=C:\\Northwind.mdb";

            //Pass connection string to OleDbConnection object.
            OleDbConnection cn = new OleDbConnection(connectString);

            //Open connection.
            cn.Open();

            //Pass SQL statement to OleDbCOmmand object.
            string selectString = "SELECT CategoryID, CategoryName FROM Categories";
            OleDbCommand cmd = new OleDbCommand(selectString,cn);

            //Execute an OleDbDataReader
            OleDbDataReader reader = cmd.ExecuteReader();

            //Bind DataReader to DataGrid
            DataGrid1.DataSource = reader;
            DataGrid1.DataBind();

            //Close reader and connection.
            reader.Close();
            cn.Close();
        }

That's the bare bones. Just binds a forward-only, read-only recordset to a DataGrid web control.

HTH,

Bob

 
Old January 10th, 2005, 09:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Should probably mention that you want to avoid hard-coding connection strings in individual WebForms as I did above. Its pretty much standard practice to maintain them in your apps Web.config file. To do that, first add another using directive to the code-behind class of your WebForm: using System.Configuration. Then in the <configuration> section of your Web.config file, create the followiing key:

<configuration>
    <appSettings>
        <add key="ConnectionString"
             value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Northwind.mdb"/>
    </appSettings>
 <configuration>

Now you can change the line in your WebForms Page_Load event that sets the connection string from:

//Set connection string.
string connectString = "Provider=Microsoft.Jet.OLEDB.4.0;"
    + "Data Source=C:\\Northwind.mdb";

to:

//Set connection string.
string connectString = ConfigurationSettings.AppSettings["ConnectionString"];

 
Old January 11th, 2005, 11:40 AM
Registered User
 
Join Date: Jan 2005
Location: , , Ireland.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to CHen
Default

Hi there.
Thanks a million for that Bob!

I've tried using that code but I'm getting an error when running it in asp.net like the following:

The type or namespace 'OleDbConnection' could not be found ( are you missing a using directive or an assembly ref?)

Any ideas?
Thanks alot,
Clare.

Clare Hennessy
 
Old January 11th, 2005, 12:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Clare,

First be sure the Page_Load event code I posted is added to the code-behind class of your WebForm. Don't use it as in-line script in your asp.net page.

Second, ASP.NET Web Apps add the following default using directives to the top of the code-behind class:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

You need to add the following to this list:

using System.Data.OleDb

This is the namespace where the OleDbConnection object lives.

There is an outside chance that you are missing an assembly reference to the System.Data.dll assembly where ADO.NET lives. Open your project's References folder and look for a reference to System.Data. If it isn't there, add it by right-clicking on the folder and selecting System.Data.dll from the Add References dialog.

HTH,

Bob

 
Old February 3rd, 2005, 02:03 AM
Authorized User
 
Join Date: Feb 2005
Location: Kolkata, West Bengal, India.
Posts: 14
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi, I am very new to programming world.Doing a project work based on Visual C# and ASP.NET. I had been trying to connect asp.net with microsoft access 2000 for last week itself.Thanks to the Wrox team for providing the free support in the net.

somissac




Similar Threads
Thread Thread Starter Forum Replies Last Post
Connecting to Access 20007 DB with vb.net JohnBoy VB.NET 1 May 21st, 2008 01:26 PM
problem connecting .net with access in 64bit darkelf ADO.NET 1 September 26th, 2006 05:26 PM
Connecting database using ms access and vb.net adil_farooq Access 0 October 27th, 2005 11:54 PM
Connecting ASP.NET with Access CHen Access 5 January 30th, 2005 01:52 AM
Connecting to Access DB using Visual Basic .NET Lieth Access 6 September 26th, 2003 06:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.