Home All Groups Group Topic Archive Search About

ConnectionString property has not been initialized

Author
19 Jan 2006 1:28 PM
tshad
I am trying to create an application in my VS 2002 application.  I took some
of the code from my web page where it works fine.

I can connect fine from my Sql Query Analyser to both my local Sql Server
and off Sql Server machines.

But I am getting the following error from my "objConn.Open()".

Is this a problem from going from my web to a normal application?

The error is:

The ConnectionString property has not been initialized

My code is:

Imports System.Data
Imports System.Data.SqlClient

....

Dim dbReader As SqlDataReader
Dim ConnectionString As String =
System.Configuration.ConfigurationSettings.AppSettings("Persist Security
Info=False;Data Source=balair.interez.com;Initial Catalog=InterezData;User
ID=xxx;Password=xxxx;")
Dim objConn As New SqlConnection(ConnectionString)
Dim CommandText As String = "GetCandidateSearchDrops"
Dim objCmd As New SqlCommand(CommandText, objConn)
objCmd.CommandType = CommandType.StoredProcedure
With objCmd.Parameters
    .Add("@CompanyID", SqlDbType.Int).Value = 153973
End With
objConn.Open()
dbReader = objCmd.ExecuteReader

Thanks,

Tom

Author
19 Jan 2006 2:04 PM
Mike Labosh
> I can connect fine from my Sql Query Analyser to both my local Sql Server
> and off Sql Server machines.
>
> But I am getting the following error from my "objConn.Open()".

> Dim dbReader As SqlDataReader
> Dim ConnectionString As String =
> System.Configuration.ConfigurationSettings.AppSettings("Persist Security
> Info=False;Data Source=balair.interez.com;Initial Catalog=InterezData;User
> ID=xxx;Password=xxxx;")
> Dim objConn As New SqlConnection(ConnectionString)


What your code is doing is trying to fetch a key from the .config file whose
name is the content of your connection string.

Here:

In App.Config:

<add key="connectionString" value="your actual connection string here" />


In your VB code:

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient

Public Class Foo

    Public Sub DoStuff()

        Dim cnstr As String =
ConfigurationSettings.AppSettings("connectionString")
        Dim cn As New SqlConnection(cnstr)
        ' etc.

End Class

You see, on the first "Dim" line, you are asking for the value of the item
whose key is named "connectionString".  The value that is returned is
whatever is in the value attribute in the config file.  Then you can go
about your business  :)


--
Peace & happy computing,

Mike Labosh, MCSD MCT
"Escriba coda ergo sum." -- vbSensei
Author
19 Jan 2006 2:05 PM
tshad
"Mike Labosh" <mlabosh_at_hotmail.com> wrote in message
news:eXWcY6PHGHA.344@TK2MSFTNGP11.phx.gbl...
> > I can connect fine from my Sql Query Analyser to both my local Sql
Server
> > and off Sql Server machines.
> >
> > But I am getting the following error from my "objConn.Open()".
>
> > Dim dbReader As SqlDataReader
> > Dim ConnectionString As String =
> > System.Configuration.ConfigurationSettings.AppSettings("Persist Security
> > Info=False;Data Source=balair.interez.com;Initial
Catalog=InterezData;User
Show quoteHide quote
> > ID=xxx;Password=xxxx;")
> > Dim objConn As New SqlConnection(ConnectionString)
>
>
> What your code is doing is trying to fetch a key from the .config file
whose
> name is the content of your connection string.
>
> Here:
>
> In App.Config:
>
> <add key="connectionString" value="your actual connection string here" />
>
>
> In your VB code:
>
> Imports System.Configuration
> Imports System.Data
> Imports System.Data.SqlClient
>
> Public Class Foo
>
>     Public Sub DoStuff()
>
>         Dim cnstr As String =
> ConfigurationSettings.AppSettings("connectionString")
>         Dim cn As New SqlConnection(cnstr)
>         ' etc.
>
> End Class
>
> You see, on the first "Dim" line, you are asking for the value of the item
> whose key is named "connectionString".  The value that is returned is
> whatever is in the value attribute in the config file.  Then you can go
> about your business  :)
>

So what I need to do in my application code is just put the connection
string directly into the variable.  I tried that and it worked.

Thanks,

Tom
Show quoteHide quote
>
> --
> Peace & happy computing,
>
> Mike Labosh, MCSD MCT
> "Escriba coda ergo sum." -- vbSensei
>
>