Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

What is the exception you are getting? What do you mean returns null and throws Exception?

From putting this code into a class and executing a test on it, I get back an object or null when the record exists or does not respectively.

If you're getting an exception from the above call with cmf.GetByPrimaryKey, is the exception in the call to the constructor of CategorymasterKeys?

Here is the test I used:

[TestMethod()]
public void TestWhenPassingAnIdNotInTheResultSetToSelectByPrimaryKey_ThenNullIsTheResult()
{
    var target = new Class2();
    CategorymasterKeys keys = new CategorymasterKeys { Category_id = 0 };
    Categorymaster actual = target.SelectByPrimaryKey(keys);
    Assert.IsNull(actual);
}
share|improve this answer
add comment

You could use a nullable type for this. Then you can flag if it is actually null or just an empty result set. MSDN on nullable types

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.