How to fix The ConnectionString property has not been initialized
Referencing the connection string should be done as such:
MySQLHelper.ExecuteNonQuery(
ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString,
CommandType.Text,
sqlQuery,
sqlParams);
ConfigurationManager.AppSettings["ConnectionString"]
would be looking in the AppSettings
for something named ConnectionString
, which it would not find. This is why your error message indicated the "ConnectionString" property has not been initialized
, because it is looking for an initialized property of AppSettings
named ConnectionString
.
ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString
instructs to look for the connection string named "MyDB".
Here is someone talking about using web.config connection strings
C# - The ConnectionString property has not been initialized
The problem is
using
keyword.using
directive only be use
disposable objects.If you create any instance in using block, the instance disposible out
of curly braces..
public SqlConnection GetConnection()
{
SqlConnection connection = new SqlConnection(connectionString);
if (connection.State == ConnectionState.Closed)
connection.Open();
return connection;
}
The ConnectionString property has not been initialized.' TO FIX
You need to open the connection
using (Connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter("INPUT INTO Person", Connection))//, Connection
{
DataTable RegisterTable = new DataTable();
adapter.Fill(RegisterTable); //System.InvalidOperationException: 'The ConnectionString property has not been initialized.' TO FIX
string name = textBox1.Text;
string organisation = textBox3.Text;
DateTime Time = DateTime.Parse(textBox2.Text);
string strDateTimeIn = Time.ToString("yyyy-MM-dd HH:mm:ss.ffff");
string query = "INSERT INTO Person (Name,Organisation,TimeIn) VALUES('" + name + "','" + organisation + "','" +strDateTimeIn+ "')";
SqlCommand SignIn = new SqlCommand(query,Connection);
SignIn.ExecuteNonQuery();
}
}
The ConnectionString property has not been initialized Error during inserting data to Database
You are not reading the connection string from Configuration so in you case connection sting is null.
var connectionString =System.Configuration.ConfigurationManager.
ConnectionStrings["_cs"].ConnectionString;
dbUtil db = new dbUtil(connectionString );
Please try this one.
The ConnectionString property has not been initialized after using
The problem is that the using
statement is closing the connection when it returns.
Create the SqlConnection
inside your using statement as follows:
using (SqlConnection conn = new SqlConnection(connString)) { ... }
For getting the connection string from your config file:
connString = ConfigurationManager.ConnectionStrings["yourConnString"].ConnectionString;
The configuration file:
<connectionStrings>
<add name="yourConnString" connectionString="..." providerName="..."/>
</connectionStrings>
The ConnectionString property has not been initialized Error with Dapper
Your code should look like:
_connection.Open();
var sql = "sql query"
foreach (var id in idlist)
{
var parameters = ....
await _connection.ExecuteAsync(sql, parameters);
}
_connection.Close();
If you're executing only once, omit the open and close (Dapper will do it), unless your connection is enrolled in a transaction
_connection
is named like as if it's a class level variable. Be sure that you aren't manipulating it elsewhere while the loop is running. I wouldn't say you gain much by holding a reference to a connection, perhaps consider:
using var connection = new SqlConnection("your conn string");
connection.Open();
var sql = "sql query"
foreach (var id in idlist)
{
var parameters = ....
await _connection.ExecuteAsync(sql, parameters);
}
connection.Close();
Error handling will vary according to what you want to do if eg one insert fails mid batch - dump it all, halt and fix, ignore and proceed.
Related Topics
How to Pass Values Between Forms in C# Windows Application
How to Print Out a Tree Structure
Deferred Execution and Eager Evaluation
Static Binding Doesn't Update When Resource Changes
Camera.Main Is Null When Performing Raycast
An Object Reference Is Required for the Non-Static Field, Method, or Property
Persist Data by Programming Against Interface
Replace Unicode Escape Sequences in a String
3D Relative Angle Sum Calculation
Extremely Large Single-Line File Parse
Class List Keeps Printing Out as Class Name in Console