Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DropDownList problem with SelectedIndex

Reply
Thread Tools

DropDownList problem with SelectedIndex

 
 
Pat
Guest
Posts: n/a
 
      09-07-2005
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


 
Reply With Quote
 
 
 
 
Onin Tayson
Guest
Posts: n/a
 
      09-07-2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Pat
Guest
Posts: n/a
 
      09-07-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Pat
Guest
Posts: n/a
 
      09-07-2005
Or i guess i should be able to use SQL to insert the "-- Select Company --"
Any ideas?

"Onin Tayson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Onin Tayson
Guest
Posts: n/a
 
      09-07-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>> > 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
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
Pat
Guest
Posts: n/a
 
      09-07-2005
Fixed it thx..


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

Company --"
> Any ideas?
>
> "Onin Tayson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:%(E-Mail Removed)...
> > > 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
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Onin Tayson
Guest
Posts: n/a
 
      09-07-2005
What method did you use?

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

> Company --"
>> Any ideas?
>>
>> "Onin Tayson" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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" <(E-Mail Removed)> wrote in message
>> > news:%(E-Mail Removed)...
>> > > 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
>> > >
>> > >
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When I set the DropDownList.SelectedIndex of ONE DropDownList they ALL change! S_K ASP .Net 5 11-14-2007 04:51 PM
DropDownList inside GridView: How to set SelectedIndex for the DropDownList? keithb ASP .Net 1 11-01-2006 05:24 AM
Problem with setting DropDownList selectedindex on multiple controls David Austin via .NET 247 ASP .Net 2 06-07-2004 02:36 AM
DropDownList SelectedIndex problem in C# (from working VB.NET) Rishad Quazi ASP .Net Datagrid Control 2 04-30-2004 11:40 PM
DropDownList SelectedIndex stuck at -1 after IsPostBack Matthew ASP .Net 1 11-12-2003 08:12 PM



Advertisments