Reputation: 133
I have a problem. I am trying To extract a connection string through
sqlConnStr = new qlConnection(ConfigurationManager.ConnectionStrings["PlacementConnectionString"].ConnectionString);
but it keeps failing :
Object Ref not set to an instance of an object
I check with the debugger and this is the value of the connectionStr
{Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Placement.accdb}
I have imported my database through VS2012's DataSet wizard so what am I doing wrong?
PS: I have tested the connection numerous times.
<connectionStrings>
<add name="_201103578_P09.Properties.Settings.PlacementConnectionString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\Placement.accdb"
providerName="System.Data.OleDb" />
</connectionStrings>
Kind regards
Markus
[UPDATE]
I changed from
sqlAdapter = new SqlDataAdapter();
try
{
sqlConnStr = new SqlConnection(ConfigurationManager.ConnectionStrings["PlacementConnectionString"].ConnectionString);
}
to
sqlAdapter = new SqlDataAdapter();
string s = ConfigurationManager.ConnectionStrings[1].ConnectionString;
try
{
sqlConnStr = new SqlConnection(s);
}
I inspect s and the value is
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Placement.accdb
Now an error is thrown
System.ArgumentException: Keyword not supported: 'provider'
At wits' end;
=================================================================
TO EVERYONE - THANK YOU the problem was (I forgot) when using an Access database you have to use OleDbCommand
and not SqlCommand
. Thank you everything works fine now! – Markus just now edit
Upvotes: 1
Views: 5594
Reputation: 973
Your connectionstring name is incorrect
<connectionStrings>
<add name="_201103578_P09.Properties.Settings.PlacementConnectionString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\Placement.accdb"
providerName="System.Data.OleDb" />
Instead it should be
<connectionStrings>
<add name="PlacementConnectionString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\Placement.accdb"
providerName="System.Data.OleDb" />
Or in your code you should look for
ConfigurationManager.ConnectionStrings["_201103578_P09.Properties.Settings.PlacementConnectionString"].ConnectionString
Upvotes: 1
Reputation: 4032
Going off the code you posted, the only explanation I can see is that the null reference you're getting is related to the configuration manager not getting anything by the string you're passing it.
if
ConfigurationManager.ConnectionStrings["PlacementConnectionString"]
doesn't return anything-- calling
.ConnectionString
will fail with your error. Can you verify that
"PlacementConnectionString"
is the correct name of the connection?
Upvotes: 1