asp:DropDownList problem

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.
 
B

Bill Priess

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

HTH,
Bill P.
 
M

Maziar Aflatoun

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top