1

Okay so I'm adapting a C# program to an asp program and I have a main form which contains a list box and another which adds new information to the list box. I can fill in the 2nd form and hold values in Application["getData"]; but when I go to the other page I need to run the following code.

public void AddGig()
    {
        AddGigForm frm = new AddGigForm();

        if (Application["getData"] != null)
        {
            Application["saveData"] = Application["getData"];
            gigList.addGig(frm.GetData());


            UpdateListbox();
        }

I run into problems at gigList.addGig as it goes back to the method GetData() on the 2nd form. I just have no idea what else to use.

GetData method:

public GigOpportunity GetData()
    {


            Application["GetData"] = new GigOpportunity
                (txtId.Text, gigDate.SelectedDate, txtVenue.Text, txtGenre.Text,
                Convert.ToDouble(txtCost.Text), Convert.ToInt32(txtCapacity.Text), chkHeadliner.Checked, txtMainAct.Text, chkEngineer.Checked);


            return new GigOpportunity(txtId.Text, gigDate.SelectedDate, txtVenue.Text, txtGenre.Text, Convert.ToDouble(txtCost.Text), Convert.ToInt32(txtCapacity.Text), chkHeadliner.Checked, txtMainAct.Text, chkEngineer.Checked);
    }

addGig method:

public void addGig(GigOpportunity gigOpportunity)
    {
        //Make sure a gig with this id does not already exist

        foreach (GigOpportunity g in gigList)
        {
            if (g.GigId == gigOpportunity.GigId)
            {
                throw new DuplicateIdException();
            }
        }

        gigList.Add(gigOpportunity);
    }
Warren1008
  • 13
  • 7
  • What is AddGigForm? The best way to use data through pages is to use `Session` object for the current user not `Apllication` object as for all session they will share the same data. –  May 03 '14 at 20:11
  • AddGigForm is the 2nd form I use which has the fields used to add another gig to the 1st form. Is there any problem with using `application`? – Warren1008 May 03 '14 at 20:23
  • I am not sure to understand well, `gigList` is it another list? And `addGig` could you show the code for this method. When you create the object AddGigForm, you can't retrieve data from it also. As it is a new object. And where do you call this method? Could you paste the all code if possible of this class. –  May 03 '14 at 20:31
  • gigList is a List, sure I'll add it to the question and I call gigList.addGig from GigOpportunity class, I'll post that method too. – Warren1008 May 03 '14 at 20:49

1 Answers1

1

I understand now your problem. You musn't think like in windows form. You declared those method inside other form. When you call it by assigning a new Form object you will not get the value inside as they have been disposed after you change the page.

So in your case:

  if (Application["getData"] != null)
  {
        Application["saveData"] = Application["getData"];
        gigList.addGig((GigOpportunity)Application["getData"]);


        UpdateListbox();
  }

But I will suggest you to use Session object instead of Application object. You can read more about it here

So you have to do like this:

  if (Session["getData"] != null)
  {
        Session["saveData"] = Session["getData"];
        gigList.addGig((GigOpportunity)Session["getData"]);


        UpdateListbox();
  }

You don't need to create the second form object AddGigForm and you must be sure to call your methodGetData in the form where is it declared to assign your Session.

Community
  • 1
  • 1
  • Wow thank you it worked! I realise the problem was gigList.addGig(Whatever) I just didn't know what I need to put inside. I think I may use sessions after being advised to by a few people now. Thanks again – Warren1008 May 03 '14 at 21:17