i want to connect asp.net 2005 with access .
now i need for the code do do this connection

Dani AI

Generated

A few practical, no-fluff points to get a working insert from ASP.NET 2005 into an Access file (building on the hints from , and ):

Keep the .mdb in the site (App_Data) and put the connection string in web.config (read it with ConfigurationManager). If you prefer a runtime-built path, use Server.MapPath to point to App_Data. Make sure the IIS process identity (ASPNET / NETWORK SERVICE / app pool identity) has modify rights to the folder and file.

Example C# insert using parameters (won't repeat the exact provider strings already posted here — put those in web.config under the name "AccessConn"):

using System.Data.OleDb;
using System.Configuration;

public void InsertCustomer(string name, string email)
{
    string connStr = ConfigurationManager.ConnectionStrings["AccessConn"].ConnectionString;
    using (var conn = new OleDbConnection(connStr))
    {
        conn.Open();
        using (var cmd = conn.CreateCommand())
        {
            // OleDb for Access uses positional ? parameters; add them in the same order
            cmd.CommandText = "INSERT INTO Customers (Name, Email) VALUES (?, ?)";
            cmd.Parameters.Add(new OleDbParameter("p1", OleDbType.VarWChar, 100) { Value = name });
            cmd.Parameters.Add(new OleDbParameter("p2", OleDbType.VarWChar, 255) { Value = email });
            cmd.ExecuteNonQuery();
        }
    }
}

Troubleshooting and cautions: always use parameters (no string concatenation) to avoid quoting bugs and injection. If you see provider errors on a 64-bit server, that is a platform/driver issue (run the app 32-bit or install the appropriate Access engine). If you get "file is locked" errors, ensure Access isn't open with an exclusive lock and the IIS identity has the right permissions. For anything beyond a few concurrent users, consider SQL Server Express for better scalability and safety.

Recommended Answers

All 4 Replies

Create a variable Like x
Then
x="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\office.mdb"
Note :C:\Program Files\office.mdb By replacing it put your datasource And then try this

thnx for reply :D

what about the full code & the veriables when i want to do insert and the code to execute the query ???

There are two functions for generating the conn string depending on the version of access, which can be determined by the file extension or various other ways.

public static string BuildAccessConnectionString(string Filename, string Username, string Password, string DatabasePassword)
    {
      return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';User Id={1};Password={2};Jet OLEDB:Database Password={3};",
                                   Filename.Replace("'", "''"),
                                   Username,
                                   Password,
                                   DatabasePassword);
    }
    public static string BuildAccess2007ConnectionString(string Filename, string DatabasePassword)
    {
      return string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='{0}';Persist Security Info=False;Jet OLEDB:Database Password={1};",
                                 Filename.Replace("'", "''"),
                                 DatabasePassword);
    }
    private void simpleButton3_Click(object sender, EventArgs e)
    {
      string connStr = BuildAccessConnectionString(@"C:\data\CustList01.mdb", string.Empty, string.Empty, string.Empty);
      DataTable result = new DataTable();
      using (OleDbConnection conn = new OleDbConnection(connStr))
      {
        conn.Open();
        using (OleDbCommand cmd = new OleDbCommand(@"Select * From [CustList01]", conn))
        {
          using (OleDbDataReader dr = cmd.ExecuteReader())
          {
            result.Load(dr);
          }
        }
        conn.Close();
      }
      //Work with table
      MessageBox.Show(result.Rows.Count.ToString());
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.