ASP.NET C# database connection in external file

pts.
Tags:
ASP.NET
C
Hi, Im a newbie to .net and c# so forgive me if im asking a stupid question. Im trying to get up to speed asap, ive figured out how to connect to a database - but i was wondering how do i store the connection in an external file so i can just simply import it into the asp.net page instead of manually writing the connection on each page. Can this be done? if so how? If have enclosed my orginal code if that helps. :::::::::::::: CODE ::::::::::::::::::::::::::::: <%@ Page Language="C#" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.OleDb" %> <script runat="server"> // function to create variables to hold information that we need void Page_Load(Object sender, EventArgs e) { OleDbConnection objConnection; OleDbDataAdapter objCommand; String strConnect; String strCommand; DataSet DataSet1 = new DataSet(); // strConnect contains database connection 'Microsoft JET.OLEDB' for MS ACCESS, connection string will change upon db used strConnect = @"Provider=Microsoft.Jet.OLEDB.4.0;"; strConnect += @"Data SOurce=D:uniEnterprise Web Applicationsassignmentdbewa.mdb;"; strConnect += @"Persist Security Info=false;"; // the database query strCommand = "Select * from Type"; // next 3 lines :: take above code and create 2 objects 'Connection' & 'DataSet1' and populate DataSet1 object with data read from the DB objConnection = new OleDbConnection(strConnect); objCommand = new OleDbDataAdapter(strCommand, objConnection); objCommand.Fill(DataSet1, "Type"); // The last 2 lines tell datagrid(HTML section) to use DataSet1 as its source DataGrid1.DataSource=DataSet1.Tables["Type"].DefaultView; DataGrid1.DataBind(); } Appreciate any help Thanks in advance Andy

Answer Wiki

Thanks. We'll let you know when a new response is added.

Put this in your code:

System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();

this.sqlConnection1.ConnectionString = ((string)(configurationAppSettings.GetValue(“sqlConnection1.ConnectionString”, typeof(string))));

Put this in your Web.config (or app.config as appropriate) under configuration->appSettings which is a sibling of system.web:

<appSettings>
<add key=”sqlConnection1.ConnectionString” value=”integrated security=SSPI;data source=server;persist security info=False;initial catalog=db” />
</appSettings>

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following