user3834541
user3834541

Reputation: 161

Connect to Access database using C# Web.config file

I am using MS Access 2013 Database name is "comm" and Password is "xyz@12345"

Web.config Connection String :

<add name="commconn" connectionString="Provider=Microsoft.ACE.OLEDB.12.0; Data Source=E:/ee/comm.accdb; Jet OLEDB:Database Password=xyz@12345;" />

When i run my WebSite it gives an Error only where my DropDownList Bind Data and the Error is this "Not a valid password."

I bind my DropDownList using this code :

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="AccessDataSource1" DataTextField="share_amt" DataValueField="ID">
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/comm.accdb" SelectCommand="SELECT * FROM [share_amt] ORDER BY [ID]">
</asp:AccessDataSource>

I am using MS Access 2013 Database and ASP.Net with C#.

Upvotes: 1

Views: 6407

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123839

It appears that the crux of your problem is:

  1. In Web.config you've created a data connection named commconn for a password-protected Access database named "comm.accdb", but
  2. You've tried to use an AccessDataSource to populate your DropDownList, and that AccessDataSource refers directly the Access database via the DataFile= argument; the commconn data connection has nothing to do with it.

Furthermore, the MSDN article

Retrieving Data Using the AccessDataSource Web Server Control

says:

Note

The AccessDataSource will not connect to an Access database that is password-protected; to retrieve data from a password-protected Access database, use the SqlDataSource control.

So, for a password-protected Access database named "comm.accdb" in your App_Data folder you would need a <connectionStrings> entry like this in Web.config:

<add name="commConnectionString" 
    connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\comm.accdb;Jet OLEDB:Database Password=xyz@12345"
    providerName="System.Data.OleDb" />

along with something like this on your .aspx page:

<asp:SqlDataSource 
    ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:commConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:commConnectionString.ProviderName %>" 
    SelectCommand="SELECT * FROM [share_amt] ORDER BY [ID]">
</asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" 
    DataSourceID="SqlDataSource1" DataTextField="share_amt" DataValueField="ID">
</asp:DropDownList>

Upvotes: 5

Related Questions