By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,813 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,813 IT Pros & Developers. It's quick & easy.

ConnectionString property has not been initialized

P: n/a
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.AppSett ings("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
Jan 19 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> 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.AppSett ings("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("connectionStrin g")
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
Jan 19 '06 #2

P: n/a

"Mike Labosh" <mlabosh_at_hotmail.com> wrote in message
news:eX*************@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.AppSett ings("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("connectionStrin g")
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
--
Peace & happy computing,

Mike Labosh, MCSD MCT
"Escriba coda ergo sum." -- vbSensei

Jan 19 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.