Click here to Skip to main content
15,902,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have trawled the net over the last couple of days to find a way of solving my issue, quite a few have done it a different way to the way I've done it but I need to keep my project running the same

I'm using Visual Studio 2008, SQL Server 2008 with AJAX and C#

I've got a grid which is returning data as a cumulative total, it all works without an issue, have attached a collapsible panel extender and another grid to display sub elements, this is where I'm having difficulty as I cannot see the second gridview.

When it gets to the following I get an error "Object not set to an instance of an object"
((GridView)FindControl("GridView2)).DataSource = reader1;

Any assistance most appreciated, code below, have had to top and tail it due to content

What I have tried:

<asp:ScriptManager ID="ScriptManager1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmplID" CellPadding="4" ForeColor="#333333" GridLines="None" Width="700px" onrowcommand="GridView1_RowCommand"
<rowstyle backcolor="#EFF3FB">
<columns> <asp:BoundField DataField="EmpID" HeaderText="EmpID" InsertVisible="False" ReadOnly="True" SortExpression="EmpID" Visible="False" />
<asp:TemplateField HeaderText="Project/Order" SortExpression="linknumber" HeaderStyle-HorizontalAlign="Center">
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Bind("EmpID") %>'>

<cc1:CollapsiblePanelExtender ID="CollapsiblePanelExtender1" runat="server"
TargetControlID="Panel1" CollapsedSize="0"
Collapsed="true" ExpandControlID="LinkButton1" CollapseControlID="LinkButton1"
AutoCollapse="false" AutoExpand="false"
ScrollContents="true" ExpandDirection="Vertical">

<asp:Panel ID="Panel1" runat="server">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="EmpID">
<columns> <asp:TemplateField HeaderText="EmpID">
<asp:Label ID="lblAbID" runat="server" Text='<%# Bind("AbID") %>'>

<itemstyle width="50px" horizontalalign="Center">

<asp:TemplateField HeaderText="EmpID">
<asp:Label ID="lblEmpIDD" runat="server" Text='<%# Bind("Date","{0:dd/MM/yyyy}") %>'>

<itemstyle width="50px" horizontalalign="Center">

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>

<asp:TemplateField HeaderText="EmpID">
<asp:Label ID="lblEmpID" runat="server" Text='<%# Bind("EmpID") %>'>

<itemstyle width="50px" horizontalalign="Center">

<asp:TemplateField HeaderText="SerNo">
<asp:Label ID="lblESerNoID" runat="server" Text='<%# Bind("SerNo") %>'>

<itemstyle width="100px" horizontalalign="Center">

C# Code
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
SQLQueryDD = "select e.EmployeeID, e.SerNo from tbl_Emp e where e.EmpID = 12";

FullStringDD = SQLQueryDD;
SqlCommand cmd = new SqlCommand(FullStringDD, connAMP);


GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
reader1 = cmd.ExecuteReader();
((GridView)FindControl("GridView2")).DataSource = reader1;
catch (Exception ex)

Updated 8-Jun-16 23:45pm

1 solution

Cracked it

reader1 = cmd.ExecuteReader();
 GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
 int i = row.RowIndex;
 GridView GridView2 = row.FindControl("GridView2") as GridView;
 GridView2.DataSource = reader1;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900