Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello fellow developers/programmers, got some problems here. I cannot get the datakey from my child gridview when i click the imagebutton.

Using my code, im getting an error saying
Object reference not set to an instance of an object.
Pointing on this area
Dim row As GridViewRow = DirectCast(ImageDelete.NamingContainer, GridViewRow)

Could you help me debug this problem.? Many thanks
    <asp:GridView ID="grdCategory" runat="server" AutoGenerateColumns="False"
        Width="900px" DataKeyNames="CourseCatID" Font-Names="verdana,arial,helvetica,sans-serif" Font-Size="8pt" CellPadding="4" ForeColor="#333333" GridLines="None">
    <columns>
    <asp:BoundField  HeaderText = "CourseCatID" DataField="CourseCatID" />

    <asp:TemplateField HeaderText="Course Category">
    <itemtemplate>
    <a href="java<!-- no -->script:toggleDiv('mydiv<%# Eval(" coursecatid=")%>')"><asp:Image ID="img" onclick="javascript:Toggle(this);" runat="server" ImageUrl="~/Images/plus.gif"
    ToolTip="Expand" Width="7px" Height="7px" ImageAlign="AbsMiddle"/></a>
    <asp:Label ID="lbllastname" Height="15px" runat="server" Text='<%# Eval("CourseCatName")%>'>
    <div id="mydiv<%# Eval(" coursecatid=")%>" style="display:none">
    <br />      
    <asp:ImageButton ID="ImageAdd" Height="17px" ImageUrl="Images/addCourse.png" runat="server" CommandName="cmdAdd" CommandArgument='<%# Eval("CourseCatID") %>' OnClick="ImageAdd_click"
    /><br /><br />

            <asp:GridView ID="grdCourse" runat="server"  Width="800px" HorizontalAlign="Right" AutoGenerateColumns="False" align="center" DataKeyNames="CourseCode" Font-Names="verdana,arial,helvetica,sans-serif" Font-Size="8pt" BackColor="White"
            GridLines="Vertical">
            <columns>
            <asp:TemplateField HeaderText="CourseName" HeaderStyle-HorizontalAlign="Left">
             <itemtemplate>
                   <asp:Label ID="lblCoursename" runat="server" Text='<%# Eval("CourseName")%>' Width="300px" Height="10px">

             </itemtemplate>

           <asp:TemplateField HeaderText="Course Code" HeaderStyle-HorizontalAlign="Left">
             <itemtemplate>
                   <asp:Label ID="lbllastname" runat="server" Text='<%# Eval("CourseCode")%>' Height="10px">
            </itemtemplate>


         <asp:BoundField HeaderText = "Duration" DataField="Duration" HeaderStyle-HorizontalAlign="Left" />

         <asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Left" >
             <itemtemplate>
                  <asp:ImageButton ID="ImageLink" Height="15px" ImageUrl="Images/edit.png" runat="server" CommandName="cmdlink" PostBackUrl='<%#"CourseUpdate.aspx?CourseName=" & Eval("CourseName")%>' />
             </itemtemplate>
             <itemstyle horizontalalign="Left" />


         <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Left"  >
             <itemtemplate>
                 <asp:ImageButton ID="ImageDeleteCourse"  Height="15px" ImageUrl="Images/Delete.png" runat="server" CommandName="cmdDelete" CommandArgument='<%# Eval("CourseCode") %>' onclick="ImageDeleteCourse_Click"
                    />
             </itemtemplate>
             <itemstyle horizontalalign="Left" />


        <asp:CommandField ShowDeleteButton="True"  />
        </columns>
        <rowstyle backcolor="White" forecolor="#333333" />
        <footerstyle backcolor="White" forecolor="#333333" />
        <pagerstyle backcolor="#336666" forecolor="White" horizontalalign="Center" />
        <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />

  </div>


 </itemtemplate>




 <asp:TemplateField HeaderText="Delete" >
  <itemtemplate>
  <asp:ImageButton ID="ImageDelete" Height="15px" ImageUrl="Images/Delete.png"
   runat="server" CommandName="cmdDelete"
   CommandArgument='<%# Eval("CourseCatID") %>' Width="16px" onclick="ImageDelete_Click"
  /><br />
  </itemtemplate>
  <itemstyle verticalalign="Top" />

  <asp:CommandField ShowDeleteButton="True" />
  <asp:TemplateField>
  <itemtemplate>

  </itemtemplate>


 </columns>



<rowstyle backcolor="#EFFDFF" forecolor="#333333" />
<footerstyle backcolor="#5D7B9D" font-bold="True" forecolor="White" />
<pagerstyle backcolor="#284775" forecolor="White" horizontalalign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<editrowstyle backcolor="#999999" forecolor="White" />
<alternatingrowstyle backcolor="White" forecolor="#333333" verticalalign="Middle" />
As you see it is a nested gridview

Now here is my code on my server side
Protected Sub ImageDeleteCourse_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
       Dim ImageDelete As ImageButton = CType(grdCategory.Rows(0).FindControl("ImageDeleteCourse"), ImageButton)
       Dim row As GridViewRow = DirectCast(ImageDelete.NamingContainer, GridViewRow)
       Dim courseCode As String = Convert.ToString(Me.grdCategory.DataKeys(row.RowIndex).Value)
       Dim coursecode2 As String = courseCode
   End Sub


Ive Updated my codes but still cant get datakeys cause the datakey im getting is on the ParentGrid(Using breakpoint)
I think im getting error here
Dim courseCode As String = Convert.ToString(Me.grdCategory.DataKeys(row.RowIndex).Value)

Any Solution sir?
For i As Integer = 0 To grdCategory.Rows.Count - 1
           Dim gvChild As GridView = DirectCast(grdCategory.Rows(i).Cells(0).FindControl("grdCourse"), GridView)
           Dim ImageDelete As ImageButton = CType(gvChild.Rows(0).FindControl("ImageDeleteCourse"), ImageButton)
           Dim row As GridViewRow = DirectCast(ImageDelete.NamingContainer, GridViewRow)
           Dim courseCode As String = Convert.ToString(Me.grdCategory.DataKeys(row.RowIndex).Value)
       Next
Posted
Updated 5-Dec-11 15:33pm
v2

1 solution

Make sure that from your VB codes, you are not getting a null value from retrieval of data from your Gridview. Put a break point on part
VB
Dim ImageDelete As ImageButton = CType(grdCategory.Rows(0).FindControl("ImageDeleteCourse"), ImageButton)

then start debugging! Hope you get it.

Regards,
Eduard
 
Share this answer
 
Comments
janwel 5-Dec-11 21:05pm    
Sir CType(grdCategory.Rows(0).FindControl("ImageDeleteCourse"), ImageButton) I think is the error since since grdcategory is parent and I am seeking data from child. Sir from my standpoint the gridcourse is just a column so how can I call it?

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