Connectionmanager

keon109

New member
Joined
Jul 12, 2007
Messages
2
Programming Experience
Beginner
Hi,

I'm a newbie in programming in VB.net and till a few days ago it was going well.

I have build a program with a database (.mdf) connection. this was build in VS 2003 and workt fine.

Now i updated my PC and installed vista with a VS 2005 and then the problems started. It looks i don't get a connection with the database. I use folowing code in the app.config
VB.NET:
    <connectionStrings>
        <add name="LiFA.My.MySettings.LiFAConnectionString" connectionString="Data Source=.\SQLEXPRESS;
            AttachDbFilename=|DataDirectory|\database\LiFA.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

and this code in my vb.net script
VB.NET:
strCon = Configurationsettings.AppSettings("connectionStrings")
conMyData = New SqlConnection(strCon)

but then i get a message that the code i use is obsolete and i must use following code
VB.NET:
strCon = ConfigurationManager.AppSettings("connectionStrings")
conMyData = New SqlConnection(strCon)

but configurationmanager is not reconised in the code????
In bouth cases the database connection does not work :(

Can someone help me with this problem? Thanks

greetings
Koen
 
Use the wizard to set up your connection. When you create the project go to data sources and click add new data source. You will then be able to create a connection string that you can use throughout your project. It will add the connection string in the XML file app.config assuming you are using winforms.


In order to work with the configuration file you must add a reference to your project. This reference will be the System.Configuration Namespace. This has many objects you can use to retrieve that various types of elements that you will have in a configuration file and to retrieve custom settings. Here is some code that I wrote up to get connection strings. I had a project that contained many connection strings in it and this gets each one and displays it.

VB.NET:
        Dim conStr As Configuration.ConnectionStringSettings

        For Each conStr In Configuration.ConfigurationManager.ConnectionStrings
            MsgBox(conStr.ConnectionString)
        Next


        Me.Close()

I think the only thing that you are missing is a reference to the System.Configuration namespace. This is actually pretty interesting to play around with. I did not know it even existed until now. You can do a number of things with all of these objects.
 
Thanks for the help. I found the problem and it looks it was something else. There is a small bug in VS2005. If i import the System.Configuration namespace. VS will use System.Configuration.dll. The command ConfigurationManager is not placed in that dll but in System.configuration.dll with a small c in stead of a capital.

I added the System.configuration.dll to the reference list in the properties of the project. the command configurationManager is now reconised by VS.

Then i changed my code into
VB.NET:
strCon = ConfigurationManager.ConnectionStrings(1).ConnectionString
and it worked.
 
Really I would not have thought that it would be case senseitive and there would be two dll's with essentially the same name and different content. Possible one of those is from 2003 and the other from 2005. That might be a good one to report for the next service pack.

But that configuration namespace was interesting. I just integrated it into one of my applications and took out this silly component I made to do basically the same thing.
 
Back
Top