The ConnectionString property has not been initialized

G

Guest

Hi,

I am fairly new to ASP.NET and have been working on a web form that will
allow a user to upload images to a database. I have found a sample web form
that I have been trying to get working, and the web form loads up fine but
upon submitting the image, it comes with the following error:

The ConnectionString property has not been initialized.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The ConnectionString
property has not been initialized.

Source Error:


Line 129: Try
Line 130:
Line 131: myConnection.Open()
Line 132:
Line 133:
myCommand.ExecuteNonQuery()

I found this script from the following website
http://aspalliance.com/articleViewer.aspx?aId=138&pId=

Can anyone please help I am completely lost as to how to correct this problem.
I am using Visual Studio 2005 beta 1, IIS 6.0, .NET version 2.0.40607 and
SQL Server 2000.

Any help would be greatly appreciated.

Thank,

Devante
 
M

Marina

The exception is very clear. The ConnectionString property has not been set,
when trying to call Open the SqlConnection object.

So obviously, this property is not being set - hence the connection can't do
it's job.

Looking through the code, it looks like the connection string is being
retrieved from Web.config: Dim myConnection As New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

Unless you have a ConnectionString setting in your Web.config with a valid
connection string to your database, this isn't going to work. I am guessing
you never added this setting. Did you think this source code would somehow
know how to connect to your particular sql server? You had to have somehow
specified how to connect.
 
G

Guest

Sounds like you did not specify a connection string when creating your
SqlConnection object. If you copied the code from the link that you
specified then you have a line of code looking like this:

Dim myConnection As New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

This indicates that whoever wrote the code put the connection string in the
appSettings section of the web.config file. If you did not do this then
ConfigurationSettings.AppSettings("ConnectionString") returns null and the
SqlConnection object does not know which database to connect to. So you have
to specify your own connection string instead.

HTH, Jakob.
 
G

Guest

Hi Jakob

I have created a web.config file and it does have the following info in it:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

<appSettings />
<connectionStrings>
<add name="AppConnectionString1"
connectionString="Server=DCKUEPERS-01;Integrated
Security=True;Database=WebDev"
providerName="System.Data.SqlClient" />
</connectionStrings>



But for some reason its still not working what am I doing wrong?
Thank you,

Devante
 
G

Guest

The line ConfigurationSettings.AppSettings("ConnectionString") will not work
with the config file you posted. Are you using this line of code? If yes,
then your config file should look like this:

<appSettings>
<add key="ConnectionString" value="..." />
</appSetting>

Or have you defined a configSection for reading the connection string? In
any case, I don't think you are reading the connection string correctly from
the config file, so you should do some debugging trying to find out what the
problem is.

Regards, Jakob.
 

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