存在记录时,对象引用未设置为对象的实例 [英] Object reference not set to an instance of an object when records exist
问题描述
大家好,
我知道上面的错误显示没有找到记录。但在下面的例子中,他们在数据表中有15条记录。
我试图在gridview中的每一行的列中显示一个下拉列表。以下是vb中的代码:
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:
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表在调试中确认了15条记录。
以下是html中的列:
dtPersons table has 15 records as confirmed in debugging.
Following is column in html:
<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>
请建议。
谢谢
Atul
Please suggest.
Thanks
Atul
推荐答案
它没有说数据表是什么都没有
您的ddlJuniors可能没什么,访问ddlJuniors.DataSource失败。
It doesn't say that datatable is nothing
Your ddlJuniors is probably nothing and accessing ddlJuniors.DataSource fails.
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
不是这个代码工作如下:
- 在名为ddlJuniors的网格控件中查找
- 将找到的控件分配给本地名称ddlTemp< - 这可以是任何你可以称之为dasfalksadfj的东西并且它仍然可以工作
- 使用< u>完全< / u>通过函数引用
你不能分配ddlTmp然后尝试在函数中使用ddlJuniors。
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.
这篇关于存在记录时,对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!