0

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

Edited by __avd: Corrected [code] tags.

4
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by deven_a76
0

I'm using this code :

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

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

0

Hello friends,

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

Rgds.
Deven

0

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

Imports System.Configuration

0

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.

0

Deven,

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

Thank you

0
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

Edited by mike_2000_17: Fixed formatting

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.