本文介绍了在运行时填充下拉列表时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试在GridView中填充下拉列表时,出现以下错误:
对象引用未设置为对象的实例.
该代码受
I am getting the following error when i am trying to fill a Dropdown inside the GridView:
Object reference not set to an instance of an object.
the code is
protected void ProjectOnFormGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
string selectcommand;
DataSet dsrow;
if (e.Row.RowType == DataControlRowType.DataRow)
{
selectcommand = "select QuantityDescId,QuantityDescName from QuantityDescriptions";
DropDownList EditddQuantDesc = (DropDownList)e.Row.FindControl("EditddQuantDesc");
if (EditddQuantDesc == null)
{
da = new SqlDataAdapter(selectcommand, con);
dsrow = new DataSet();
da.Fill(dsrow, "QuantityDescriptions");
EditddQuantDesc.DataSource = dsrow.Tables["QuantityDescriptions"];
EditddQuantDesc.DataValueField = dsrow.Tables[0].Columns[0].ToString();
EditddQuantDesc.DataTextField = dsrow.Tables[0].Columns[1].ToString();
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
selectcommand = "select QuantityDescId,QuantityDescName from QuantityDescriptions";
DropDownList FooterddQuantDesc = (DropDownList)e.Row.FindControl("FooterddQuantDesc");
if (FooterddQuantDesc != null)
{
da = new SqlDataAdapter(selectcommand, con);
dsrow = new DataSet();
da.Fill(dsrow, "QuantityDescriptions");
FooterddQuantDesc.DataSource = dsrow;
FooterddQuantDesc.DataValueField = dsrow.Tables[0].Columns[0].ToString();
FooterddQuantDesc.DataTextField = dsrow.Tables[0].Columns[1].ToString();
}
}
}
catch (Exception ce)
{
lblError.Text = ce.Message;
lblError.Visible = true;
}
}
我在此行遇到错误:
EditddQuantDesc.DataSource = dsrow.Tables["QuantityDescriptions"];
推荐答案
if (EditddQuantDesc == null)
{
da = new SqlDataAdapter(selectcommand, con);
dsrow = new DataSet();
da.Fill(dsrow, "QuantityDescriptions");
EditddQuantDesc.DataSource = dsrow.Tables["QuantityDescriptions"];
EditddQuantDesc.DataValueField = dsrow.Tables[0].Columns[0].ToString();
EditddQuantDesc.DataTextField = dsrow.Tables[0].Columns[1].ToString();
}
您检查EditddQuantDesc是否为null并以任何方式使用它,并且因为您知道该错误,所以未将其设置为对象的引用.
祝你好运!
You check if EditddQuantDesc null and work with it anyway and as the error let''s you know, it isn''t set to a reference of an object.
Good luck!
这篇关于在运行时填充下拉列表时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!