次のクエリがあります。
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;
}
}
上記のメソッドを次のように呼び出しています。
string customerName = GetCustomerName(CustomerID);
if (customerName.Contains(Constants.Company.CompanyName))
{
Additional Logic...
}
ただし、メソッドが顧客名を返さない場合、Object Reference Null エラーが発生します。GetCustomer メソッドは空の文字列を返すと思います。
CustomerName を取得する呼び出しを以下に変更すると、完全に機能します。
string customerName = GetCustomerName(emailAndSTCodeInfo.CustomerID);
if (String.IsNullOrEmpty(customerName))
{
customerName = "";
}
if (customerName.Contains(Constants.Chase.ACCOUNT_NAME))
{
Additional Logic
}
したがって、私の質問は、GetCustomer メソッドがレコードを見つけられず、null を返す場合、これを処理する適切な方法は何であるかということです。現在、上記の作業コードを使用していますが、ハックか何かのようです。
どんな助けでも大歓迎です。