本文介绍了如何摆脱对象引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在表格中有值,对象引用显示错误ddlAge.DataSource = dt;



我尝试了什么:



Hi I have values in the table and the object reference is showing error ddlAge.DataSource = dt;

What I have tried:

<pre>protected void BindAge()
        {


                    SqlCommand cmd = new SqlCommand("select * from Age ", cn);
                    cmd.CommandType = CommandType.Text;
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    DataTable dt = new DataTable();
                    da.Fill(dt);

                    DataSet ds = new DataSet();


                    if (dt.Rows.Count > 0)
                    {
                        DropDownList ddlAge =
                        (DropDownList)FindControl("ddlAddAge");
                        ddlAge.DataSource = dt;
                        ddlAge.DataTextField = "Age";
                        ddlAge.DataValueField = "Ageid";
                        ddlAge.DataBind();
                        ddlAge.Items.Insert(0, new ListItem("--Select--", "0"));
                        cn.Close();
                        //ddlAge.Items.FindByValue(ViewState["Filter"].ToString()).Selected = true;
        }
        }

推荐答案




这篇关于如何摆脱对象引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 10:11