0

My program has a SQL connection string hard coded in place:

Public SQLconn As SqlConnection = New SqlConnection("Data Source=Database444;Initial Catalog=SQL3;User ID=user;Password=user")

Now I realized that there is a connection string stored in the Web.config file called connectionString and I want to link to that instead of hardcoding it on my program.

So I tried this:

Dim SQLconn As String = "Provider=SQLOLEDB;" & connectionString.ConnectionString

Now I get the error The name connectionString is not declared.

I've also added in the imports settings, but still no luck

Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Globalization

What else am I missing?

hjh93
  • 570
  • 11
  • 27
  • 1
    Possible duplicate of [Read connection string from web.config](https://stackoverflow.com/questions/6134359/read-connection-string-from-web-config) – Esko Feb 19 '19 at 08:40
  • I checked the duplicate question, its correct answer is already included in my code. `system.configuration` is already included in the code. – hjh93 Feb 19 '19 at 08:43
  • 1
    `Configuration.ConfigurationManager.AppSettings("connectionString")` – D Ie Feb 19 '19 at 08:56

1 Answers1

0

Finally found one answer after scrounging the Internet.

'first I need to declare this on my public class
Private connectionStr As String = ConfigurationSettings.AppSettings("connectionString")


'then do this
Public SQLconn As SqlConnection = New SqlConnection(connectionStr)

In hindsight I kinda asked this question in a panic.

hjh93
  • 570
  • 11
  • 27