-1

I am developing a School Management System in Asp.net and C#. I have created a login page when i click to login an error occoured that Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error:

public string connectionString()
{
    string connString = ConfigurationManager.ConnectionStrings["SchoolConnectionString1"].ConnectionString;
    return connString;
}

Source File: C:\Users\ALI\Desktop\Final Project\SchoolERP\SchoolERP\Connection\MyConnection.cs Line: 14

My Stack Trace is given under

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] SchoolERP.Connection.MyConnection.connectionString() in C:\Users\ALI\Desktop\Final SchoolERP.Connection.MyConnection.connectionString() in C:\Users\ALI\Desktop\Final Project\SchoolERP\SchoolERP\Connection\MyConnection.cs:14 SchoolERP.Pages.Membership.Logout.btnLogout_Click(Object sender, EventArgs e) in C:\Users\ALI\Desktop\Final Project\SchoolERP\SchoolERP\Pages\Membership\Logout.aspx.cs:26 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

mehdi lotfi
  • 11,194
  • 18
  • 82
  • 128
Ali Akbar
  • 11
  • 1
  • 2
  • 5
  • 1
    Is `ConfigurationManager.ConnectionStrings["SchoolConnectionString1"]` `null`? Use the debugger to check that. If so, ensure that you provide it in your `web.config`-file. Here you see how: http://www.connectionstrings.com/store-connection-string-in-webconfig/ – Tim Schmelter Jan 07 '14 at 07:50
  • Object reference errors are very easy. You are trying to access a property of some variable whos value is null. Clearly its not finding your connection string, so the value is null, so it cannot call the `.ConnectionString` property. – crthompson Jan 07 '14 at 07:50
  • Your config file seems missing connection string –  Jan 07 '14 at 07:51
  • Most of the time Object Reference exception is cause by invalid name or accessing field for null object. – Nagaraj S Jan 07 '14 at 07:52

4 Answers4

3

Problem : You might be missing ConnectionString SchoolConnectionString1 in your web.config file.

Solution : You need to add ConnectionString in your web.config file as below:

<connectionStrings>
  <add 
    name="SchoolConnectionString1" 
    connectionString="Data Source=serverName;Initial 
    Catalog=DatabaseName;Persist Security Info=True;User 
    ID=userName;Password=password"
    providerName="System.Data.SqlClient"
  />
</connectionStrings>
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
1

Please check if you actually have a "SchoolConnectionString1" connection string in your config file.

In the config file (web.config if you are working on a web project), under you should see something like this:

<add name="SchoolConnectionString1" connectionString="Data Source=someServer;Initial Catalog=MyDB[...]" providerName="System.Data.SqlClient" />

If this is not the case, and the connection string appears correct, check if your web.config as a whole is well-formatted.

nestedloop
  • 2,596
  • 23
  • 34
1

Do not use appsettings in web.config. Instead use the connectionStrings section in web.config.

     <connectionStrings>
     <add name="SchoolConnectionString1" 
     connectionString="Data Source=DatabaseServerName;Integrated Security=true;Initial   Catalog=MySampleDB" providerName="System.Data.SqlClient"/>
     </connectionStrings >

In code behind use add namespace

using System.Configuration;
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
1

Most propably this is a case of misplaced web config or invalid structure (if the connection string is there and there is no spelling error in

SchoolConnectionString1

as stated in the other answers.

In our projects there are more than one Web config files (one for debug and one for release) so this stuff happens regularly.

So check if there are two in your case (maybe your project has Web.Degug.Config and Web.Release.Config for different builds). This is an option in VS 2012 (Maybe earlier versions too) to replace some of your configuration for different enviroments.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61