Connection Strings in 2005 from web.config

Jay1b

Contributor
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
Hi

I'm trying to use a connection string saved in the web.config, but i'm getting an error

Visual Basic:
Dim conn As New SqlConnection("ConnectionStrings:connCReq.ProviderName")

Visual Basic:
  <connectionStrings>
    <add name="connCReq" connectionString="Data Source=RA\SQLExpress;Initial Catalog=CReq;User ID=ASP;Password=password"
      providerName="System.Data.SqlClient" />
      </connectionStrings>

If i copy the ACTUAL connection string in, it works fine. Obviously i dont wish to do this as that would be besides the point of the web.config.

Can anybody spot whats wrong please?

Thanks
 
Don't have VS handy on this PC so I haven't tested it but have you tried
Visual Basic:
Dim conn As New SqlConnection("ConnectionStrings:connCReq")
if not I'll have a proper look at it later.
 
Thanks, but unfortunately i have tried it.... it returns the following error

'Format of the initialization string does not conform to specification starting at index 0'
 
Try something like
Visual Basic:
Dim conf as Configuration  = Configuration.GetWebConfiguration("~/")
Dim section as ConnectionStringsSection = conf.Sections("connectionStrings")
Dim conn As New SqlConnection(section.ConnectionStrings("connCReq").ConnectionString)
 
hmmm... Strange one....

Getwebconfiguration is not a member of system.configuration.configuration

Tried importing system.configuration as well...

I ran a search on google and it found the below
Visual Basic:
Dim cnfg As System.Configuration.Configuration
cnfg = System.Configuration.Configuration.GetWebConfiguration(Request.ApplicationPath)
Dim trace As System.Web.Configuration.TraceSection
trace = DirectCast(cnfg.GetSection("system.web/trace"), 
System.Web.Configuration.TraceSection)
trace.Enabled = True
cnfg.Update()

Which states its there as well.... I've almost tried referencing system.configuration, incase its that.... but it didnt change anything.
 
I've done that.... Or at least i think i have.

Website -> Add Reference... -> (Select System.Configuration) -> Click OK.

Also tried -

Website -> Start Options... -> References -> Add Reference... -> (Select System.Configuration) -> Click OK. (Box still remains blank).

I try this for "Imports System.Data.SqlClient" and it adds it to the box, and creates a 'bin' folder with the dll in it.
 
Back
Top