c# - Null Reference Exception error in ASP.net -
this question has answer here:
- what nullreferenceexception, , how fix it? 32 answers
i have port application windows forms asp.net
everything works fine untill try add data of sale form using form. in windows form works fine, when try in asp "null reference exception unhandeled user code
this pops when try access "add new sale from" main form.
the code in "add sale form" is:
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); }
any idea of goes wrong , can fix it?
here constructor:
namespace antiquesale { [serializable] public class sale : icomparable<sale> { private string saleid; public string saleid { { return saleid; } set { saleid = value; } } private string saledate; private string location; private double pitchcost; private int numpitches; private bool charity; public bool charity { { 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); } }
the constructor not designed me, starting code received needed modify asp.
your problem 1 of these 2 lines of code:
verify
pitchcosttextbox.text
not null or empty in lineconvert.todouble(pitchcosttextbox.text)
doing instead:double pitchcost = 0.0; if(!string.isnullorempty(pitchcosttextbox.text)) { pitchcost = convert.todouble(pitchcosttextbox.text); }
verify
numpitchestextbox.text
not null or empty in lineconvert.toint32(numpitchestextbox.text)
.int numpitches = 0; if(!string.isnullorempty(numpitchestextbox.text)) { numpitches = convert.toint32(numpitchestextbox.text); }
Comments
Post a Comment