0

I have to port an application from windows forms to ASP.net

Everything works fine untill I try to add data of a sale to a form using another form. In windows form it works fine, but when I try it in ASP I get the "Null Reference Exception was unhandeled by user code

This pops up when I try to access the "Add new sale from" from my main form.

The code in my "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 what goes wrong and where can I fix it?

Here is the constructor:

    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);
        }
    }

The constructor was not designed by me, this is the starting code I received that I needed to modify into asp.

Andrei
  • 57
  • 2
  • 10
  • 5
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Aug 13 '13 at 19:35
  • If the one line in the `GetData()` method is what's throwing the error then one of the objects in that line is `null`. In your debugger put a breakpoint on that line and inspect the objects. See which one/ones is/are `null`. Also, your statement of "when I try to access the form from my main form" sounds a bit disconcerting. How are you trying to do this, exactly? In WinForms you can instantiate new forms and use them directly. This is not the case in WebForms. While you technically *can* create an object of a Page, you shouldn't ever need to or want to. – David Aug 13 '13 at 19:36
  • On a side note, notice that a constructor that takes so many parameters is a clear sign of extremely poor design. You'll make your life easier by refactoring that. If you're the author, you'd also benefit from a basic programming course. – Geeky Guy Aug 13 '13 at 19:45
  • @Andrei are you sure that is **all** your code in the page? Assuming the page markup has the controls declared, the code you posted would not throw a NullReference exception but it will throw other kinds. I am going to delete my answer until you post the full code and markup. – Icarus Aug 13 '13 at 19:56

1 Answers1

1

Your problem is one of these two lines of code:

  1. Verify that the PitchCostTextbox.Text is not null or empty in the line Convert.ToDouble(PitchCostTextbox.Text) by doing this instead:

    double PitchCost = 0.0;
    if(!String.IsNullOrEmpty(PitchCostTextbox.Text))
    {
        PitchCost = Convert.ToDouble(PitchCostTextbox.Text);
    }
    
  2. Verify that the NumPitchesTextbox.Text is not null or empty in the line Convert.ToInt32(NumPitchesTextbox.Text).

    int NumPitches = 0;
    if(!String.IsNullOrEmpty(NumPitchesTextbox.Text))
    {
        NumPitches = Convert.ToInt32(NumPitchesTextbox.Text);
    }
    
Karl Anderson
  • 33,426
  • 12
  • 62
  • 78