Hi,

I am using VS 2005. I am trying to get the connectionstring from the Web.config file.

As per the Msdn as well as search on the google, most of the code days to use ConfigurationManager.Appsettings --> for getting the string from the file.

But when I am going deep into the System.Configuration dll, I am not able to find the ConfigurationManager class.

Please help me to reterive the connectionstring from the config file.

Here is the code that giving me issue.

lsSettings = System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString

Rgds,
Deven

Recommended Answers

All 9 Replies

I'm using this code :

Dim SQLConn As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

Yes, you are correct with the mentioned code. But it is not working for me. It gives me ConfigurationManager class cannot be found.

Hello friends,

Kindly look into this an let me know regarding the same as it is giving me error.

Rgds.
Deven

Make sure you have reference to System.Configuration added i.e.

Imports System.Configuration

Yes, I had already added the reference. Also I tried to use the System.Web.Confirguration Namescape so that I can get the webConfigurationmanager class.

Rgds,
Deven.

Deven,

Could you post the piece of code that produces error. so that we will digg more on this issue.

Thank you

Imports System
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Web

 Private Function GetConnectionString() As String

            Dim lsSettings As String

lsSettings = System.Configuration.ConfigurationManager.ConnectionString("ConnectionString").ToString
GetConnectionString = lsSettings

End Function

Deven,

I checked the given code.

1) I got a compilation error saying that "Error 1 'ConnectionString' is not a member of 'System.Configuration.ConfigurationManager' since the property has to be changed to "ConnectionStrings" (may be typo error in your code).

2) After changing the above it is working as expected.

Here is my sample code for reference;

Imports System
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Web

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Write(GetConnectionString())
    End Sub

    Private Function GetConnectionString() As String

        Dim lsSettings As String

        lsSettings = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ToString
        GetConnectionString = lsSettings

    End Function
End Class

config file (in complete)

<configuration>
	<appSettings/>
	<connectionStrings>
		<add name="ConnectionString" connectionString="server=;db=;user=;pwd;"/>
	</connectionStrings>
	<system.web>

Could you check the code again? so that we can find out.

Good luck.

Hello Sampath,

Yes, in the forum it is typo error. But in the IDE it is giving me error that ConfigurationManager class cannot be found.

Also to ensure that I cross-checked the same in the Object Browser. I could not get the ConfigurationManager class. I found ConfigurationSettings class.

Rgds,
Deven

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.