468,766 Members | 1,292 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,766 developers. It's quick & easy.

DropDownList problem with SelectedIndex

Pat
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
Nov 19 '05 #1
6 5660
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" <na********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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

Nov 19 '05 #2
Pat
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.Clos eConnection)
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" <Le*********@compaid.com> wrote in message
news:uv**************@TK2MSFTNGP15.phx.gbl...
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" <na********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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


Nov 19 '05 #3
Pat
Or i guess i should be able to use SQL to insert the "-- Select Company --"
Any ideas?

"Onin Tayson" <Le*********@compaid.com> wrote in message
news:uv**************@TK2MSFTNGP15.phx.gbl...
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" <na********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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


Nov 19 '05 #4
Pat
Fixed it thx..
"Pat" <na********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Or i guess i should be able to use SQL to insert the "-- Select Company --" Any ideas?

"Onin Tayson" <Le*********@compaid.com> wrote in message
news:uv**************@TK2MSFTNGP15.phx.gbl...
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" <na********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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



Nov 19 '05 #5
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" <na********@hotmail.com> wrote in message
news:ef*************@TK2MSFTNGP11.phx.gbl...
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.Clos eConnection)
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" <Le*********@compaid.com> wrote in message
news:uv**************@TK2MSFTNGP15.phx.gbl...
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" <na********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
> 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
>
>



Nov 19 '05 #6
What method did you use?

"Pat" <na********@hotmail.com> wrote in message
news:uj*************@TK2MSFTNGP15.phx.gbl...
Fixed it thx..
"Pat" <na********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Or i guess i should be able to use SQL to insert the "-- Select

Company --"
Any ideas?

"Onin Tayson" <Le*********@compaid.com> wrote in message
news:uv**************@TK2MSFTNGP15.phx.gbl...
> 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" <na********@hotmail.com> wrote in message
> news:%2****************@TK2MSFTNGP15.phx.gbl...
> > 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
> >
> >
>
>



Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Tim::.. | last post: by
4 posts views Thread by Mark Waser | last post: by
12 posts views Thread by Frederik Vanderhaeghe | last post: by
11 posts views Thread by Santosh | last post: by
1 post views Thread by pleaseexplaintome | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.