8

Really having an annoying time with connection strings.

I have two projects together in a single solution. A Web forms application acting as the presentation layer, and a class library supporting it which will send and receive data from a database.  

-- The Employee Class within the Class Library Project --

Friend Class Employee

Public Function GetEmployees() As DataSet

    Dim DBConnection As New SqlConnection(My_ConnectionString)
    Dim MyAdapter As New SqlDataAdapter("exec getEmployees", DBConnection)

    Dim EmployeeInfo As DataSet
    MyAdapter.Fill(EmployeeInfo, "EmployeeInfo")

    Return EmployeeInfo

End Function

End Class

Currently the application is telling me it cannot access "My_ConnectionString" which I have attempted to store within a config file for quick repeated access:

<configuration>

<system.web>
  <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5"  />
</system.web>

 <connectionStrings>
   <add name="My_ConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=My_DB;Integrated Security=True;"/>
 </connectionStrings>

</configuration>

The web.config is part of the web form project and not the class library, are these projects unable to 'talk' to each other? Do I need to add a web / app config file to the class library to store a connection string within that project?

2
  • From your code it looks like you're trying to use My_ConnectionString to retrieve the value from your config file. If that is correct, that will not work - you need to retrieve it from the config file in a manner similar to @G. Stoynev's answer below. Commented Mar 16, 2013 at 6:29
  • this might be useful connectionstrings.com Commented Mar 16, 2013 at 6:40

5 Answers 5

23

Not clear where My_ConnectionString is coming from in your example, but try this

System.Configuration.ConfigurationManager.ConnectionStrings("My_ConnectionString").ConnectionString

like this

Dim DBConnection As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("My_ConnectionString").ConnectionString)
Sign up to request clarification or add additional context in comments.

7 Comments

You can also add a reference to System.Configuration, import it with Imports System.Configuration and then access it with the shorter ConfigurationManager.ConnectionStrings("My_ConnectionString").ConnectionString.
I would also suggest using a Using block to ensure the connection is properly disposed of.
'ConfigurationManager' is not a member of 'Configuration' I feel like I've made a mistake somewhere...
@Corgalas - Did you add a reference to System.Configuration in your project (the DLL) and add an Imports System.Configuration statement?
Forgot the add the reference to the project. Thanks Tim!
|
5

If it's a .mdf database and the connection string was saved when it was created, you should be able to access it via:

    Dim cn As SqlConnection = New SqlConnection(My.Settings.DatabaseNameConnectionString)

Hope that helps someone.

Comments

1

Connection in APPConfig

<connectionStrings>
  <add name="ConnectionString" connectionString="Data Source=192.168.1.25;Initial Catalog=Login;Persist Security Info=True;User ID=sa;Password=example.com"   providerName="System.Data.SqlClient" />
</connectionStrings>

In Class.Cs

public string ConnectionString
{
    get
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    }
}

Comments

0
Public Function connectDB() As OleDbConnection

        Dim Con As New OleDbConnection
        'Con.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=" & DBNAME & ";Data Source=" & DBSERVER & ";Pwd=" & DBPWD & ""
        Con.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DBNAME;Data Source=DBSERVER-TOSH;User ID=Sa;Pwd= & DBPWD"
        Try
            Con.Open()
        Catch ex As Exception
            showMessage(ex)
        End Try
        Return Con
    End Function

2 Comments

Would you please add some explanation to your code? Thanks!
This connection string has hard coded entries,it's not getting them from the .config. What if you need to point to a different server? With this code the application would need deploying with the changes. With .config it can be changed in the background
0

I don't know if this still is an issue, but i prefere just to use the My.Settings in my code.

Visual Studio generates a simple class with functions for reading settings from the app.config file.

You can simply access it using My.Settings.ConnectionString.

    Using Context As New Data.Context.DataClasses()
        Context.Connection.ConnectionString = My.Settings.ConnectionString
    End Using

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.