mardi 7 mars 2017

Object reference not set to an instance of an object -

this is a site to Order Tickets To movies, so i have a number of movies There is a DropDownList and when you change index its load data from database for another movie . There is a problem that when I order a chair , and then change a movie the CheckBox still checked.

.

I have 3 Functions.

One - PanelCare : Calls OtherFunctions and Builds the table.

Two - BuildSitsTable : Get number of chairs and return table with Checkbox Image and Label in each cell

Three - UncheckCB: Runs on the Table and Uncheck (Checked = false) all the Checkbox in the cells.

Codes:

One - PanelCare

 protected void PanelCare()
{
    DataBaseClass DBC = new DataBaseClass();
    DataSet DS = new DataSet();
    string strsql = "SELECT * FROM Movies";
    DS = DBC.GetDataSet(strsql, "Movies");
    Sits = int.Parse(DS.Tables[0].Rows[MovieNum]["MovieSits"].ToString());

    SitsPanel.Controls.Clear(); 
    SitsTable = BuildSitsTable(Sits);
    UncheckCB(SitsTable, Sits);
    SitsPanel.Controls.Add(SitsTable);
}

Two - BuildSitsTable

protected Table BuildSitsTable(int chairs)  
{
    int rows;
    if (chairs % 2 == 0 && chairs % 10 == 0)
    { rows = chairs / 10; }
    else
    { rows = chairs / 10 + 1; }

    Table Sits = new Table();

    int SitNum = 1;
    for (int i = 1; i <= rows; i++)
    {
        TableRow TR = new TableRow();

        for (int j = 1; j <= 10; j++)
        {
            if (SitNum <= chairs)
            {
                TableCell TC = new TableCell();
                Label LB = new Label();
                LB.Text = SitNum.ToString();

                CheckBox CB = new CheckBox();
                CB.ID = "CheckBox" + SitNum.ToString();
                CB.Attributes.Add("onclick", "YellowChair('" + SitNum + "')");

 // I tried to type here [CB.checked = false] But no succeed



                Image Img = new Image();
                Img.ImageUrl = "Images/BlueChair.png";
                Img.Width = 30;
                Img.ID = "ChairImg" + SitNum.ToString();


                TC.Controls.Add(CB);
                TC.Controls.Add(LB);
                TC.Controls.Add(Img);


                TR.Cells.Add(TC);
                SitNum++;
            }
        }
        Sits.Rows.Add(TR);
    }
    return Sits;
}

Three - UncheckCB

    protected void UncheckCB(Table SitsTable,int chairs)
{
    int rows;
    if (chairs % 2 == 0 && chairs % 10 == 0)
    { rows = chairs / 10; }
    else
    { rows = chairs / 10 + 1; }


    int SitNum = 1;

    for (int i = 0; i <= rows; i++)
    {
        for (int j = 0; j <= 10; j++)
        {
            if (SitNum <= chairs)
            {
                ((CheckBox)SitsTable.Rows[i].Cells[j].FindControl("CheckBox" + SitNum)).Checked = false;
                SitNum++;
            }
        }
    }

}

So , Why am i getting this Error? i scan the table the same way.

Or, if anyone know why the checkbox doesnt Reset although i make a brand new table.

Thanks.




Aucun commentaire:

Enregistrer un commentaire