3
\$\begingroup\$

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

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

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);
}
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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