不应该抛出的异常
Exception thrown when it shouldn't be
private async Task<long> InsertCustomerRecord(CreateCustomerModel model)
{
Int64 phoneNumber = 0;
var isParsed = Int64.TryParse(model.PhoneNumber, out phoneNumber);
if (!isParsed)
phoneNumber = 0;
var USPSAddressValidatedId = (int)model.USPSAddressValidated;
try
{
IDataParameter[] parameters = new IDataParameter[]
{
new SqlParameter("@FirstName", model.FirstName.ToUpper()),
new SqlParameter("@LastName", model.LastName.ToUpper()),
//new SqlParameter("@MiddleInitial", model.MiddleInitial),
new SqlParameter("@AddressLine1",model.AddressLine1.ToUpper() ?? ""),
new SqlParameter("@AddressLine2",model.AddressLine2.ToUpper() ?? ""),
new SqlParameter("@City",model.City.ToUpper()),
new SqlParameter("@State",model.State.ToUpper()),
new SqlParameter("@CountyCode",model.CountyCode.ToUpper()),
new SqlParameter("@ZipCode",model.ZipCode),
new SqlParameter("@DateOfBirth",model.DateOfBirth.ToShortDateString()),
new SqlParameter("@Phone",phoneNumber),
new SqlParameter("@Email",model.EmailAddress.ToUpper()??""),
new SqlParameter("@PersonalRepresentative",model.CustomerClass.ToUpper()),
new SqlParameter("@ExpiryDate", model.ExpirationDate),
new SqlParameter("@CustomerClass", model.Customer_Class),
new SqlParameter("@USPSAddressValidated",USPSAddressValidatedId ),
new SqlParameter("@ImportFromLegacySystem", model.ImportFromLegacySystem)
};
return await InsertUpdateProcedure("RF_InsertCustomerCard", parameters);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return 0;
}
}
抛出的异常是
{"Object reference not set to an instance of an object."}
Data: {System.Collections.ListDictionaryInternal}
如果您怀疑某些内容可能为空:
model.EmailAddress
^^^^^^^^^^^^
this might be null
您需要对其使用空传播运算符:
model.EmailAddress?.ToUpper() ?? ""
^^
这意味着如果它是 null
,那么操作链的评估将在 .EmailAddress
returns null
的点停止,并且子-表达式 (model.EmailAddress?.ToUpper()
) 将解析为 null
。然后它将被空合并运算符 ??
拾取并变成您放在运算符右侧的任何内容。如果它是一个常量,比如 ""
那么整个表达式保证不为 null
您可以在 属性 和方法 return 值上多次使用这些运算符:
thing.Prop?.Method() ?? thing.OtherProp?.OtherMethod()?.AnotherProp ?? "Constant"
如果集合可能为空,还有一个空容忍索引器:
//in case the collection is null, or the thing at index 0 is null, or its Property is null...
thing.Collection?[0]?.Property?.Something() ...
^^
private async Task<long> InsertCustomerRecord(CreateCustomerModel model)
{
Int64 phoneNumber = 0;
var isParsed = Int64.TryParse(model.PhoneNumber, out phoneNumber);
if (!isParsed)
phoneNumber = 0;
var USPSAddressValidatedId = (int)model.USPSAddressValidated;
try
{
IDataParameter[] parameters = new IDataParameter[]
{
new SqlParameter("@FirstName", model.FirstName.ToUpper()),
new SqlParameter("@LastName", model.LastName.ToUpper()),
//new SqlParameter("@MiddleInitial", model.MiddleInitial),
new SqlParameter("@AddressLine1",model.AddressLine1.ToUpper() ?? ""),
new SqlParameter("@AddressLine2",model.AddressLine2.ToUpper() ?? ""),
new SqlParameter("@City",model.City.ToUpper()),
new SqlParameter("@State",model.State.ToUpper()),
new SqlParameter("@CountyCode",model.CountyCode.ToUpper()),
new SqlParameter("@ZipCode",model.ZipCode),
new SqlParameter("@DateOfBirth",model.DateOfBirth.ToShortDateString()),
new SqlParameter("@Phone",phoneNumber),
new SqlParameter("@Email",model.EmailAddress.ToUpper()??""),
new SqlParameter("@PersonalRepresentative",model.CustomerClass.ToUpper()),
new SqlParameter("@ExpiryDate", model.ExpirationDate),
new SqlParameter("@CustomerClass", model.Customer_Class),
new SqlParameter("@USPSAddressValidated",USPSAddressValidatedId ),
new SqlParameter("@ImportFromLegacySystem", model.ImportFromLegacySystem)
};
return await InsertUpdateProcedure("RF_InsertCustomerCard", parameters);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return 0;
}
}
抛出的异常是
{"Object reference not set to an instance of an object."}
Data: {System.Collections.ListDictionaryInternal}
如果您怀疑某些内容可能为空:
model.EmailAddress
^^^^^^^^^^^^
this might be null
您需要对其使用空传播运算符:
model.EmailAddress?.ToUpper() ?? ""
^^
这意味着如果它是 null
,那么操作链的评估将在 .EmailAddress
returns null
的点停止,并且子-表达式 (model.EmailAddress?.ToUpper()
) 将解析为 null
。然后它将被空合并运算符 ??
拾取并变成您放在运算符右侧的任何内容。如果它是一个常量,比如 ""
那么整个表达式保证不为 null
您可以在 属性 和方法 return 值上多次使用这些运算符:
thing.Prop?.Method() ?? thing.OtherProp?.OtherMethod()?.AnotherProp ?? "Constant"
如果集合可能为空,还有一个空容忍索引器:
//in case the collection is null, or the thing at index 0 is null, or its Property is null...
thing.Collection?[0]?.Property?.Something() ...
^^