你调用的对象是空的。数据未在下拉列表中加载 [英] Object reference not set to an instance of an object. Data not loading in dropdown

查看:54
本文介绍了你调用的对象是空的。数据未在下拉列表中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表格。我试图在页面加载时从数据库加载文本框和下拉列表中的数据但是没有加载。





它显示错误line-DropDownList4.SelectedItem.Text = dtt.Rows [0] [Branch]。ToString();



I have one form. I am trying to load the data on textboxes and dropdownlist from database on page load but its not loading.


it shows error on this line- DropDownList4.SelectedItem.Text = dtt.Rows[0]["Branch"].ToString();

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
                if ( Request.QueryString.Count > 0)
                {
                    Loaddata();
                }
            }
        
        protected void Loaddata()
        {
            SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");

            
            SqlCommand cmd3 = new SqlCommand("select * from Internship where InternshipID=" + Convert.ToInt16(Request.QueryString["InternshipID"].ToString()), con);

            SqlDataAdapter daa = new SqlDataAdapter(cmd3);
            DataTable dtt = new DataTable();
            daa.Fill(dtt);
            if (dtt.Rows.Count > 0)
            {
                title.Text = dtt.Rows[0]["Title"].ToString();
                role.Text = dtt.Rows[0]["InternshipRole"].ToString();
                noofopenings.Text = dtt.Rows[0]["NoOfOpenings"].ToString();
                startdate.Text = dtt.Rows[0]["InternshipStartDate"].ToString();
                enddate.Text = dtt.Rows[0]["ApplicationDeadLine"].ToString();
                duration.Text = dtt.Rows[0]["Duration"].ToString();
                type.SelectedItem.Text = dtt.Rows[0]["Type"].ToString();
                DropDownList2.SelectedItem.Text = dtt.Rows[0]["InternshipCity"].ToString();
                DropDownList3.SelectedItem.Text = dtt.Rows[0]["Degree"].ToString();
               
                //Response.Write(dtt.Rows[0]["Degree"].ToString());
                DropDownList4.SelectedItem.Text = dtt.Rows[0]["Branch"].ToString();
                Response.Write(dtt.Rows[0]["Branch"].ToString());
                

                int_address.Text = dtt.Rows[0]["Address"].ToString();
                int_contactno.Text = dtt.Rows[0]["ContactNo"].ToString();
                
                RadioButtonList1.Items.FindByText(dtt.Rows[0]["Stipend"].ToString()).Selected = true;
                RadioButtonList2.Items.FindByText(dtt.Rows[0]["FullTimeJob"].ToString()).Selected = true;
                
                skills_req.Text = dtt.Rows[0]["SkillsRequired"].ToString();





            }
        }

推荐答案

检查是否有行数且分支不为空。

例如

Check if there is a row count and branch is not null.
for e.g.
if (dtt.Rows.Count > 0)
{
      DropDownList4.SelectedItem.Text = Convert.ToString(dtt.Rows[0]["Branch"]);
}


这篇关于你调用的对象是空的。数据未在下拉列表中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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