Click here to Skip to main content
15,913,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();

C#
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();





            }
        }
Posted
Comments
DamithSL 11-May-14 14:19pm    
put your column names in select statement like 'select Title, InternshipRole ... from ...` then run the program, you need to give the same column names when you get data from the datatable.

1 solution

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"]);
}
 
Share this answer
 
Comments
jayraj86 11-May-14 14:08pm    
no its not null. i tried DropDownList4.SelectedItem.Text = Convert.ToString(dtt.Rows[0]["Branch"]); but it shows same error on same line
[no name] 11-May-14 14:11pm    
Well which part of that line is null? That is your answer.
jayraj86 11-May-14 14:12pm    
what ?
[no name] 11-May-14 14:14pm    
Which part did you not understand? Find out which object on that line is null and you will have your answer.
jayraj86 11-May-14 14:16pm    
its not null.. i checked via response.write data is loading in dtt..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900