Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am loading data from database. This error is shown for line

DropDownList1.SelectedItem.Text = dtt.Rows[0]["Country"].ToString();

C#
protected void Page_Load(object sender, EventArgs e)
       {

               loadprofiledata1();

       }
       protected void loadprofiledata1()
       {
           SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");

           con.Open();
           SqlCommand cmd3 = new SqlCommand("select * from Users where UserID=" + Convert.ToInt16(Session["UserID"].ToString()), con);

           SqlDataAdapter daa = new SqlDataAdapter(cmd3);
           DataTable dtt = new DataTable();
           daa.Fill(dtt);
           if (dtt.Rows.Count > 0)
           {
               firstname.Text = dtt.Rows[0]["FirstName"].ToString();
               lastname.Text = dtt.Rows[0]["LastName"].ToString();
               username.Text = dtt.Rows[0]["UserName"].ToString();
               gender.SelectedItem.Text = dtt.Rows[0]["Gender"].ToString();
               email.Text = dtt.Rows[0]["EmailAddress"].ToString();
               dob.Text = dtt.Rows[0]["DOB"].ToString();
               mobile.Text = dtt.Rows[0]["MobileNo"].ToString();
               nationality.Text = dtt.Rows[0]["Nationality"].ToString();
               address.Text = dtt.Rows[0]["Address"].ToString();
               DropDownList1.SelectedItem.Text = dtt.Rows[0]["Country"].ToString();
               DropDownList2.SelectedItem.Text = dtt.Rows[0]["State"].ToString();
               DropDownList3.SelectedItem.Text = dtt.Rows[0]["City"].ToString();
               collegename.Text = dtt.Rows[0]["College"].ToString();
               degree.SelectedItem.Text = dtt.Rows[0]["Degree"].ToString();
               seconddegree.SelectedItem.Text = dtt.Rows[0]["SecondDegree"].ToString();
               branch1.SelectedItem.Text = dtt.Rows[0]["Branch"].ToString();
               startyear.SelectedItem.Text = dtt.Rows[0]["StartYear"].ToString();
               endyear.SelectedItem.Text = dtt.Rows[0]["ExpectedPassOutYear"].ToString();








           }
       }
Posted
Comments
Thanks7872 5-May-14 2:26am    
See this: Object reference not set to an instance of an object
Its a well explained answer to this error.
Sivaraman Dhamodharan 5-May-14 2:43am    
Check the spelling for Country in the database. I mean check that the column name is right.
dtt: is valid
Rows[0] also valid.
Only suspect is misspelled column name

well check if dropdown1 has any selecteditem or not,


C#
if(dropdown1.selecteditem!=null)
{
dropdown1.selecteditem.text = dtt.Rows[0]["Country"].ToString();
}

but i would suggest you to write

C#
dropdown1.selectedvalue = dtt.Rows[0]["Country"].ToString(); 

instead of selecteditem.text
 
Share this answer
 
Comments
jayraj86 5-May-14 3:39am    
Same thing i am loading in other page using same code and it works perfectly on other page
this is due to null value in

dtt.Rows[0]["Country"].ToString();

check dtt ,by compile step by step ,hover the mouse in dtt and take full view of that table and check the value ["Country"]

OR check the name ["Country"] is same as in DB
 
Share this answer
 
Comments
jayraj86 5-May-14 3:39am    
Same thing i am loading in other page using same code and it works perfectly on other page

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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