954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ConnectionString Config

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

deven_a76
Newbie Poster
21 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

I'm using this code :

Dim SQLConn As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Kusno
Junior Poster
191 posts since Aug 2007
Reputation Points: 11
Solved Threads: 17
 

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

deven_a76
Newbie Poster
21 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

Hello friends,

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

Rgds.
Deven

deven_a76
Newbie Poster
21 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

Imports System.Configuration

guru_sarkar
Junior Poster in Training
70 posts since Jul 2008
Reputation Points: 12
Solved Threads: 8
 

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_a76
Newbie Poster
21 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

Deven,

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

Thank you

MeSampath
Junior Poster
102 posts since Oct 2009
Reputation Points: 23
Solved Threads: 27
 

[code]
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_a76
Newbie Poster
21 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.

MeSampath
Junior Poster
102 posts since Oct 2009
Reputation Points: 23
Solved Threads: 27
 

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

deven_a76
Newbie Poster
21 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You