ASP.net Checkbox Error: Object reference not set to an instance of an object. C# RRS feed

  • Question

  • User-49671077 posted

    Error Object reference not set to an instance of an object with the following code: c#

    <input type="checkbox" id="AppEnroll" name='AppEnroll' onchange="javascript:Calculation();" />
    while (reader1.Read())
            bool AppPhotoChk = (bool)reader1["enroll"];
            if  (AppPhotoChk == true)
           AppEnroll.Checked = true  // here is error throw (Object reference not set to an instance of an object)

    Same error on:  AppEnroll.Checked = (reader.GetBoolean(reader.GetOrdinal("enroll")));

    Can Please someone help!

    Friday, October 23, 2020 11:06 PM


  • User475983607 posted

    The checkbox shown in your code snippet is an HTML input NOT a server control.  You cannot access the checkbox input as if it is a Server control.  Use the following syntax to access the value.


    Secondly, only checked HTML checkbox values are submitted by the browser.   You'll need to handle null.

    Lastly, you should always use standard framework constructs.  In this case, the ASP.NET CheckBox control.  You can also add runat="server" to the HTML input which will allow you to access the input by Id in the code behind.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 23, 2020 11:20 PM