Error 401 when using ObjectDatasource to call a web service

August 31, 2008 at 5:29 pm 6 comments

Recently, I used a ObjectDataSource object to call a web service from an ASP.Net web site. Much to my surprise, I got the following error when I run it:

The request failed with HTTP status 401: Unauthorized.

In addition, the error stated the routine resulted in the following source error:

Line 111:        [return: System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
Line 112:        public string Test() {
Line 113:            object[] results = this.Invoke(“Test”, new object[0]);
Line 114:            return ((string)(results[0]));
Line 115:        }

Apparently, the error occur in line 113, when the web service is invoked. What is annoying is that the error does not appear in the security event log. Reading the IIS log file did not result in further understanding. This post did however gave me a big clue.

What appears to be happening is that the authentication credentials are not being passed to the web service. One would expect that with reflection, the ObjectDataSource would have some seamless way to pass the credentials, but there is not. You will have to create an instance of the web service proxy. A good place to do this is when the ObjectDataSource is being created. Double-click on your objectDataSource’s Object Creating event. This will create an event handler.

protected void MyObjDS_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
MyWebService.Service webProxy = new MyWebService.Service();
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
e.ObjectInstance = webProxy;

In the above example, I am passing my default credential to the web service. This occur was the ObjectDataSource is being created. Note that I am assuming that the URL of the webProxy is already set when you added the web service as a web reference. If you have different web service for each environment (dev, test, prod, etc), you can put the URL in the web.config and add an additional line to set the URL.

protected void MyObjDS_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
MyWebService.Service webProxy = new MyWebService.Service();
webService.Url = ConfigurationManager.AppSettings[“MyWebServiceURL”];
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
e.ObjectInstance = webProxy;


Entry filed under: .Net.

Fixing “Carrier Stall” error on a Lexmark Z52 Printer Creating and Managing CentOS virtual machine under Virtualbox

6 Comments Add your own

  • 1. sam  |  February 26, 2009 at 10:29 am

    I am currently having this exact problem and its driving me mad! But i have tried following your advice but still having issues… I am new to this so please excuse me if I ask stupid questions! When you say double click on your object data souces object for me this created an event like this:

    protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)

    is this right? And also I am lost at the final bit of code, where you have e.ObjectInstance, what is the ObjectInstance?
    Thanks in advance for any help

    • 2. paulsiu  |  March 1, 2009 at 11:29 pm

      It’s been a while since I looked at the code. What I am basically doing is to setup an event handler for the object create event. By double-clicking on the object, you generated a select event, which is not what you want. Right-click on the object and get properties. There should be two tabs in Visual Studio. The one with the lighting bolt is the event tab. Click on that. You should see a list of events. If you double-click on the event, it will create the event handler for that event if you have not done so.

      e.ObjectInstance is reference to the instance of the object. When you click on the object, it sends a message to the event handler. The reference to the calling object is in the e.ObjectInstance, so you know who called.

  • 3. Richard  |  May 21, 2009 at 8:33 pm

    I love this article, thanks dude!

  • 4. Julie  |  July 8, 2009 at 9:40 am

    FYI this works great as long as you don’t have a “MasterPage”. If you do, the event won’t fire before the call is made to the web service… not sure why. If anyone has any ideas on how to make this work with a “MasterPage” it would be greatly appreciated.

  • 5. subsoft  |  January 20, 2013 at 7:02 pm

    Love it … that’s what i was looking for 😉

  • 6. ideas practicas para el hogar  |  September 23, 2014 at 7:19 am

    This pɑragraph provide сlear idea for the new people oof Ƅlogging, that truly
    how to do running a blog.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


August 2008
« Jul   Sep »

Most Recent Posts

%d bloggers like this: