In this blog you will find ASP.NET 3.5 articles and some code samples. These code samples are created by me and should be working. Thanks for visiting my blog! Happy Dot Netting

Monday, July 14, 2008

How to get connection string from web.config file

Following function can get the connection string from web.config file. Please let me know your comments if it helped and saved your time. Thanks


    Public Shared Function GetConnectionString() As String

        Dim settings As ConnectionStringSettingsCollection = ConfigurationManager.ConnectionStrings

        ' Return No connection string found if there is nothing found

        Dim Constr = "No ConnectionString Found"

        If Not settings Is Nothing Then

            For Each cs As ConnectionStringSettings In settings

                If cs.Name = "XYZConnectionString" Then

                    Constr = cs.ConnectionString

                    Exit For

                End If

            Next

        End If

        Return Constr

    End Function

No comments: