asp:DropDownList problem

Discussion in 'ASP .Net' started by Maziar Aflatoun, Nov 14, 2003.

  1. 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.
     
    Maziar Aflatoun, Nov 14, 2003
    #1
    1. Advertising

  2. Maziar Aflatoun

    Bill Priess Guest

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

    HTH,
    Bill P.

    "Maziar Aflatoun" <> wrote in message
    news:wg7tb.67315$...
    > 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.
    >
    >
     
    Bill Priess, Nov 14, 2003
    #2
    1. Advertising

  3. Thanks worked :)

    Maz.

    "Bill Priess" <> wrote in message
    news:...
    > Try this:
    > Country.SelectedIndex =
    > Country.Items.IndexOf(Country.Items.FindByText("Canada"));
    >
    > HTH,
    > Bill P.
    >
    > "Maziar Aflatoun" <> wrote in message
    > news:wg7tb.67315$...
    > > 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.
    > >
    > >

    >
    >
     
    Maziar Aflatoun, Nov 14, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mr2_93
    Replies:
    1
    Views:
    3,645
    mr2_93
    Oct 2, 2005
  2. tshad
    Replies:
    8
    Views:
    2,253
    tshad
    Oct 19, 2005
  3. =?Utf-8?B?SnVhbmpv?=

    GridView: Filtr DropDownList from another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,888
    =?Utf-8?B?SnVhbmpv?=
    Dec 23, 2005
  4. =?Utf-8?B?SnVhbmpv?=

    GridView: Filter DropDownList for another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,323
    =?Utf-8?B?SnVhbmpv?=
    Dec 29, 2005
  5. keithb
    Replies:
    1
    Views:
    12,131
    shauns1
    Nov 1, 2006
Loading...

Share This Page