Connection String Problem

S

shapper

Hello,

I am need to get a connection string details. I am doing as follows:

01: Dim connectionStrings As New ConnectionStringSettingsCollection
02: Dim connectionString As New ConnectionStringSettings
03: connectionString = connectionStrings.Item("MyConn")
04: Console.WriteLine(" Connection String: ",
connectionString.ConnectionString) *** ERROR ***
05: Console.WriteLine(" Name: ", connectionString.Name)
06: Console.WriteLine(" Provider Name: ",
connectionString.ProviderName)

I get my first error on line 04
System.NullReferenceException: Object reference not set to an
instance of an object.

To check my connection string I did the following:
Dim otherConnectionString As String =
ConfigurationManager.ConnectionStrings("MyConn").ConnectionString
Response.Write(otherConnectionString)

And I get:
Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Documents and
Settings\User\My Documents\Visual Studio
2005\WebSites\MySite\App_Data\MySite.mdb; User Id=; Password=;

Which proves I have the connection string with name MyConn in my
web.config.

Could someone tell me what am I doing wrong?

I tried everything I could think off.

Thanks,
Miguel
 
K

Karl Seguin [MVP]

Is there a reason you can't juse use
ConfigurationManager.ConnectionStrings("MyConn").ConnectionString ?

You're code doesn't make too much sense to me.
connectionStrings.Item("MyConn") will return null/nothing. All you do is
create a new collection and try to retrieve a value. It's like doing:

dim arr as new ArrayList()
dim o as object = arr(0);

you never put anything into arr.

Karl
 
S

shapper

Hi,

The point in this was to be able to connect to a database, an Access or
SQL.
So I was trying to check the provider of the connection string and have
two different codes.

Anyway, I found out that there is a Data.Common class with the factory
provider which can access any database.

I am using it now. I think this is the way to go and it solves my
problem. I don't need to find the provider name anymore.

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top