Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting to Access DB via web.config

Status
Not open for further replies.

guestAsh

Technical User
Feb 27, 2004
65
GB
Hiya -i'm using the following code for a button_click event, and works fine, but i just thought the connection string should be referencing the web.config file, not sure how to do this any pointers?

Code:
    protected void Button1_Click(object sender, EventArgs e)
    {

OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|DB1.mdb;User Id=admin;Password=;");
              OleDbCommand cmd = new OleDbCommand();
              cmd.CommandText = "INSERT INTO Xref_Roles_Users (Role, Users) VALUES ('" + Request.Form["DropDownList1"] + "','"+Request.Form["ListBox1"]+"')";
              cmd.Connection = cn;
              cmd.Connection.Open();
              cmd.ExecuteNonQuery();
              cmd.Connection.Close();
              cmd.Dispose();
              cn.Dispose();
    }

and the web.config file:
Code:
<appSettings/>
    <connectionStrings>
<add name="DBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\DB1.mdb;Persist Security Info=True"
            providerName="System.Data.OleDb" />
      </connectionStrings>
    <system.web>....
 
along with managing the connection string from the config file there are some other enhancements I would recommend. I wrote a FAQ on the subject. a link to the FAQ is in my signature below. if you have any questions, please ask.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top