1

I have a simple WinForm that I'm using to try to help me with ADO.NET. It has a datagridview

I have added a compact sql server database to the project called experiment.sdf

In the App.config file I have added the following. I used a previous project app file to base this on so maybe there's an error in here?:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
  <add name="DatabaseDGVexperiments.Properties.Settings.DatabaseDGVexperimentsConnStg"
      connectionString="Data Source=|DataDirectory|\experiment.sdf"
      providerName="Microsoft.SqlServerCe.Client.3.5" />
</connectionStrings>
</configuration>

I've added a reference to the configuration library.

Behind the form is the following code:

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

When it hits the line SqlConnection conn = ... I get an error NullReferenceException was unhandled. How do I fix this?

jmoreno
  • 12,752
  • 4
  • 60
  • 91
whytheq
  • 34,466
  • 65
  • 172
  • 267
  • Make sure `ConfigurationManager.ConnectionStrings["DatabaseDGVexperimentsConnStg"].ConnectionString` is not null. – Nikhil Agrawal Jun 09 '12 at 13:18
  • 1
    You calling `.ConnectionString` of a null reference. `ConfigurationManager.ConnectionStrings["DatabaseDGVexperimentsConnStg"]` does not return an object. The string specified does not match a string in the web.config – Nope Jun 09 '12 at 13:24

3 Answers3

7

Change this:

name="DatabaseDGVexperiments.Properties.Settings.DatabaseDGVexperimentsConnStg"

to:

name="DatabaseDGVexperimentsConnStg"

or change this:

ConfigurationManager.ConnectionStrings["DatabaseDGVexperimentsConnStg"]

to:

ConfigurationManager.ConnectionStrings["DatabaseDGVexperiments.Properties.Settings.DatabaseDGVexperimentsConnStg"]
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • I changed the config file. Now it errors on the line `myAdapt.Fill...` with an `SQLException was Unhandled`; A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. – whytheq Jun 09 '12 at 13:37
  • the sdf file is in the same project! why should it have difficulty connecting? – whytheq Jun 09 '12 at 13:38
  • @whytheq: You need the SQL CE classes to use the SqlServerCe provider. Change SqlConnection to SqlCeConnection, and so on. – Guffa Jun 09 '12 at 15:42
  • that worked a treat; I also had to change `SELECT * FROM experiment.dbo.helloworld` to `SELECT * FROM helloworld` for the code to work - why? – whytheq Jun 09 '12 at 17:22
  • @whytheq: Becauase there are no databases and users (the first two parts of the xx.xx.xx notation), the SQL CE file is a single database, and you don't specify a user when you connect to it. – Guffa Jun 09 '12 at 18:09
1
ConfigurationManager.ConnectionStrings["DatabaseDGVexperiments.Properties.Settings.DatabaseDGVexperimentsConnStg"].ConnectionString

note that you need to give name as

DatabaseDGVexperiments.Properties.Settings.DatabaseDGVexperimentsConnStg

not as

DatabaseDGVexperimentsConnStg

EDIT:

You need to use SqlCeConnection and SqlCeDataAdapter too work with SqlServerCe database, add reference to System.Data.SqlServerCe and then code should like below.

using (var conn = new SqlCeConnection(ConfigurationManager.ConnectionStrings["DatabaseDGVexperiments.Properties.Settings.DatabaseDGVexperimentsConnStg"].ConnectionString))
{
    conn.Open();
    using (var myAdapt = new SqlCeDataAdapter("SELECT * FROM experiment.dbo.helloworld", conn))
    {
        DataSet mySet = new DataSet();
        myAdapt.Fill(mySet, "AvailableValues");
        DataTable myTable = mySet.Tables["AvailableValues"];
        this.uxExperimentDGV.DataSource = myTable;
    }
}
Damith
  • 62,401
  • 13
  • 102
  • 153
  • Thanks. I changed the config file. Now it errors on the line `myAdapt.Fill...` with an `SQLException was Unhandled; A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.` – whytheq Jun 09 '12 at 13:41
  • thanks Damith - `using` is a way to avoid persistent database activity? – whytheq Jun 09 '12 at 17:20
  • Using statement handles the disposing object. you don't need to do it from code. – Damith Jun 09 '12 at 17:27
0

You've used a different key value in your code then in web.config. the strings need to match exactly.

D Stanley
  • 149,601
  • 11
  • 178
  • 240