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

ASP.NETVisual Basic.NET.NET Programming

Avatar of undefined
Last Comment
shieldguy

8/22/2022 - Mon
balochdude

               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
balochdude

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

ASKER
shieldguy

which classes i need to import as i am getting errors

on Configuration and ConnectionStringsSection

error is type expected
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
balochdude

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
ASKER
shieldguy

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




ASKER
shieldguy

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
prairiedog

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
shieldguy

Great thats the solution which I am looking for

Thanks