Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am checking for a specific record in a list but when i enter the correct data the try block is not returning any value.....but it is working fine when there is no record.

C#
public List<Customer> get(string str)
       {
           //  var result = cutomerList.Find(x => x.CustomerName == str);

           try
           {

               var ss = cutomerList.Where(x => x.CustomerName == str).ToList();

               if (Convert.ToInt32(ss) == 0)
               {
                   Exception ex;

               }

               return ss;
           }

           catch (Exception ex)
           {

               MyFaultException fault = new MyFaultException();
               fault.Reason = "Record not found ";
               throw new FaultException<MyFaultException>(fault, new FaultReason(fault.Reason));
           }
Posted

1 solution

I think your problem comes from the line :
C#
if(Convert.ToInt32(ss)==0)

when there is no record, ss may be null. and the Convert method works.
But when you have one or more records, ss is not null and cannot be converted to an int and an exception is thrown by the Convert.ToInt32 method.

You should use that kind of code instead:
C#
if(ss==null || ss.Count==0) throw new Exception();


I have successfully tried the following code :
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Customer
    {
        public string CustomerName { get; set; }
        public int CustomerId { get; set; }
    }
    class Program
    {
        static private List<Customer> customerList;
        static public List<Customer> get(string str)
        {
            try
            {
                var ss = customerList.Where(x => x.CustomerName == str).ToList();
                if (ss == null || ss.Count == 0)
                {
                    throw new Exception();
                }
                return ss;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        static void Main(string[] args)
        {
            customerList = new List<Customer>() { new Customer() { CustomerName = "Name1", CustomerId = 1 }, new Customer() { CustomerName = "Name2", CustomerId = 2 } };
            var s = get("Name1");
            var s2 = get("Name3");
        }
    }
}


no exception with get("Name1") ; exception with get("Name3") ;
 
Share this answer
 
v2
Comments
shacha 9-Aug-13 11:26am    
When i keep like ss==0 i am getting an error like

Operator '==' cannot be applied to operands of type 'System.Collections.Generic.List<wcfexample.customer>' and 'int'

UPDATE:: oops i missed ss.count it worked thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900