locked
While Designing "Gridview", fixing "NullReferenceException" RRS feed

  • Question

  • User522058128 posted

    hello, I am new in .Net programming. it's my first webpage. and I have a problem with "Object reference not set to an instance of an object."

    I am designing "Gridview" with Toolbox - Data Source Configuration Wizard. I have some empty columns in my database. I learned that this error because of empty cells in database. 

    in my .aspx file the codes are like below;

    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"></asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ukracingConnectionString %>" ProviderName="<%$ ConnectionStrings:ukracingConnectionString.ProviderName %>" SelectCommand="SELECT dor, tor, track, runner, odd, result FROM engbet365"></asp:SqlDataSource>

    in my .aspx.cs file the codes are like below;

    {
        public partial class EnglandAll : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
     
            }
     
            protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
     
            }
        }
    }

    and in .aspx.designer.cs file, the codes are like below;

    public partial class EnglandAll {

    /// <summary>
    /// GridView1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.GridView GridView1;

    /// <summary>
    /// SqlDataSource1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.SqlDataSource SqlDataSource1;
    }

    finally...

    at the end of the progress with "Data Source Wizard" , when I test the query, I took this error. 

    this wizard is so good for beginners I think, and I want to use this. 

    Is there quick way like data binding or something like that, recover this error? I saw that "Gridview" "Properties" box there is an option "Data.Binding". I tried that but I couldn't managed. 

    How should I manage this problem?

    Thanks for your interest. 

    Tuesday, January 30, 2018 11:50 AM

Answers

All replies

  • User-1838255255 posted

    Hi tutovavera,

    After reading your description and code, i make a test in my side, when my table column exist null value, when i bind it through data source wizard, but can not meet this issue. 

    Here is the test result:

    So i think this issue is not related to the bind data through data source wizard. About the NullReferenceException exception, usually caused by can not find the value or can not found the object. here is a article about introducing this type error: 

    What is NullReferenceException? Object reference not set to an instance of an object: 

    https://stackify.com/nullreferenceexception-object-reference-not-set/ 

    You could use ado.net bind datasource to gridview, please check the following sample tutorial: 

    https://www.aspsnippets.com/Articles/How-to-bind-GridView-with-DataReader-in-ASPNet-using-C-and-VBNet.aspx 

    Best Regards,
    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 31, 2018 6:58 AM
  • User522058128 posted

    thanks for your interest. I did it. I noticed that you are using MSSQLServer. 

    I changed my database server from MySQLServer to MSSQLServer. Everything is ok now. I am using Bootcamp Windows, and I think this is a driver problem. 

    Have a nice day.

    Wednesday, January 31, 2018 12:50 PM