472,976 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,976 software developers and data experts.

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 5791
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jm | last post by:
I have datagrid. On this datagrid there is a button and a dropdownlist. When I press the button, I want the selectedIndex of the dropdownlist. There is a button and a dropdownlist for each...
3
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would...
10
by: dhnriverside | last post by:
Hi guys Still having a problem with this dropdownlist. Basically, I've got 4. The first 2 work fine, then my code crashes on the 3rd. ddlEndTimeHour.Items.FindByValue(endTime).Selected =...
4
by: Mark Waser | last post by:
I've discovered a very odd bug when attempting to put a dropdown list in a datagrid. In the page PreRender step, the selected index of the datagrid is successfully set during databinding. Yet,...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a dropdownlist that contains should contain several values from the database, I fill it up in de page_load() with this code: If Not Page.IsPostBack Then InlezenConfig() Dim...
11
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
1
by: Jason Wilson | last post by:
I have two dropdownlists that are bound to the same datasource and I have a couple of questions: 1) Because they are bound to the same datasource, I am assuming that they only make 1 round trip...
1
by: pleaseexplaintome | last post by:
Hi all, I have a datagrid with a dropdownlist and would like to have the dropdownlist display a database value correctly while the grid is in edit mode. I have a templatecolumn as follows: ...
1
by: marcbb | last post by:
Hi all, I have a really strange problem working with Dropdownlists in a DataGrid. I'm trying to preselect some values from the DropDownlist for each row in the DataGrid, but it seems that...
3
by: S_K | last post by:
Hi, I have a list of 6 DropDownList boxes, from DropDownList1 to DropDownList6, and I'm changing the SelectedIndex of each using a foreach loop as follows: foreach(PaymentReqDisplay...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.