I think your problem comes from the line :
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:
if(ss==null || ss.Count==0) throw new Exception();
I have successfully tried the following code :
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") ;