空参考对象异常 [英] Null reference object exception

查看:73
本文介绍了空参考对象异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

If e.Row.RowType = DataControlRowType.Header Then
            Dim ddlVCrop As New DropDownList
            ddlVCrop = CType(e.Row.FindControl("ddlCrop"), DropDownList)
            If Not ddlVCrop Is DBNull.Value Then
                Dim ds As New DataSet
                ds = objYield.getVariety
                If ds.Tables(0).Rows.Count > 0 Then
                 ddlVCrop.DataSource = ds
                    ddlVCrop.DataValueField = "VARIETY_CODE"
                    ddlVCrop.DataTextField = "VARIETY_CODE"
                    ddlVCrop.DataBind()
                    ddlVCrop.Items.Insert(0, "Select One")
                   
End If





在上面的代码中,当我尝试将"ds"绑定到下拉列表"(用粗体显示)时,我面临的是空引用异常.任何人请告诉我解决我的问题的方法.

在此先感谢.
[edit]添加了代码块[/edit]



Hi

In the above code when i am trying to bind the "ds" to "dropdown"(in the sentence in bold), i am facing null reference exception.Any one plz tell me the solution to my problem.

thanks in advance.
[edit]code block added[/edit]

推荐答案

将代码更改为

Change the code to,

If ddlVCrop IsNot Nothing Then


为避免异常,我会这样做:

To avoid the exception, I''d do this:

Dim ds As DataSet = objYield.getVariety()
If ds IsNot Nothing AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
   ... do your processing
End If


我认为您的下车是在gridview标头模板中

尝试以这种方式找出您的下拉菜单


i think your drodown is in gridview header template

try to find out your dropdown in this way


GridView1.HeaderRow.FindControl("DropDownList1")


这篇关于空参考对象异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆