1

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

10
  • 3
    plz post the past of config containing connection string Commented May 9, 2012 at 19:43
  • 1
    check that PlacementConnectionString is indeed the name of the ConnectionString Commented May 9, 2012 at 19:43
  • You're missing a letter in new qlConnection Commented May 9, 2012 at 19:44
  • What is connectionStr ? I don't see it in your code Commented May 9, 2012 at 19:48
  • connectionStr, the abbreviated "ConnectionString" which is a private variable of the ConfigurationManager's enumeration of all the ConnectionStrings Commented May 9, 2012 at 19:54

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.