Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8162667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:47:51+00:00 2026-06-06T18:47:51+00:00

I have a web method like this as part of a web service public

  • 0

I have a web method like this as part of a web service

public List<CustomObject> GetInformation() {
    List<CustomObject> cc = new List<CustomObject>();

    // Execute a SQL command
    dr = SQL.Execute(sql);

    if (dr != null) {
       while(dr.Read()) {
           CustomObject c = new CustomObject()
           c.Key = dr[0].ToString();
           c.Value = dr[1].ToString();
           c.Meta = dr[2].ToString();
           cc.Add(c);
       }
    }
    return cc;
}

I want to incorporate error handling into this so that if the no rows are returned or if dr is null or if something else goes wrong, I want to return the exception in the form of an error description. However, if the function is returning List<CustomObject> how can I also return an error message to the client when something goes wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:47:52+00:00Added an answer on June 6, 2026 at 6:47 pm

    I will create a wrapper class which has both your List of CustomObject and the error information as another Property.

    public class MyCustomerInfo
    {
      public List<CustomObject> CustomerList { set;get;}
      public string ErrorDetails { set;get;}
    
      public MyCustomerInfo()
      {
        if(CustomerList==null)
           CustomerList=new List<CustomObject>();  
      }
    }
    

    Now I will return an object of this class from my Method

    public MyCustomerInfo GetCustomerDetails()
    {
      var customerInfo=new MyCustomerInfo();
    
      // Execute a SQL command
        try
        {
          dr = SQL.Execute(sql);
    
          if(dr != null) {
             while(dr.Read()) {
               CustomObject c = new CustomObject();
               c.Key = dr[0].ToString();
               c.Value = dr[1].ToString();
               c.Meta = dr[2].ToString();
               customerInfo.CustomerList.Add(c);
             }
          }
          else
          {
              customerInfo.ErrorDetails="No records found";
          } 
       }
       catch(Exception ex)
       {
           //Log the error in this layer also if you need it.
           customerInfo.ErrorDetails=ex.Message;
       }     
      return customerInfo;    
    }
    

    EDIT: To make it more reusable and generic, It is a good idea to create a seperate class to handle this. and I will have this in my Baseclass as a property

    public class OperationStatus
    {
      public bool IsSuccess { set;get;}
      public string ErrorMessage { set;get;}
      public string ErrorCode { set;get;}
      public string InnerException { set;get;}
    }
    public class BaseEntity
    {
      public OperationStatus OperationStatus {set;get;}
      public BaseEntity()
      {
          if(OperationStatus==null)
             OperationStatus=new OperationStatus();
      } 
    }
    

    And Let all your Child entities involved in a transaction inherit from this base class.

       public MyCustomInfo : BaseEntity
       {
          public List<CustomObject> CustomerList { set;get;}
          //Your constructor logic to initialize property values
       } 
    

    Now from your Method you may set the values of OperationStatus property as needed

    public MyCustomInfo GetThatInfo()
    {
        var thatObject=new MyCustomInfo();
        try
        {
           //Do something 
           thatObject.OperationStatus.IsSuccess=true;
        }
        catch(Exception ex)
        {
          thatObject.OperationStatus.ErrorMessage=ex.Message;
          thatObject.OperationStatus.InnerException =(ex.InnerException!=null)?ex.InnerException:"";
        }
        return thatObject;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple web service like this: @WebService public class MyWebService { @WebMethod
I have a web service method where I would like to throw some custom
I have a Web Service with Just one Web Method that returns a boolean
I have a web service contains a web method with the url http://localhost/Service1.asmx?op=AddNumbers .
I have a web service method in which I create a particular type of
I have a simple web service method which I'm trying to convert: <WebMethod()> _
I have an ASP.NET 2.0 web method with the following signature: [WebMethod] public QueryResult[]
I have this project in which i am calling a web service, receiving rates
I have an ASP.NET web service (.asmx). My service is defined like the following:
I'm a new Rails programmer working on a web app. As part of this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.