The ConnectionString property has not been initialized.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My debugger is saying that the ConnectionString property has not been
initialized. when I try to open the connection. My goal is to loop through an
arraylist of objects (OrderModel) and insert a record each time. What am I
missing here?

using (SqlConnection conn = new
SqlConnection(ConfigurationManager.AppSettings["myConnectionString"]))
{
int counter = 0;

SqlCommand cmd = conn.CreateCommand();
cmd.Connection.Open();
foreach (OrderModel ord in NewOrders)
{
cmd.CommandText = buildSql(ord);
try
{
cmd.ExecuteNonQuery();
counter++;
}
catch (SqlException ex)
{
TextBox1.Text = ex.Message;
}
}
TextBox1.Text = counter.ToString() + " records inserted";
}
 
It looks like there is no myConnectionString seting in your configuration
file, or it's empty. Basically, you are passing empty string for the
connection string.
 
in my web.config file I have:

<connectionStrings>
<clear />
<add name="myConnectionString" connectionString="Data
Source=(local)\SQLDB;Initial Catalog=myDatabase;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
</connectionStrings>

am I referencing this incorrectly?


Marina Levit said:
It looks like there is no myConnectionString seting in your configuration
file, or it's empty. Basically, you are passing empty string for the
connection string.

archuleta37 said:
My debugger is saying that the ConnectionString property has not been
initialized. when I try to open the connection. My goal is to loop through
an
arraylist of objects (OrderModel) and insert a record each time. What am I
missing here?

using (SqlConnection conn = new
SqlConnection(ConfigurationManager.AppSettings["myConnectionString"]))
{
int counter = 0;

SqlCommand cmd = conn.CreateCommand();
cmd.Connection.Open();
foreach (OrderModel ord in NewOrders)
{
cmd.CommandText = buildSql(ord);
try
{
cmd.ExecuteNonQuery();
counter++;
}
catch (SqlException ex)
{
TextBox1.Text = ex.Message;
}
}
TextBox1.Text = counter.ToString() + " records inserted";
}
 
I don't know if this has changed with 2.0, but in 1.1, it had to be
something like:

<appSettings>
<add key="myConnectionString" value="Data
Source=(local)\SQLDB;Initial Catalog=myDatabase;Integrated Security=SSPI" />
</appSettings>

I am not familiar with the connectionStrings web.config sections, but I
suspect you probably have to access it a different way, other then through
AppSettings property which refers to the XML chunk as I showed above.

archuleta37 said:
in my web.config file I have:

<connectionStrings>
<clear />
<add name="myConnectionString" connectionString="Data
Source=(local)\SQLDB;Initial Catalog=myDatabase;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
</connectionStrings>

am I referencing this incorrectly?


Marina Levit said:
It looks like there is no myConnectionString seting in your configuration
file, or it's empty. Basically, you are passing empty string for the
connection string.

archuleta37 said:
My debugger is saying that the ConnectionString property has not been
initialized. when I try to open the connection. My goal is to loop
through
an
arraylist of objects (OrderModel) and insert a record each time. What
am I
missing here?

using (SqlConnection conn = new
SqlConnection(ConfigurationManager.AppSettings["myConnectionString"]))
{
int counter = 0;

SqlCommand cmd = conn.CreateCommand();
cmd.Connection.Open();
foreach (OrderModel ord in NewOrders)
{
cmd.CommandText = buildSql(ord);
try
{
cmd.ExecuteNonQuery();
counter++;
}
catch (SqlException ex)
{
TextBox1.Text = ex.Message;
}
}
TextBox1.Text = counter.ToString() + " records
inserted";
}
 
hmmm...
The connection string works, so I don't think I have to worry about my
syntax there. But apparently I'm not getting my sytax right for referencing
it. I had picked up that syntax from something I found on the web (not sure
where anymore) but I noticed that there is a System.Web.Configuration
namespace, so since I'm working with a web app, maybe that's what I need.

Thank goodness for intellisense! At least that gives me something to go on
to play around with, but so far I still haven't found another syntax that
doesn't throw a build error.

Marina Levit said:
I don't know if this has changed with 2.0, but in 1.1, it had to be
something like:

<appSettings>
<add key="myConnectionString" value="Data
Source=(local)\SQLDB;Initial Catalog=myDatabase;Integrated Security=SSPI" />
</appSettings>

I am not familiar with the connectionStrings web.config sections, but I
suspect you probably have to access it a different way, other then through
AppSettings property which refers to the XML chunk as I showed above.

archuleta37 said:
in my web.config file I have:

<connectionStrings>
<clear />
<add name="myConnectionString" connectionString="Data
Source=(local)\SQLDB;Initial Catalog=myDatabase;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
</connectionStrings>

am I referencing this incorrectly?


Marina Levit said:
It looks like there is no myConnectionString seting in your configuration
file, or it's empty. Basically, you are passing empty string for the
connection string.

My debugger is saying that the ConnectionString property has not been
initialized. when I try to open the connection. My goal is to loop
through
an
arraylist of objects (OrderModel) and insert a record each time. What
am I
missing here?

using (SqlConnection conn = new
SqlConnection(ConfigurationManager.AppSettings["myConnectionString"]))
{
int counter = 0;

SqlCommand cmd = conn.CreateCommand();
cmd.Connection.Open();
foreach (OrderModel ord in NewOrders)
{
cmd.CommandText = buildSql(ord);
try
{
cmd.ExecuteNonQuery();
counter++;
}
catch (SqlException ex)
{
TextBox1.Text = ex.Message;
}
}
TextBox1.Text = counter.ToString() + " records
inserted";
}
 
I found my mistake.
I now have it working with this syntax:

SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString)



Marina Levit said:
I don't know if this has changed with 2.0, but in 1.1, it had to be
something like:

<appSettings>
<add key="myConnectionString" value="Data
Source=(local)\SQLDB;Initial Catalog=myDatabase;Integrated Security=SSPI" />
</appSettings>

I am not familiar with the connectionStrings web.config sections, but I
suspect you probably have to access it a different way, other then through
AppSettings property which refers to the XML chunk as I showed above.

archuleta37 said:
in my web.config file I have:

<connectionStrings>
<clear />
<add name="myConnectionString" connectionString="Data
Source=(local)\SQLDB;Initial Catalog=myDatabase;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
</connectionStrings>

am I referencing this incorrectly?


Marina Levit said:
It looks like there is no myConnectionString seting in your configuration
file, or it's empty. Basically, you are passing empty string for the
connection string.

My debugger is saying that the ConnectionString property has not been
initialized. when I try to open the connection. My goal is to loop
through
an
arraylist of objects (OrderModel) and insert a record each time. What
am I
missing here?

using (SqlConnection conn = new
SqlConnection(ConfigurationManager.AppSettings["myConnectionString"]))
{
int counter = 0;

SqlCommand cmd = conn.CreateCommand();
cmd.Connection.Open();
foreach (OrderModel ord in NewOrders)
{
cmd.CommandText = buildSql(ord);
try
{
cmd.ExecuteNonQuery();
counter++;
}
catch (SqlException ex)
{
TextBox1.Text = ex.Message;
}
}
TextBox1.Text = counter.ToString() + " records
inserted";
}
 
Back
Top