Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hi All,

I am getting the above error while i have linked a datatable to a dropdownlist in asp.net Grid view in Footer for Insert.

I have checked in immediate window and datatable is having 2 records (datatable.rows.count).

following is the code in asp and vb... Please sugggest

Asp.
XML
<asp:TemplateField HeaderText="Type" HeaderStyle-HorizontalAlign="Center">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlGridType" runat="server" DataTextField="BOMIT" DataValueField="BOMIT"> </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblGridType" runat="server" Text='<%# Eval("BOMIT")%>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="ddlNewGridType" runat="server" DataTextField="BOMIT" DataValueField="BOMIT"> </asp:DropDownList>
                 </FooterTemplate>
            </asp:TemplateField>


VB:
VB
Protected Sub DataGridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles DataGridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.Footer Then

            Dim daGridTypes As SqlDataAdapter = New SqlDataAdapter
            daGridTypes.SelectCommand = New SqlCommand("select distinct BOMIT from bom", connection)

            Dim tbGridTypes As DataTable = New DataTable
            connection.Open()
            daGridTypes.Fill(tbGridTypes)
            connection.Close()

            'To SORT THE DATATABLE
            Dim dataGridView As New DataView(tbGridTypes)
            dataGridView.Sort = "BOMIT ASC"
            tbGridTypes = New DataTable
            tbGridTypes = dataGridView.ToTable()

            ddlNewGridType = e.Row.FindControl("ddlNewGridype")

            **********ddlNewGridType.DataSource = tbGridTypes
            ddlNewGridType.DataTextField = "BOMIT"
            ddlNewGridType.DataValueField = "BOMIT"
            ddlNewGridType.DataBind()

        End If

    End Sub
Posted
Comments
[no name] 24-May-14 21:53pm    
You are getting that error for the exact same reason that everyone else gets that error. You are trying to use the methods or properties of an object that has not been instantiated. You need to debug your code and find the object that is null.
atul sharma 5126 24-May-14 22:08pm    
Thanks for replying Wes.
I have already tried debugging and mentioned the results within the question.

The error generates on the linking of datasource to dropdownlist (marked with ***)
And i have checked that at the time of linking, datatable has 2 records i.e. it is not Null.
atul sharma 5126 24-May-14 22:11pm    
Can the error be related to the other fields which i have not called and also not used directly but may me checked as the event is RowBound event?

You have a typo, missing a letter in the control ID.
C#
ddlNewGridType = e.Row.FindControl("ddlNewGridype")

should be
C#
ddlNewGridType = e.Row.FindControl("ddlNewGridType")


It is always a good idea to check that the returned value is not null, so that you catch and log these errors. This prevents changes to the .aspx file from silently breaking the .aspx.cs file.

C#
ddlNewGridType = e.Row.FindControl("ddlNewGridype")
if (ddlNewGridType == null)
    logger.Error("Oops, I can't find 'ddlNewGridType'.);
 
Share this answer
 
Comments
atul sharma 5126 26-May-14 3:33am    
Thanks a lot Matthew. Small things can make such a big difference.
you can't directly access control by id inside the templates like item, header or footer
what you can do is Find the control and set the data source. add below line
VB
If e.Row.RowType = DataControlRowType.Footer Then
    Dim ddlNewGridType As DropDownList = DirectCast(e.Row.FindControl("ddlNewGridType"), DropDownList)
 
Share this answer
 
v2
Comments
atul sharma 5126 25-May-14 1:52am    
Thanks for the reply!
Following error showing: 'DropDownList' is as type and cannot be as an expression
DamithSL 25-May-14 2:02am    
check my update
atul sharma 5126 25-May-14 2:31am    
New error removed. But now stuck on the original error
DamithSL 25-May-14 2:40am    
you know how to debug? check what is null? check ddlNewGridType is null or not and tbGridTypes has records
atul sharma 5126 25-May-14 2:45am    
ddlNewGridType is a new dropdown list
whereas tbGridTypes has 2 records

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