0

I have a problem connecting my database (created in SQL Server 2008 R2 Express) with c# in vs 2013

Here is the code I wrote

string connStr = ConfigurationManager.ConnectionStrings["newSchool"].ToString();
SqlConnection conn = new SqlConnection(connStr);

the error is NullReferenceException

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3100088
  • 335
  • 3
  • 8
  • 20

3 Answers3

1

Change this:

string connStr = ConfigurationManager.ConnectionStrings["newSchool"].ToString();

to...

string connStr = ConfigurationManager.ConnectionStrings["newSchool"].ConnectionString;

Cheers -

simon at rcl
  • 7,326
  • 1
  • 17
  • 24
0

I assume that there is no connection string named newSchool. It is probably NewSchool or some other casing.


In response to your comment: You need to provide a proper connection string. The ConfigurationManager is used to get a value from the configuration. The NullReferenceException shows you that the setting you request is not there.

You need to create a connection string first using the settings manager and then you can access it.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

Where did you define your connection string? In the app.config? Please send us the place where you configure this.

See http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx for help.

See http://www.connectionstrings.com/ how to configure a connection string for .net

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325