HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Handling Null Result with class object

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
handlingresultwithnullobjectclass

Problem

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 faci

Solution

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);
}

Code Snippets

[TestMethod()]
public void TestWhenPassingAnIdNotInTheResultSetToSelectByPrimaryKey_ThenNullIsTheResult()
{
    var target = new Class2();
    CategorymasterKeys keys = new CategorymasterKeys { Category_id = 0 };
    Categorymaster actual = target.SelectByPrimaryKey(keys);
    Assert.IsNull(actual);
}

Context

StackExchange Code Review Q#3339, answer score: 4

Revisions (0)

No revisions yet.