asp:DropDownList problem

  • Thread starter Thread starter Maziar Aflatoun
  • Start date Start date
M

Maziar Aflatoun

Hi everyone,

I am trying to select an item in my DropDownList (Everything on 1 .aspx
page) and it gives me the following error message. Has anyone encountered
this problem before? or any idea how I can resolve it? (In this case I want
to select the Canada from the country list)

SqlDataAdapter daCountry = new SqlDataAdapter("SELECT * FROM Country ORDER
BY Value", conn);
DataSet dsCountry = new DataSet();
daCountry.Fill(dsCountry, "Country");
Country.DataSource = dsCountry.Tables["Country"];
Country.DataTextField = "Value";
Country.DataValueField = "Key";
//Country.ClearSelection();
Country.Items.FindByText("Canada").Selected = true;
Country.DataBind();


Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 27: Country.DataValueField = "Key";
Line 28: //Country.ClearSelection();
Line 29: Country.Items.FindByText("Canada").Selected = true;
Line 30:
Line 31: Country.DataBind();




Thank you
Maziar A.
 
Try this:
Country.SelectedIndex =
Country.Items.IndexOf(Country.Items.FindByText("Canada"));

HTH,
Bill P.
 
Thanks worked :)

Maz.

Bill Priess said:
Try this:
Country.SelectedIndex =
Country.Items.IndexOf(Country.Items.FindByText("Canada"));

HTH,
Bill P.

Maziar Aflatoun said:
Hi everyone,

I am trying to select an item in my DropDownList (Everything on 1 .aspx
page) and it gives me the following error message. Has anyone encountered
this problem before? or any idea how I can resolve it? (In this case I want
to select the Canada from the country list)

SqlDataAdapter daCountry = new SqlDataAdapter("SELECT * FROM Country ORDER
BY Value", conn);
DataSet dsCountry = new DataSet();
daCountry.Fill(dsCountry, "Country");
Country.DataSource = dsCountry.Tables["Country"];
Country.DataTextField = "Value";
Country.DataValueField = "Key";
//Country.ClearSelection();
Country.Items.FindByText("Canada").Selected = true;
Country.DataBind();


Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 27: Country.DataValueField = "Key";
Line 28: //Country.ClearSelection();
Line 29: Country.Items.FindByText("Canada").Selected = true;
Line 30:
Line 31: Country.DataBind();




Thank you
Maziar A.
 
Back
Top