Object not defined to reference with the Web service


Basically I have an old asmx web service on one server and I have a consume app that has this as a web reference and can see the available methods. Also if I go to the web service URI I get the standard page that lists them all out.

I am trying to create a "consumer" app to prove it works as just pressing "play" in "debug" mode is not good enough as we need to give the customer examples of how to use it.

Although it works 100% when I do it as a debug run from VS 2010 on my PC and localhost:5768 I cannot get it to work from my consumer console app.

It is raising an exception saying that an object reference doesn't exist when I try to get the connection string I need from the web.config file.

This works when I run it in debug mode but NOT when my consumer app tried to run it which has a web reference to the right service URI loaded.

The code in web.confifg is just

    <add name="APIDatabase" connectionString="SERVER=MYSERVER;DATABASE=MYDB;uid=MYUID;pwd=MYPWD;" providerName="System.Data.SqlClient" />

The code in my .asmx file with the webmethod in that calls SetUp that tries to get this value is below (with fluff removed)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService

    [WebService(Namespace = "http://webservice.demo.net/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Service1 : System.Web.Services.WebService
        // my DB object that runs all my DB queries, opens & kills connections etc
        private SqlDatabase SqlDatabase;

        public string GetNames(string ClientHash)
            // gets here then calls the SetUp method passing in "GetNames"

            // call SetUp to set up global objects - call on every web method

            //.. more code

    private void SetUp(string method)
        // create DB object
        this.SqlDatabase = new SqlDatabase();

        // **it is erroring here** - Using System.Configuration is added at top of class and there is a manual reference added to the DLL
        **this.SqlDatabase.ConnectionString = ConfigurationManager.ConnectionStrings["APIDatabase"].ToString();**

        // more code it cannot get to due to the above error

This is the simple consumer application project in C# that I am running on my PC

I've addeded the web reference as a proper reference to the console app and I can see all the web methods it has exposed including GetNames

I get to the web service call the method I want e.g, GetNames, but then it errors on the SetUp method as it tries to set the connection string for my SqlDatabase object (my data class).

This all works 100% in DEMO, pressing play, running on localhost etc.

However when I try and run the test console app on my PC connecting to the webservice I get the following error.

Start 09/10/2014 22:05:23

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at WebService.Service1.SetUp(String method) in C:\Users\me\Documents\Visual Studio 2010\Projects\WebService\WebService\WebService.asmx.cs:line 170 at WebService.Service1.GetNames(String ClientHash) in C:\Users\me\Documents\Visual Studio 2010\Projects\WebService\WebService\WebService.asmx.cs:line 44 at TestWebService.Program.Main(String[] args) in C:\Users\rreid\Documents\Visual Studio 2010\Projects\WebService\TestWebService\Program.cs: line 21

The code in my consumer app is below.

using System;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;

namespace TestWebService
    public class Program
        static void Main(string[] args)
            Console.WriteLine("Start " + DateTime.Now.ToString());

            // Create my web service object
            MyWebService.Service1 MyWebService = new MyWebService.Service1();

            string customerID = "6D104928-7633-4998-90C5-C01ABD4E010B"; // test a customer I know works

            // this is line 21 in my code (see ethe rror message above)
            // It errors in the SetUp method in the asmx file in the SetUp method when trying to get a connection string from the app.config file and set it to the database object I use
            string response = MyWebService.GetNames(customerID);

            Console.WriteLine("Stop " + DateTime.Now.ToString());


So I am following a guide (I've taken over the project) > http://support2.microsoft.com/kb/308359

And it seems to work in that the web service is found, the method can be run but a sub method in the project is failing.

-Am I supposed to rename web.config to something else e.g app.config? -Am I supposed to hardcode connection strings or can they be handled - I am not doing anything I have not done before in regards to getting configuration settings? -Do I need to add in extra code to my consumer application to allow IT to run SQL or get configuration options?

I am not sure what I am doing wrong as the code all seems correct. Plus no one at work can help me but are constantly on my back asking for it to be done!


Since you say this is the line that is erroring, one can only assume that "APIDatabase" was not found in your .config file. Calling .ToString() on a null object will cause that error. If you are able to edit and test the code, you should split it out and check that you were able to get a connection string.

ConnectionStringSettings csItem = ConfigurationManager.ConnectionStrings["APIDatabase"];
if (csItem == null)
    throw new Exception("Connection string not found for \"APIDatabase\"");

SqlDatabase.ConnectionString = csItem.ConnectionString;

The web.config should be set up with a connection string entry like so:

       <add name="APIDatabase"
            providerName="YOUR PROVIDER"
            connectionString="YOUR CONNECTION STRING" />

EDIT: Just noticed you supplied your web.config connection string entry in your question. Make sure it is located under the configuration element.

Double check that the server is supplying the correct web.config file. It's been awhile since I have used IIS, but I remember it seeming like there were several different versions of web.config in the UI. Make sure you added it to the correct one.

Am I supposed to rename web.config to something else e.g app.config?

Only if you are using an EXE to communicate with the web service. EXEs use app.config in the project and then when built get renamed to MyExecutableName.exe.config. If you're using a WinForm or Console app, then it won't read your web.config and will instead look for a .config like I described.