0
votes

In my project, initially, i am submitting which year,division and subject (of students) i want to access in the student database. Once i click the submit button, dynamic checkboxes are created according to the query submited above. However i am facing a problem while eventhandling these checkboxes to update the attendance of students for the above submited subject! please help! code :
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq;

namespace iso_generator
{
public partial class attendance : System.Web.UI.Page
{

    CheckBox cb1;
    System.Data.OleDb.OleDbConnection con;
    System.Data.OleDb.OleDbDataAdapter da;
    System.Data.DataSet ds;

    static int inc = -1;
    static int maxRows;
   // static int maxRows1;



   protected void Page_Load(object sender, EventArgs e)
    {
        con = new System.Data.OleDb.OleDbConnection();
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ISO\\DB\\db1.mdb";
        con.Open();


        string division = ddlDivision.Text;
        string year = ddlYear.Text;
        string subj = ddlSubject.Text;

        String query = "select * from student " +
            "where syear = '" + year + "' AND div ='" + division + "' order by roll";

        da = new System.Data.OleDb.OleDbDataAdapter(query, con);
        ds = new DataSet();
        da.Fill(ds, "student");
        maxRows = ds.Tables["student"].Rows.Count;





    }

    protected void ATTENDANCE_SUBMIT_Click(object sender, EventArgs e)
    {

        // Create a new HtmlTable object.
        HtmlTable table1 = new HtmlTable();
        table1.ID = ("YOURID");

        // Set the table's formatting-related properties.
        table1.Border = 1;
        table1.CellPadding = 1;
        table1.CellSpacing = 1;
        table1.BorderColor = "red";

        // Start adding content to the table.
        HtmlTableRow row;
        HtmlTableCell cell;
        int i = 0;


        for (int m = 0; m < 10; m++)
        {
            // Create a new row and set its background color.
            row = new HtmlTableRow();
            row.BgColor = "lightyellow";
            for (int j = 0; j < 5; j++)
            {
                if (i < maxRows)
                {
                    // Create a cell and set its text.
                    DataRow dRow;
                    dRow = ds.Tables["student"].Rows[i];

                    cb1 = new CheckBox();
              cb1.Checked = false;

                    cb1.ID = "" + dRow.ItemArray.GetValue(0).ToString();   //setting gr no to chechked box id//
                    cb1.Text = dRow.ItemArray.GetValue(2).ToString() + " : " +
                        dRow.ItemArray.GetValue(1).ToString();
                    cb1.Height = 50;
                    cb1.AutoPostBack = true;
                    cb1.CheckedChanged += new EventHandler
                                       (cb1_CheckedChanged);

                    cell = new HtmlTableCell();

                    cell.Controls.Add(cb1);
                    // Add the cell to the current row.
                    row.Cells.Add(cell);
                    i++;
                }

            }

            // Add the row to the table.
            table1.Rows.Add(row);
        }

        // Add the table to the page.
        //this.Controls.Add(table1);

        form1.Controls.Add(table1);




    }

    protected void cb1_CheckedChanged
            (object sender, EventArgs e)
    {
        string marks;
        if (cb1.Checked)
        {

            DataRow drow = ds.Tables["student"].Rows[0];  //go  to 1st row of database//
            string variable = drow.ItemArray.GetValue(0).ToString(); //get 1st student GR_NO value in variable//

            for (inc = 0; variable != cb1.ID; inc++)  //scan from 1st student GR_NO till the NON-CHECKED chckbox student GR_NO IN DATABASE//
            {
                drow = ds.Tables["student"].Rows[inc];
                variable = drow.ItemArray.GetValue(0).ToString();
            }

            if (ddlSubject.Text == "SUB1")
            {
                marks = drow.ItemArray.GetValue(7).ToString();   //7TH COLUMN IN DATABASE IS SUBJECT1 COLUMN//
                marks = marks + 1;                //INCREMENT THE ATTENDANCE BY 1//
                drow[7] = marks;
            }


        }

    }

}

1
The error is pretty clear: cb1 is null when you try to access it.CodeCaster
It looks like it doesn't even need to be made dynamically, just hide it. Edit or just disable save button until it can be usedSayse

1 Answers

0
votes

That is the behaviour. When there is postback, all variables are reset and reassigned.

EDIT: Ok the problem is that cb1 is just a variable. You use it every time you dynamically create a checkbox which means you can't access each checkbox by simply referring to cb1. You need to find the control by the ID you assigned to it OR with the same logic used to create it. I would advise giving your table an ID that you can use to retrieve it in subsequent postbacks.

HtmlTable table = Form1.FindControl("YourID");
for (int m = 0; m < table.Rows.Count; m++)
{
    for (int x = 0; x < table.Rows[m].Cells.Count, x++)
    {
        for (int c = 0; c < table.Rows[m].Cells[x].Controls.Count; c++)
        {
            if (typeof(table.Rows[m].Cells[x].Controls[c]) == typeof(CheckBox))
            {
                if (((CheckBox)table.Rows[m].Cells[x].Controls[c]).Checked)
                {     
                    //Your logic here
                }
            }
        }
    }
}

What's important when you take this approach is to have a way of identifying the learner who's checkbox you are currently busy with in order to update the DB. Please let me know if you need any clarity on anything.