Link to home
Start Free TrialLog in
Avatar of shieldguy
shieldguyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to read the connection string value from the web.config

How can I read the value of the connectionstring1 from the web.config file in the asp.net  to create connetion object

Thanks

<?xml version="1.0"?>
 
<configuration>
  
    <configSections>
    </configSections>
    
	<connectionStrings>
		<add name="connectionstring1" connectionString="Server=ABCServerName;Database=Db1;User ID=sa;Password=abc;Trusted_Connection=False;Min Pool Size = 9;Max Pool Size = 80" providerName="System.Data.SqlClient"/>
	</connectionStrings>
  
    <system.web>
..........
..........
..........

Open in new window

Avatar of balochdude
balochdude
Flag of United Kingdom of Great Britain and Northern Ireland image

               Dim WebConfig As Configuration = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath)
                Dim ConnectionString As ConnectionStringsSection = CType(WebConfig.GetSection("connectionStrings"), ConnectionStringsSection)
dim strConnectionString = ConnectionString.ConnectionStrings("connectionstring1").ConnectionString
you may like to remove the localdatabase, in case it may conflict with the connection:


<connectionStrings>
		<remove name="LocalSqlServer"/>
		<add name="connectionstring1" connectionString="Server=ABCServerName;Database=Db1;User ID=sa;Password=abc;Trusted_Connection=False;Min Pool Size = 9;Max Pool Size = 80" providerName="System.Data.SqlClient"/>
	</connectionStrings>

Open in new window

Avatar of shieldguy

ASKER

which classes i need to import as i am getting errors

on Configuration and ConnectionStringsSection

error is type expected
I am importing these classes in my dbconnection class:
Imports System.Web.Configuration
Imports System.Data
Imports System.Data.SqlClient

Just for the getting the connection string from the web config you need:
Imports System.Web.Configuration
Can I use some other way as I might use this code in the vb.net desktop application so it will be problem when I am calling the Imports System.Web.Configuration later




I think this solution is not stuitable with my code can someone give me the other way to read this connection please so I would be able to read the value in vb.net application

thanks
ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great thats the solution which I am looking for

Thanks