gpt4 book ai didi

c# - 我怎么能有一个 NullReferenceException 但我的代码仍然按照我想要的方式工作?我该如何纠正它?

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:56 24 4
gpt4 key购买 nike

<分区>

我得到了一个 NullReferenceException,但我的代码仍然在做我想做的事情。怎么会这样?我怎样才能摆脱它?我不明白为什么它会抛出这个。

在线

            int result = (int)sqlCmd.ExecuteScalar();

在方法中:

    public override int AddCustomer(Customer customer)
{

try
{
SqlCommand sqlCmd = new SqlCommand("INSERT INTO [AXMAP].[Customers] ([LegalEntity], [OldSourceEnvironment], [OldCompanyCode], [OldAcctNum], [AccountNum], [Name], [BusinessUnit], [DefaultDimensionStr]) VALUES (@LegalEntity, @OldSourceEnvironment, @OldCompanyCode, @OldAcctNum, @AccountNum, @Name, @BusinessUnit, @DefaultDimensionStr)");
sqlCmd.Parameters.AddRange(new SqlParameter[] { new SqlParameter("@LegalEntity", customer.LegalEntity), new SqlParameter("@OldSourceEnvironment", customer.OldSourceEnvironment),
new SqlParameter("@OldCompanyCode", customer.OldCompanyCode), new SqlParameter("@OldAcctNum", customer.OldAccountNumber), new SqlParameter("@AccountNum", customer.NewAccountNumber),
new SqlParameter("@Name", customer.BusinessName), new SqlParameter("@BusinessUnit", customer.BusinessUnit), new SqlParameter("DefaultDimensionStr", customer.DefaultDimensionString) });

sqlCmd.Connection = OpenConnection();
int result = (int)sqlCmd.ExecuteScalar();
sqlCmd.Connection.Close();
return result;
}
catch (SqlException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}

我在这个方法中调用了 AddCustomer:

    protected void btnAdd_Click(object sender, EventArgs e)
{
try
{
int result = new Customer() { LegalEntity = ddlLegalEntity.Text.Trim(), OldSourceEnvironment = ddlOldSourceEnvironment.Text.Trim(),
OldCompanyCode = ddlOldCompanyCode.Text.Trim(), OldAccountNumber = txtOldAccountNumber.Text.Trim(), NewAccountNumber = txtNewAccountNumber.Text.Trim(),
BusinessName = txtBusinessName.Text.Trim(), BusinessUnit = txtBusinessUnit.Text.Trim(), DefaultDimensionString = txtDefaultDimensionString.Text.Trim()}.AddCustomer();

if (result > 0)
{
lvCustomers.EditIndex = -1;
lblCurrent.Text = "Added Successfully!";
ddlLegalEntity.Text = string.Empty;
ddlOldSourceEnvironment.Text = string.Empty;
ddlOldCompanyCode.Text = string.Empty;
txtOldAccountNumber.Text = string.Empty;
txtNewAccountNumber.Text = string.Empty;
txtBusinessName.Text = string.Empty;
txtBusinessUnit.Text = string.Empty;
txtDefaultDimensionString.Text = string.Empty;
}
else
{
lvCustomers.EditIndex = -1;
lblCurrent.Text = "Sorry, Add failed.";
}
}
catch (Exception ex)
{
lblCurrent.Text = ex.Message;
}
}

行尾:

            int result = new Customer() { LegalEntity = ddlLegalEntity.Text.Trim(), OldSourceEnvironment = ddlOldSourceEnvironment.Text.Trim(),
OldCompanyCode = ddlOldCompanyCode.Text.Trim(), OldAccountNumber = txtOldAccountNumber.Text.Trim(), NewAccountNumber = txtNewAccountNumber.Text.Trim(),
BusinessName = txtBusinessName.Text.Trim(), BusinessUnit = txtBusinessUnit.Text.Trim(), DefaultDimensionString = txtDefaultDimensionString.Text.Trim()}.AddCustomer();

客户类如下所示:

public class Customer
{

public string LegalEntity { get; set; }
public string OldSourceEnvironment { get; set; }
public string OldCompanyCode { get; set; }
public string OldAccountNumber { get; set; }
public string NewAccountNumber { get; set; }
public string BusinessName { get; set; }
public string BusinessUnit { get; set; }
public string DefaultDimensionString { get; set; }

public int AddCustomer()
{
try
{
return DataAccessLayer.GetDataAccessLayer().AddCustomer(this);
}
catch (Exception ex)
{
throw ex;
}
}
}

}

它可能在客户类中吗?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com