Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modify connectionstring at runtime

Status
Not open for further replies.

steve1rm

Programmer
Aug 26, 2006
255
GB
Hello

VS 2005

I have my connection string in the web.config as I am developing a ASP.NET website the code as followings
Code:
appSettings
connectionStrings
add name="serviceMasterConnectionString" connectionString="Data Source=CF_DEVELOP\realitysql;Initial Catalog=ServiceMaster;User ID=assword="
connectionStrings

I can read the connection string using the following. but how do i change the connection string.

Code:
imports system.web.configuration
imports system.configuration

Dim cnnString As String
cnnString = WebConfigurationManager.ConnectionStrings("serviceMasterConnectionString").ConnectionString()
cnn.ConnectionString = cnnString

I tried the following and got an error message.
Code:
config.ConnectionStrings.ConnectionStrings("serviceMasterConnectionString").ConnectionString = "new connection string"
config.Save()

Error:
Access to the path 'C:\Inetpub\ is denied.

I would like to change the connection string at run-time and save it.

Does anyone have any code examples.

Many thanks,

Steve
 
The web.config is a simplw XML file. Here is an example:

Code:
using System;
using System.Xml;  
using System.Configuration;
using System.Collections;
using System.Reflection;  
using System.Diagnostics ;

public enum   ConfigFileType
{
 WebConfig ,
 AppConfig
}

public class AppConfig : System.Configuration.AppSettingsReader
{ 
 public string  docName = String.Empty;
 private  XmlNode node=null;

 private int _configType; public   int ConfigType
 {
  get
  {
   return _configType;
  }
  set
  {
   _configType=value;
  }
 }

 public bool SetValue(string key, string value)
 {
  XmlDocument cfgDoc = new XmlDocument(); 
  loadConfigDoc(cfgDoc);
   // retrieve the appSettings node 
  node =  cfgDoc.SelectSingleNode("//appSettings");
   
   if( node == null )
   {
    throw new System.InvalidOperationException( "appSettings section not found"); 
   }
   
  try
  {
   // XPath select setting "add" element that contains this key    
   XmlElement addElem= (XmlElement)node.SelectSingleNode("//add[@key='" +key +"']") ;
   if (addElem!=null)
   {
    addElem.SetAttribute("value",value);    
   }
    // not found, so we need to add the element, key and value
   else 
   {
    XmlElement entry = cfgDoc.CreateElement("add");
    entry.SetAttribute("key",key);
    entry.SetAttribute("value",value); 
    node.AppendChild(entry);    
   }
   //save it
   saveConfigDoc(cfgDoc,docName);
   return true;
  }
  catch 
  {
   return false;
  }
 }
 
 private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath)
 { 
  try
  {    
   XmlTextWriter writer = new XmlTextWriter( cfgDocPath , null );
   writer.Formatting = Formatting.Indented;     
   cfgDoc.WriteTo( writer );     
   writer.Flush();
   writer.Close();   
   return;
  }
  catch
  {
   throw;
  }
 }
 
 public bool removeElement ( string elementKey)
 {
  try
  {
   XmlDocument cfgDoc = new XmlDocument(); 
   loadConfigDoc(cfgDoc);
   // retrieve the appSettings node 
   node =  cfgDoc.SelectSingleNode("//appSettings");   
   if( node == null )
   {
    throw new System.InvalidOperationException( "appSettings section not found"); 
   }   
   // XPath select setting "add" element that contains this key to remove   
   node.RemoveChild( node.SelectSingleNode("//add[@key='" +elementKey +"']") );
   
   saveConfigDoc(cfgDoc,docName);
   return true;
  }
  catch
  {
   return false;
  }
 }


 private XmlDocument loadConfigDoc( XmlDocument cfgDoc )
 { 
  // load the config file 
  if(  Convert.ToInt32(ConfigType)==Convert.ToInt32(ConfigFileType.AppConfig))
  {
    
   docName= ((Assembly.GetEntryAssembly()).GetName()).Name; 
   docName +=   ".exe.config";
  }
  else
  {
   docName=System.Web.HttpContext.Current.Server.MapPath("web.config");
  }
  cfgDoc.Load( docName ); 
  return cfgDoc;
 }

}
 
As you see it is in C#, but it is simple to convert
 
Hello TipGiver,

I thought there must be something much easier than that.

I thought there was a method using the configuration that enabled you to modify the connection string.

Any ideas I will be happy to know.

Thanks for the code I will look into this. converting easy for me.

Thanks

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top