My BLL's function is like:
public Categorymaster GetByPrimaryKey(CategorymasterKeys keys)
{
return _dataObject.SelectByPrimaryKey(keys);
}
// above funciton is calling function written below
public Categorymaster SelectByPrimaryKey(CategorymasterKeys keys)
{
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandText = "dbo.[categorymaster_SelectByPrimaryKey]";
sqlCommand.CommandType = CommandType.StoredProcedure;
// Use connection object of base class
sqlCommand.Connection = MainConnection;
try
{
sqlCommand.Parameters.Add(new SqlParameter("@category_id", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, keys.Category_id));
if (MainConnection.State == ConnectionState.Closed)
{
MainConnection.Open();
}
IDataReader dataReader = sqlCommand.ExecuteReader();
if (dataReader.Read())
{
Categorymaster businessObject = new Categorymaster();
PopulateBusinessObjectFromReader(businessObject, dataReader);
return businessObject;
}
else
{
return null;
}
}
catch (Exception ex)
{
throw new Exception("Categorymaster::SelectByPrimaryKey::Error occured.", ex);
}
finally
{
MainConnection.Close();
sqlCommand.Dispose();
}
}
In my web page i have written below code:
BusinessLayer.Product_category_transFactory pctf = new Product_category_transFactory();
Categorymaster cm = cmf.GetByPrimaryKey(new CategorymasterKeys(catid));//throws exception when it returns null
Now the issue i am facing is that when there is no result for the query then it returns null and throw exception.
The same model is used in whole application. Please tell me what all minimum and optimum changes should i do handle null (when no result).
NOTE: i am using .net framework 4.0
Thanks