我必须将应用程序从Windows窗体移植到ASP.net
一切正常,直到我尝试使用其他表单将销售数据添加到表单中。在Windows窗体中它工作正常,但是当我在ASP中尝试它时,我得到“Null引用异常被用户代码取消了
当我尝试从我的主表单访问“添加新销售”时,会弹出。
我的“添加销售表单”中的代码是:
public partial class AddSaleForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public Sale GetData()
{
return new Sale(IdTextbox.Text, DateTextbox.Text, LocationTextBox.Text, Convert.ToDouble(PitchCostTextbox.Text), Convert.ToInt32(NumPitchesTextbox.Text), CharityCheckbox.Checked, CharityNameTextbox.Text, CateringCheckBox.Checked);
}
知道出了什么问题,我在哪里可以解决它?
这是构造函数:
namespace AntiqueSale
{
[Serializable]
public class Sale : IComparable<Sale>
{
private String saleId;
public String SaleId
{
get { return saleId; }
set { saleId = value; }
}
private String saleDate;
private String location;
private double pitchCost;
private int numPitches;
private bool charity;
public bool Charity
{
get { return charity; }
set { charity = value; }
}
private String charityName;
private bool catering;
public Sale(String saleId,
String saleDate,
String location,
double pitchCost,
int numPitches,
bool charity,
String charityName,
bool catering)
{
this.saleId = saleId;
this.saleDate = saleDate;
this.location = location;
this.pitchCost = pitchCost;
this.numPitches = numPitches;
this.charity = charity;
this.charityName = charityName;
this.catering = catering;
}
public override string ToString()
{
String str;
String charityString;
String cateringString;
String charityNameString;
if (charity)
{
charityString = "Yes";
charityNameString = charityName;
}
else
{
charityString = "No";
charityNameString = "N/A";
}
if (catering)
{
cateringString = "Yes";
}
else
{
cateringString = "No";
}
str = String.Format("{0}: {1}: {2}: {3}: {4}: {5}: {6}: {7}",
saleId,
saleDate,
location,
pitchCost,
numPitches,
charityString,
charityNameString,
cateringString);
return str;
}
public int CompareTo(Sale sale)
{
return this.SaleId.CompareTo(sale.SaleId);
}
}
构造函数不是我设计的,这是我收到的启动代码,我需要修改为asp。
答案 0 :(得分:1)
您的问题是以下两行代码之一:
通过执行此操作,验证行PitchCostTextbox.Text
中的Convert.ToDouble(PitchCostTextbox.Text)
不为空或为空:
double PitchCost = 0.0;
if(!String.IsNullOrEmpty(PitchCostTextbox.Text))
{
PitchCost = Convert.ToDouble(PitchCostTextbox.Text);
}
确认NumPitchesTextbox.Text
行中Convert.ToInt32(NumPitchesTextbox.Text)
不为空或为空。
int NumPitches = 0;
if(!String.IsNullOrEmpty(NumPitchesTextbox.Text))
{
NumPitches = Convert.ToInt32(NumPitchesTextbox.Text);
}