tags:

views:

25

answers:

2

how I can call a variable (private Label ccc;) in another event:

private Label ccc;

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        Label Label1 = FindControlRecursive(Page, DropDownList1.SelectedValue) as Label;
        if (Label1 != null)
            this.ccc = lblCont;
            this.ccc.Text = Label1.Text;
    }


 public void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {


          this.ccc.Text = lblCont.Text;


            int bbb = Convert.ToInt32(lblCont.Text) - Convert.ToInt32(tbEnter.Text);
            if (bbb >= 0)
            {
                lblCont.Text = Convert.ToString(bbb);

            }
            else
            {
                ErrorDisplay.Text = "There are not enough tickets";
            }
        }
    catch (Exception ex)
    {
        ErrorDisplay.Text = ex.Message;
    }


}

I can not call this.ccc.Text = lblCont.Text; in "public void btnSubmit_Click"

It does (Object reference not set to an instance of an object.)

Thakns!!

A: 

Your ccc Label field must be assigned a value before you try to modify the .Text property. Since ccc is pointing to nothing you're getting that exception.

Make sure that before the btnSubmit_Click function is called that the ccc field is assigned a Label object or assign it before doing anything inside the function.

Nick Bedford
A: 

In addition to Nick's answer:

The DropDownList1_SelectedIndexChanged event is not guaranteed to fire on every postback - if the user submits the page without changing the selection, this.ccc will be null. Even if this.ccc was assigned in a previous postback, it will not be in the current one if the drop down list selection didn't change.

Hence why you need to make sure you assign it a Label instance somewhere else. For example in Page_Load or even in btnSubmit_Click just before your this.ccc.Text = lblCont.Text; line.

lethek