0

Edit: This is not a duplicate question as my problem is a tad bit more complicated than a simple NullReferenceException.

This is the bit of code that is giving me problems:

 private void BindData()
{
    SqlConnection con = new SqlConnection(Connection.constr);
    con.Open();
    string[] Querys = new string[4];
    Querys[0] = "PopulateConstraintTable";
    Querys[1] = "PopulateModuleTable";
    Querys[2] = "PopulateFeatureTable";
    Querys[3] = "PopulateInterfaceTable";
    for (int n = 0; n < 4; n++)
    {
        SqlCommand cmd = new SqlCommand(Querys[n], con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@License", SqlDbType.Int).Value = 20000 + MyGlobals.CurrentOrgID;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable Table = new DataTable();
        da.Fill(Table);
        ((GridView)Page.FindControl("GridView" + (n + 1))).DataSource = Table;
        ((GridView)Page.FindControl("GridView" + (n + 1))).DataBind();
        cmd.ExecuteNonQuery();
    }
    con.Close();
}

This code SHOULD be populating 4 different Gridviews with the information in those stored procedures, but I get an error at lines

 ((GridView)Page.FindControl("GridView" + (n + 1))).DataSource = Table;
 ((GridView)Page.FindControl("GridView" + (n + 1))).DataBind();

that says:

 An exception of type 'System.NullReferenceException' occurred in App_Web_30v1z0on.dll but was not handled in user code

 Additional information: Object reference not set to an instance of an object.

Could someone walk me through what is wrong please? Thank you!

EDIT: Markup -

<asp:GridView 
    ID="GridView1" 
    runat="server" 
    CellPadding="6" 
    GridLines="Horizontal"
    Font-Names="Verdana" 
    Font-Size="10pt" 
    DataKeyNames="ConstraintID" 
    AutoGenerateColumns="false" 
    allowpaging="false"
    Width="730px">

    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" ForeColor="White" HorizontalAlign="Left" Height="25" Font-Bold="True" />   
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

    <Columns>...</Columns>



    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="false" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

    </asp:GridView>

<asp:GridView 
    ID="GridView2" 
    cellpadding="6"
    runat="server" 
    GridLines="Horizontal"
    Font-Names="Verdana" 
    Font-Size="10pt" 
    DataKeyNames="ModuleID" 
    AutoGenerateColumns="False"
    onrowcancelingedit="GridView2_RowCancelingEdit" 
    onrowediting="GridView2_RowEditing" 
    onrowupdating="GridView2_RowUpdating" 
    Width="730px">


    <HeaderStyle BackColor="#336699" ForeColor="White" HorizontalAlign="Left" Height="25" />            

    <Columns>...</Columns>

</asp:GridView>


<asp:GridView 
    ID="GridView3" 
    runat="server" 
    CellPadding="6" 
    GridLines="Horizontal"
    Font-Names="Verdana" 
    Font-Size="10pt" 
    DataKeyNames="FeatureID" 
    AutoGenerateColumns="false" 
    allowpaging="false"
    onrowcancelingedit="GridView3_RowCancelingEdit" 
    onrowediting="GridView3_RowEditing" 
    onrowupdating="GridView3_RowUpdating" 
    OnRowDataBound="GridView3_RowDataBound"
    Width="730px">

    <EditRowStyle Font-Bold="True" />

    <HeaderStyle BackColor="#336699" ForeColor="White" HorizontalAlign="Left" Height="25" />   

    <Columns>...</Columns>
</asp:GridView>



<asp:GridView 
    ID="GridView4" 
    runat="server" 
    CellPadding="6" 
    GridLines="Horizontal"
    Font-Names="Verdana" 
    Font-Size="10pt" 
    DataKeyNames="InterfaceID" 
    AutoGenerateColumns="false" 
    allowpaging="false"
    onrowcancelingedit="GridView4_RowCancelingEdit" 
    onrowediting="GridView4_RowEditing" 
    onrowupdating="GridView4_RowUpdating" 
    OnRowDataBound="GridView4_RowDataBound"
    Width="730px">

    <EditRowStyle Font-Bold="True" />

    <HeaderStyle BackColor="#336699" ForeColor="White" HorizontalAlign="Left" Height="25" />   

    <Columns>...</Columns>
</asp:GridView>

Answer: Seeing as how this post was bombarded with people claiming this was a duplicate, and so I can not post an answer, I'm going to put the answer here in an edit (All credit and praise be unto @ConnorsFan)

The solution was replacing the two problem lines of code with

 ((GridView)Master.FindControl("MainContent").FindControl("GridView" + (n + 1))).DataSource = Table;
 ((GridView)Master.FindControl("MainContent").FindControl("GridView" + (n + 1))).DataBind();
JDawg848
  • 121
  • 2
  • 10
  • What does `cmd.ExecuteNonQuery();` do in your code? Also change `"GridView" + (n + 1)` to `"GridView" + (n + 1).ToString()` – Alex Kudryashev Jun 24 '16 at 16:54
  • My Grid IDs that I'm trying to access are GridView1, GridView2, GridView3, and GridView4. It does nothing I believe, just leftover from when I was messing around earlier. Adding .ToString() gives the same error as before. – JDawg848 Jun 24 '16 at 16:59
  • 1
    As suggested by MoustafaS, please include in the question the part of the markup (from the ASPX file) where the GridViews are declared. – ConnorsFan Jun 24 '16 at 17:05
  • Are the GridViews inside another container? – ConnorsFan Jun 24 '16 at 17:11
  • I do not believe so. – JDawg848 Jun 24 '16 at 17:13
  • Try removing all elements that are inside the ... tags. Then check if you are still getting a null. If not, put the elements back in a few at a time to find out which ones are causing the problem. – Clint B Jun 24 '16 at 17:18
  • 1
    Do you have a master page? If so, you would have to do something like this: `(GridView)Master.FindControl("MainContent").FindControl("GridView1");` (see: http://stackoverflow.com/questions/3720590/find-a-control-on-a-page-with-a-master-page). – ConnorsFan Jun 24 '16 at 17:20
  • please paste full aspx markup.You only pasted gridview markup so we don't know if its child of some other server control or not. If you gridview is present in page then you should find it directly via `ID` property – KanisXXX Jun 24 '16 at 17:31
  • ConnorsFan, that worked! And following it, I can see why it works and what my problem was before! Thank you! – JDawg848 Jun 24 '16 at 17:51
  • 1
    For what it's worth. you don't need to find controls defined at the "top" level of a `Page`. You can just access them directly as GridView1...4. I would have placed them in an array or a List so they could be referenced via the same index as the for loop. No need to use FindControl at all. – fnostro Jun 24 '16 at 20:52

1 Answers1

2

This is null,

Page.FindControl("GridView" + (n + 1))

Can you paste the markup so that we check it.

MoustafaS
  • 1,991
  • 11
  • 20
  • You're right, by debugging I found that it is NULL, although I don't understand WHY it is NULL. Forgive me, but what would you like when you say to paste the markup? – JDawg848 Jun 24 '16 at 17:01
  • 2
    Markup is a term used to describe HTML and elements in .aspx/.cshtml files. He wants you to post the elements from your .aspx fle. – Clint B Jun 24 '16 at 17:07