下拉列表显示此代码的错误 [英] Dropdown list showing error with this code

查看:88
本文介绍了下拉列表显示此代码的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,全部



这里我尝试在下拉列表中显示两列,当我从DB中检索显示错误对象参考错误的数据时!



我的代码是:



Hi, All

Here i try to show two columns in dropdownlist, when i retrieve the data from DB showing Error Object Reference error !

My Code IS:

DataTable Dtab_1 = new DataTable();
            Dtab_1 = DbCon.GetData("select ReasonCodeC +' /  '+ReasonNameC as ReasonD from reason");
            ddlresoncode.DataSource = Dtab_1;
            ddlresoncode.DataTextField = "ReasonD";
            ddlresoncode.DataBind();
            DataTable Dtab_2 = new DataTable();
            Dtab_2 = DbCon.GetData("select r.ReasonCodeC+' / '+r.ReasonNameC as reasonD from reason as r inner join holiday as h on r.ReasonCodeC=h.ReasonCodeC where h.HolidayNameC='" + strCmd + "'");
            ddlresoncode.Items.FindByText(Dtab_2.Rows[0]["reasonD"].ToString()).Selected = true; // Here i Got Error!

推荐答案

错误可能是由于FindByText返回NULL而你在使用它之前没有检查它。

在使用之前一定要检查对象的存在和有效性。



对象sel = ddlresoncode.Items.FindByText(Dtab_2.Rows [0] [reasonD]。ToString());

if(sel! = null)

sel.Selected = True

else //做其他事情

return;
The error is probably caused by FindByText returning NULL and you not checking it before using it.
Always check for object existence and validity before using it.

Object sel = ddlresoncode.Items.FindByText(Dtab_2.Rows[0]["reasonD"].ToString());
if ( sel != null)
sel.Selected = True
else // do something else
return;


这篇关于下拉列表显示此代码的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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