Jump to content
Xtreme .Net Talk

Connection string from web.config - ok in debug but not when published


Recommended Posts

Posted

Hi,

 

I have an Web Service project, and for the purposes of testing I have a web method that simply connects to the Database (SQLExpress on my local machine using SQL authentication), and returns a dataset. I am storing the connection string in the web.config file. I have another project which consumes the web service, and when I step through this one and enter the web method, the connection string is null so it fails to return anything.

 

The code in the web.config looks like:

<?xml version="1.0"?>
<configuration>
   <appSettings/>
   <connectionStrings>
     <add name="ConString" 
          connectionString="Data Source=PC\SQLEXPRESS; User Id=user; Pwd=password; Initial Catalog=db; Min Pool Size=5; Max Pool Size=75; Pooling=True"/>
   </connectionStrings>

The code I'm using in the web method to access this looks like:

System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
System.Configuration.ConnectionStringSettings connString;

connString = rootWebConfig.ConnectionStrings.ConnectionStrings["ConString"];

As I say - when I Debug the Web Service on it's own it works fine, but when run from a browser on it's own (http://localhost/MyProject/MyWebService.asmx) it does not return anything, and when I've debugged it from another project that consumes it this is because the connection string is null.

Any thoughts?

What if the Hokey-Pokey IS what it's all about?
Posted
Have you tried just accessing the connection strings directly? Something like

connString  = WebConfigurationManager.ConnectionStrings["ConString"].ConnectionString;

Once again PlausiblyDamp - thanks

 

I'm still in the dark as to why the code I had works in Debug but not when published. I think I got it directly from the MSDN help as well for accessing a connecting string from a web config.

 

Anyhow, thanks, sorted now

What if the Hokey-Pokey IS what it's all about?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...