Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7160099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:20:32+00:00 2026-05-28T13:20:32+00:00

Loking for your insight as to why I’m getting this error, actually better yet,

  • 0

Loking for your insight as to why I’m getting this error, actually better yet, how to fix it. I have a nested gridview inside gridview. There may be a better way but at this point I’ve got the modal popup to work which also took forever, so unless this is completely stupid way to display master-detail then say so now.

I am able to pass the datakey id to the detail grid but that is where I get the null reference exception, I guess the bugger doesen’t exist yet, or is not visible. I mean, what-ever-the-reason is, how do I fix this? Thanks in advace.

aspx file:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:GridView ID="gvForum" runat="server" DataSourceID="odsForumApproval" DataKeyNames="id" Width="200px" 
      RepeatColumns="1" DataKeyField="id"  CssClass="gridview"
      AutoGenerateColumns="False" GridLines="None"  OnSelectedIndexChanged="_OnCommand">
      <AlternatingRowStyle CssClass="altbgcolor"  />
      <Columns>   
      <asp:TemplateField >
      <ItemTemplate>
      <asp:Label runat="server" ID="lblTitle" Text='<%# Bind("Title") %>' />                                

      <asp:Panel id="div" runat="server" align="center" class="confirm" style="display:none"  >
      <asp:GridView runat="server" ID="gvForumDetail" AutoGenerateColumns="False" DataKeyNames="id"
      EmptyDataText="No Forum Detail"  AllowPaging='true'
      AllowSorting="true" PageSize="5"  >
      <AlternatingRowStyle CssClass="altbgcolor" />
      <RowStyle VerticalAlign="Top" HorizontalAlign="Left" />
      <Columns>
        <asp:TemplateField>
          <ItemTemplate>
            <asp:Label runat="server" ID="lblTraining" Text='<%# Bind("title") %>' />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
          <ItemTemplate>
            <asp:Label runat="server" ID="lblCompletionDate" Text='<%# Bind("description") %>' />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
          <ItemTemplate>
            <asp:Label runat="server" ID="lblRecurence" Text='<%# Bind("MemberName") %>' />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
          <ItemTemplate>
            <asp:Label runat="server" ID="lblNotes" Text='<%# Bind("itemdate") %>' Width="200" />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowCancelButton="true" ShowEditButton="true" ShowDeleteButton="true" />
      </Columns>
      </asp:GridView>                               
      <br />
      <asp:Button ID="btnClose" runat="server" Text="Close" Width="50px" />                               
      </asp:Panel>   

      <ajaxToolKit:ModalPopupExtender ID="ModalPopupExtender1" runat="server"                                        
      TargetControlID="lblTitle" 
      PopupControlID="div" 
      CancelControlID="btnClose"  
      BackgroundCssClass="modalBackground" />

      </ItemTemplate>
      </asp:TemplateField>
      <asp:CommandField ShowSelectButton="True" ShowDeleteButton="true" />
      </Columns> 
      </asp:GridView>

code file:

public void _OnCommand(object sender, EventArgs e)
{
    ObjectDataSource ods = new ObjectDataSource();
    ods.ID = "ods_ForumDetail";
    ods.EnableViewState = true;
    ods.TypeName = "Forum";
    ods.SelectMethod = "GetForumPostByID";

    string id = "";
    int rowIndex = gvForum.SelectedIndex;
    id = gvForum.DataKeys[rowIndex].Value.ToString();
    ods.SelectParameters.Add("id", System.TypeCode.Int32, id);

    GridView gvForumDetail = (GridView)Master.FindControl("ContentPlaceHolder1").FindControl("gvForum").FindControl("gvForumDetail");
    gvForumDetail.DataSource = ods;
    gvForumDetail.DataBind();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:20:33+00:00Added an answer on May 28, 2026 at 1:20 pm

    You haven’t told where exactly you get the null-reference-exception.
    But why are you going this indirect way via MasterPage to find your Detail-Grid?

    The direct way is much easier and above all less error-prone:

    var gvForum = (GridView)sender;
    var gvForumDetail = (GridView)(gvForum.SelectedRow.FindControl("gvForumDetail"));
    

    Apart from that, your approach to find the nested GridView can never work. FindControl will only search the current NamingContainer for the given ID. Hence following might work, but is hardlinking the Master with one of it’s ContentPages what is always bad:

    (GridView)Master.FindControl("ContentPlaceHolder1").FindControl("gvForum");
    

    But this will not lead you to your nested GridView since…

    gvForum.FindControl("gvForumDetail");
    

    …will not search all GridViewRows in the GridView for gvForumDetail but only the NamingContainer of the GridView itself. A GridViewRow has it’s own NamingContainer(to enable to use same ID’s in ItemTemplate although multiple rows exist), so the only correct approach is to get the current SelectedRow and search there your nested Grid(see above).

    You haven’t yet shown where you DataBind your nested GridView. That should be done in RowDataBound of the outer GridView. That might be the reason why it’s null.
    Another reason for getting a NullRefernceException might be that you are possibly DataBinding your outer GridView on every postback in page_load instead of only if(!IsPostBack). That would deselect your Grid which causes SelectedRow to be nothing in my approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update 25/03/2011 I've marked this question as answered, while I don't yet have the
Hey everyone, I'm back and looking forward to more of your brilliance. I have
I have been looking at the YouTube Insight function and want to learn how
I need your help to add some insight into JNI on Android. I've been
Again, I'm here looking for your wisdom, in this case, I need to learn
Setup: I'm looking for insight into using a ABPersonViewContoller without getting data from the
Ok, new coder here looking for a little insight into this problem. So I
Now I am looking for your help to create index on these. Now this
I'm looking for your opinion on the technologies you have used successfully - or
If you work with FileOutputStream methods, each time you write your file through this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.