0

I am trying to connect my MS Access Db with my asp.net website. When I run my code(on localhost only) it does not show any error or exception but it just wait for server to response. I am following code :

 string connection = "Provider =Microsoft.ACE.OLEDB.12.0; datasource=I:/Adittya/test.accdb";

        string query = "select * from test";
        OleDbConnection con = new OleDbConnection(connection);
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = query;
        cmd.CommandType = CommandType.Text;
        OleDbDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);

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

One more thing my DB is in my pen-drive whose location is static.

Do I have problem with my code or we cannot connect to DB present in pen-drive.

3
  • Mr. Janjua kept editing his answer based on my suggestions to you. and then submitted the working copy. he edited thrice. I think, you need to do justice when marking the answer. Thanks
    – Shezi
    Jul 7, 2012 at 5:44
  • read my below comment. You have not said a single time that the connection is missing etc. Read your comments again yourself.Your focus is only on the connection string. Thanks. Jul 7, 2012 at 5:48
  • @AtharAnis yes you talk about command object but I have not seen your comments, bcz they are hidden. I created a test db on MS Access and ran her code on my PC and found the exact errors. Jul 7, 2012 at 5:52

3 Answers 3

4

There is a problem in your code, You have not set the connection to the command object and also not open the connection.Below code is working for me. Try this

    string connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Test1.accdb";

    string query = "select * from [Table]";
    OleDbConnection con = new OleDbConnection(connection);
    OleDbCommand cmd = new OleDbCommand();     
    cmd.CommandText = query;
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    con.Open();
    OleDbDataReader dr = cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(dr);
    con.Close();
    testGrid.DataSource = dt;
    testGrid.DataBind();

or Try this solution Using SqlDataSource

Example:I have created a test Acess database and I used Sql datasource it is working fine. Try this solution

<connectionStrings>       
    <add name="ConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Test1.accdb"
        providerName="System.Data.OleDb" />
  </connectionStrings>
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$     ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>
 <asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" runat="server"></asp:GridView>

It is working fine for me.

5
  • why are u editing your answers based on my suggestions? u just added the command connection after i suggested it in my answer. How unfair is that?
    – Shezi
    Jul 7, 2012 at 5:42
  • @AtharAnis I had only seen you connection string but it is not the answer. You have not said that the Connection is not open or set. I ran this code on my PC and When I debug I found that the Connection command is not set to the command object and Connection is also not open. I have not read your any comment except the connection string answer. And i'm editing my anser bcz I have done it on my PC and and I have to copy the code from there Jul 7, 2012 at 5:47
  • I have tried your code too, it is also not working when I am running the code but when I check it on visual studio it works fine there.
    – user1465587
    Jul 7, 2012 at 6:16
  • Both of the above codes worked, I test it on my PC using Visual Studio and MS Access Jul 7, 2012 at 6:42
  • Voted up for efforts and a detailed answer
    – Shezi
    Jul 7, 2012 at 7:48
1

try your data source as

datasource=I:\\Adittya\\test.accdb
10
  • Also try to add Persist Security Info = False in your connection string
    – Shezi
    Jul 7, 2012 at 5:16
  • and Jet OLEDB:Database Password=yourpassword; too
    – Shezi
    Jul 7, 2012 at 5:19
  • I tried your solution but gain no success. I am still on previous position.
    – user1465587
    Jul 7, 2012 at 5:20
  • ok , tell me how are u creating ur connection string? you typed it ? or did you use a wizard to copy it? I am sure it is your connection string thats causing problem
    – Shezi
    Jul 7, 2012 at 5:22
  • Your connection string should be like, "Provider =Microsoft.ACE.OLEDB.12.0; datasource=I:\\Adittya\\test.accdb"; Persist Security Info = false;
    – Shezi
    Jul 7, 2012 at 5:24
0

use this

"Provider=Microsoft.Jet.OLEDB.4.0; " + 
     "Data Source=" + Server.MapPath("Database/TestDB.mdb"))

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.