I have a particular function which checks if a particular row matches the query condition.If it does then I retrieve the value and store it in my variable , if it doesnot have a value I insert it.However I have a try{} catch{} block which determines if it exists or not.If it goes inside a catch block , I call a function which will insert a record in that table , I would like to know is this approach good ,Calling a function inside a catch block , is it ok, what are the alternatives to find out if my query returns a result or not.Here is my code
public void CheckApplicationNo(string TableName,string BranchNo)
{
try
{
var appno = (from app in dt.sys_Keys
where app.TableName == TableName && app.BranchNo.ToString() == BranchNo
select app.NewValue).Single();
Global.ApplicationNo = appno.ToString();
UpdateApplicationNo("Data_Customer_Log", Global.BranchNo);
}
catch (Exception ex)
{
InsertApplicationNo();
}
}
Any suggestons are welcome.
Thanks.
You should use .SingleOrDefault()
It returns null if there are no results.