Click here to Skip to main content
15,884,074 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All,

I know that the above error shows when there are no records found. But in the following case their are 15 records in datatable.

I am trying to show a dropdown list in a column for each row in a gridview. the following is the code in vb:

VB
If dtPersons.Rows.Count > 0 Then
           Dim ddlJuniors As DropDownList = DirectCast(e.Row.FindControl("ddlJuniors"), DropDownList)
           **ddlJuniors.DataSource = dtPersons
           ddlJuniors.DataTextField = "USERN"
           ddlJuniors.DataValueField = "USERID"
           ddlJuniors.DataBind()
       End If


dtPersons table has 15 records as confirmed in debugging.

Following is column in html:
XML
<asp:TemplateField HeaderText="Delegate" HeaderStyle-HorizontalAlign="Left" ControlStyle-Width="100">
                  <ItemTemplate>
                        <asp:DropDownList ID="ddlJuniors" runat="server" DataTextField="USERN" DataValueField="USERID" CssClass="ddl"> </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>


Please suggest.
Thanks
Atul
Posted

1 solution

It doesn't say that datatable is nothing
Your ddlJuniors is probably nothing and accessing ddlJuniors.DataSource fails.



VB
If dtPersons.Rows.Count > 0 Then
If (e.Row.RowType = DataControlRowType.DataRow) Then
           Dim ddlTemp As DropDownList = DirectCast(e.Row.FindControl("ddlJuniors"), DropDownList)
           ddlTemp.DataTextField = "USERN"
           ddlTemp.DataValueField = "USERID"
           ddlTemp.DataSource = dtPersons

           ddlTemp.DataBind()
       End If
End If


Not this code works as follows:
- find in the grid control named ddlJuniors
- assign the found control to local name ddlTemp <-- this can be anything you could call it dasfalksadfj and it would still work
- work with <u>that exact</u> reference through the function

You cannot assign ddlTmp and then try to use ddlJuniors later in the function.
 
Share this answer
 
v5
Comments
atul sharma 5126 15-Oct-14 7:19am    
You are probably right. After making your suggested changes the error point has changed to:
Dim ddlJuniors As DropDownList = DirectCast(e.Row.FindControl(ddlJuniors.ClientID), DropDownList)

Also highlighed is the ddlJuniors within find control: variable ddljuniors is used before being assigned a value.
Sinisa Hajnal 15-Oct-14 7:59am    
You have to rename your local variable so it doesn't clash with found control :)
atul sharma 5126 16-Oct-14 0:53am    
the second error resolved. thanks.

But now stuck with the original error of object reference at:
ddlJuniors.DataSource = dtPersons
Sinisa Hajnal 16-Oct-14 6:54am    
I think you don't understand the code. You're finding the control ddlJuniors from the web page by findControl("id")

It returns DropDownList object and puts it into the variable you name however you want. It can be just cmb...see solution. You have to change all references in your handler, not just first one.
atul sharma 5126 17-Oct-14 5:59am    
I accept your comments with respect. Yes I am new to coding.
But I have used similar code earlier it worked nicely. I have made following changes as per understanding your solution. Pls check and guide. Thanks..

If dtPersons.Rows.Count > 0 Then

dim ddl as dropdownlist = DirectCast(e.Row.FindControl("ddlJuniors"), DropDownList)
**ddl.DataSource = dtPersons
ddl.DataTextField = "USERN"
ddl.DataValueField = "USERID"
ddl.DataBind()
End If

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