DropDownList problem with SelectedIndex

Discussion in 'ASP .Net' started by Pat, Sep 7, 2005.

  1. Pat

    Pat Guest

    When selecting a DropDownlList to display some Data based on the
    ddl.SelectedItem.Value
    since a dropdownlist index starts from 0..
    After selecting for example value1 and 2 which its ok but when you get back
    to select 0
    it gives an error "Object reference not set to an instance of an object."
    So after selecting this
    ddl.Items.Insert(0, "-- Select Company --")
    i get the error
    Any workarounds
     
    Pat, Sep 7, 2005
    #1
    1. Advertising

  2. Pat

    Onin Tayson Guest

    Hi Pat,

    Can you show some code snippets? Do you check for IsPostBack property
    before population of your DropDownList? You might be rebuilding the DDL
    every PostBack.

    HTH,

    "Pat" <> wrote in message
    news:%...
    > When selecting a DropDownlList to display some Data based on the
    > ddl.SelectedItem.Value
    > since a dropdownlist index starts from 0..
    > After selecting for example value1 and 2 which its ok but when you get
    > back
    > to select 0
    > it gives an error "Object reference not set to an instance of an object."
    > So after selecting this
    > ddl.Items.Insert(0, "-- Select Company --")
    > i get the error
    > Any workarounds
    >
    >
     
    Onin Tayson, Sep 7, 2005
    #2
    1. Advertising

  3. Pat

    Pat Guest

    thx for the reply.
    In the database there is no CompanyID with the value "0" but i'm using the
    SelectedItem.value to populate the Data 'm displaying
    I have i.e
    If Not Page.IsPostBack Then
    GetCompany()
    End If
    And in the Sub:-
    objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    ddlcompany.DataSource = objDR
    ddlcompany.DataValueField = "CompanyID"
    ddlcompany.DataTextField = "CompanyName"
    ddlcompany.DataBind()
    ' i get the error after first selecting the other optrions
    and then select the "-- Select Company --"
    ddlcompany.Items.Insert(0, "-- Select Company --")



    "Onin Tayson" <> wrote in message
    news:...
    > Hi Pat,
    >
    > Can you show some code snippets? Do you check for IsPostBack property
    > before population of your DropDownList? You might be rebuilding the DDL
    > every PostBack.
    >
    > HTH,
    >
    > "Pat" <> wrote in message
    > news:%...
    > > When selecting a DropDownlList to display some Data based on the
    > > ddl.SelectedItem.Value
    > > since a dropdownlist index starts from 0..
    > > After selecting for example value1 and 2 which its ok but when you get
    > > back
    > > to select 0
    > > it gives an error "Object reference not set to an instance of an

    object."
    > > So after selecting this
    > > ddl.Items.Insert(0, "-- Select Company --")
    > > i get the error
    > > Any workarounds
    > >
    > >

    >
    >
     
    Pat, Sep 7, 2005
    #3
  4. Pat

    Pat Guest

    Or i guess i should be able to use SQL to insert the "-- Select Company --"
    Any ideas?

    "Onin Tayson" <> wrote in message
    news:...
    > Hi Pat,
    >
    > Can you show some code snippets? Do you check for IsPostBack property
    > before population of your DropDownList? You might be rebuilding the DDL
    > every PostBack.
    >
    > HTH,
    >
    > "Pat" <> wrote in message
    > news:%...
    > > When selecting a DropDownlList to display some Data based on the
    > > ddl.SelectedItem.Value
    > > since a dropdownlist index starts from 0..
    > > After selecting for example value1 and 2 which its ok but when you get
    > > back
    > > to select 0
    > > it gives an error "Object reference not set to an instance of an

    object."
    > > So after selecting this
    > > ddl.Items.Insert(0, "-- Select Company --")
    > > i get the error
    > > Any workarounds
    > >
    > >

    >
    >
     
    Pat, Sep 7, 2005
    #4
  5. Pat

    Onin Tayson Guest

    Change your code to this...

    ddlcompany.Items.Insert(0, new ListItem("-- Select Company --", "0"))

    The Insert method expects a ListItem object for its item parameter.

    HTH,

    "Pat" <> wrote in message
    news:...
    > thx for the reply.
    > In the database there is no CompanyID with the value "0" but i'm using the
    > SelectedItem.value to populate the Data 'm displaying
    > I have i.e
    > If Not Page.IsPostBack Then
    > GetCompany()
    > End If
    > And in the Sub:-
    > objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    > ddlcompany.DataSource = objDR
    > ddlcompany.DataValueField = "CompanyID"
    > ddlcompany.DataTextField = "CompanyName"
    > ddlcompany.DataBind()
    > ' i get the error after first selecting the other optrions
    > and then select the "-- Select Company --"
    > ddlcompany.Items.Insert(0, "-- Select Company --")
    >
    >
    >
    > "Onin Tayson" <> wrote in message
    > news:...
    >> Hi Pat,
    >>
    >> Can you show some code snippets? Do you check for IsPostBack property
    >> before population of your DropDownList? You might be rebuilding the DDL
    >> every PostBack.
    >>
    >> HTH,
    >>
    >> "Pat" <> wrote in message
    >> news:%...
    >> > When selecting a DropDownlList to display some Data based on the
    >> > ddl.SelectedItem.Value
    >> > since a dropdownlist index starts from 0..
    >> > After selecting for example value1 and 2 which its ok but when you get
    >> > back
    >> > to select 0
    >> > it gives an error "Object reference not set to an instance of an

    > object."
    >> > So after selecting this
    >> > ddl.Items.Insert(0, "-- Select Company --")
    >> > i get the error
    >> > Any workarounds
    >> >
    >> >

    >>
    >>

    >
    >
     
    Onin Tayson, Sep 7, 2005
    #5
  6. Pat

    Pat Guest

    Fixed it thx..


    "Pat" <> wrote in message
    news:%...
    > Or i guess i should be able to use SQL to insert the "-- Select

    Company --"
    > Any ideas?
    >
    > "Onin Tayson" <> wrote in message
    > news:...
    > > Hi Pat,
    > >
    > > Can you show some code snippets? Do you check for IsPostBack property
    > > before population of your DropDownList? You might be rebuilding the DDL
    > > every PostBack.
    > >
    > > HTH,
    > >
    > > "Pat" <> wrote in message
    > > news:%...
    > > > When selecting a DropDownlList to display some Data based on the
    > > > ddl.SelectedItem.Value
    > > > since a dropdownlist index starts from 0..
    > > > After selecting for example value1 and 2 which its ok but when you get
    > > > back
    > > > to select 0
    > > > it gives an error "Object reference not set to an instance of an

    > object."
    > > > So after selecting this
    > > > ddl.Items.Insert(0, "-- Select Company --")
    > > > i get the error
    > > > Any workarounds
    > > >
    > > >

    > >
    > >

    >
    >
     
    Pat, Sep 7, 2005
    #6
  7. Pat

    Onin Tayson Guest

    What method did you use?

    "Pat" <> wrote in message
    news:...
    > Fixed it thx..
    >
    >
    > "Pat" <> wrote in message
    > news:%...
    >> Or i guess i should be able to use SQL to insert the "-- Select

    > Company --"
    >> Any ideas?
    >>
    >> "Onin Tayson" <> wrote in message
    >> news:...
    >> > Hi Pat,
    >> >
    >> > Can you show some code snippets? Do you check for IsPostBack property
    >> > before population of your DropDownList? You might be rebuilding the
    >> > DDL
    >> > every PostBack.
    >> >
    >> > HTH,
    >> >
    >> > "Pat" <> wrote in message
    >> > news:%...
    >> > > When selecting a DropDownlList to display some Data based on the
    >> > > ddl.SelectedItem.Value
    >> > > since a dropdownlist index starts from 0..
    >> > > After selecting for example value1 and 2 which its ok but when you
    >> > > get
    >> > > back
    >> > > to select 0
    >> > > it gives an error "Object reference not set to an instance of an

    >> object."
    >> > > So after selecting this
    >> > > ddl.Items.Insert(0, "-- Select Company --")
    >> > > i get the error
    >> > > Any workarounds
    >> > >
    >> > >
    >> >
    >> >

    >>
    >>

    >
    >
     
    Onin Tayson, Sep 7, 2005
    #7
    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. Matthew
    Replies:
    1
    Views:
    1,478
    Matthew
    Nov 12, 2003
  2. David Austin via .NET 247
    Replies:
    2
    Views:
    561
    =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?=
    Jun 7, 2004
  3. keithb
    Replies:
    1
    Views:
    12,003
    shauns1
    Nov 1, 2006
  4. S_K
    Replies:
    5
    Views:
    771
    Eliyahu Goldin
    Nov 14, 2007
  5. Rishad Quazi

    DropDownList SelectedIndex problem in C# (from working VB.NET)

    Rishad Quazi, Apr 29, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    570
    Scott Mitchell [MVP]
    May 1, 2004
Loading...

Share This Page