0

I have the following query:

public static string GetCustomerName(string customerNo)
{
   string query = "query to get customer";
   var myConn= new MYConnection();
   using (SqlConnection con = new SqlConnection(myConn.MYConnectionString))
   {
       con.Open();
       SqlCommand cmd = new SqlCommand(query, con);
       cmd.Parameters.Add("@customerNo", SqlDbType.NVarChar).Value = customerNo;
       object result = cmd.ExecuteScalar();
       return result == DBNull.Value ? String.Empty : (string)result;
   }

}

I'm calling the method above like this:

string customerName = GetCustomerName(CustomerID);

if (customerName.Contains(Constants.Company.CompanyName))
{

    Additional Logic...
}

However, I'm getting a Object Reference Null error if my method doesn't return a customer name. I would think that the GetCustomer method would return an empty string.

If I change the call to get the CustomerName to below, it works perfectly.

string customerName = GetCustomerName(emailAndSTCodeInfo.CustomerID);
if (String.IsNullOrEmpty(customerName))
{
    customerName = "";
}
if (customerName.Contains(Constants.Chase.ACCOUNT_NAME))
{
    Additional Logic
}

So, my question is, what would be the proper way of handling this if my GetCustomer method doesn't find a record and returns null. I'm currently using the working code above but it seems like a hack or something.

Any help would be greatly appreciated.

5
  • When you debug, what is the value of result prior to your return statement? Commented Apr 30, 2013 at 18:01
  • 3
    Please make more effort when formatting your question. There's no need to have the code starting a third of the way across the window. Commented Apr 30, 2013 at 18:01
  • Do you ever expect GetCustomerName() to return null? If not and if your logic depends on CustomerName, then I'd say throwing exception and catching it on the upper layer (caller method) would be the best way to handle this. Commented Apr 30, 2013 at 18:02
  • this may sound funny, but I can't actually 'debug' this. It's a windows service that is very large. For some reason, corporate has some odd email setup that blocks this program from sending emails from my development machine. I know, it makes it a nightmare. It's suppose to be fixed soon...Not soon enough though. Commented Apr 30, 2013 at 18:03
  • You can still write tests. Commented Apr 30, 2013 at 18:04

2 Answers 2

2

ExecuteScalar returns null if no record is returned.

To guarantee that GetCustomerName never returns null, you could change the last line to

return Convert.ToString(result);

Convert.ToString(object) returns an empty string if the argument is either null or DBNull.Value.

Sign up to request clarification or add additional context in comments.

Comments

2

If a query returns no rows, then executing it with ExecuteScalar will return null, not DBNull.Value.

So your GetCustomerName method needs to check for a null return value as well as DBNull.Value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.