How to move this line to web.config?

S

Shapper

Hello,

I have a dataset in an ASP.NET/VB web page:

Function dsNews() As System.Data.DataSet
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;
Ole DB Services=-4; Data Source=C:\Database\myDatabase.mdb"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.OleDb.OleDbConnection(connectionString)
Dim queryString As String = "SELECT [t_news].* FROM [t_news]"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New
System.Data.OleDb.OleDbDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function

Shouldn't I move the connection,
connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB
Services=-4; Data Source=C:\Database\myDatabase.mdb"

to my Web.config file?

Can someone tell me how can I do this?

Thank You,
Miguel
 
L

Lars-Erik Aabech

Hi!

There are several recommended ways to do this. One of them (at least the
initial step towards a better workday) is to put connectionstring info in
web.config. You can do this by adding the element <appSettings> to your
web.config between the <configuration> and <system.web> elements. Inside the
appSettings element you can define as many keys with string values as you
want and then access these with the
System.Configuration.ConfigurationSettings class later.
Here's an example:

In your web.config:

<configuration>
<appSettings>
<add key="connStr" value="Data Source=xx......"/> <!-- Full
connection string -->
</appSettings>
<system.web>
.....

In your code:

string connStr =
System.Configuration.ConfigurationSettings.AppSettings.Get("connStr");

Et voilà :) You've made you'r app configurable..

HTH,
Lars-Erik
 
S

Shapper

Thanks Lars.

Hi!

There are several recommended ways to do this. One of them (at least the
initial step towards a better workday) is to put connectionstring info in
web.config. You can do this by adding the element <appSettings> to your
web.config between the <configuration> and <system.web> elements. Inside the
appSettings element you can define as many keys with string values as you
want and then access these with the
System.Configuration.ConfigurationSettings class later.
Here's an example:

In your web.config:

<configuration>
<appSettings>
<add key="connStr" value="Data Source=xx......"/> <!-- Full
connection string -->
</appSettings>
<system.web>
.....

In your code:

string connStr =
System.Configuration.ConfigurationSettings.AppSettings.Get("connStr");

Et voilà :) You've made you'r app configurable..

HTH,
Lars-Erik

Hello,

I have a dataset in an ASP.NET/VB web page:

Function dsNews() As System.Data.DataSet
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole
DB Services=-4; Data Source=C:\Database\myDatabase.mdb"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.OleDb.OleDbConnection(connectionString)
Dim queryString As String = "SELECT [t_news].* FROM [t_news]"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New
System.Data.OleDb.OleDbDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function

Shouldn't I move the connection,
connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB
Services=-4; Data Source=C:\Database\myDatabase.mdb"

to my Web.config file?

Can someone tell me how can I do this?

Thank You,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top