Drop down list and Object reference not set to an instance of an object

Drop down list and Object reference not set to an instance of an object

Post by Alex Shirl » Tue, 03 Sep 2002 23:19:53



Hi

When I need to set the default selected value of a drop down box that
has been dynamically loaded with values. Other drop down boxes that
have had values statically defined using html code seen to work fine
when I select a value.

The error message is below, followed by the code and I've clearly
labeled the problem area in the code.

I tested drDataReader.GetValue(1).ToString()) and this comes up with a
relevant value (and it's not null!). Also note that ddlMedia was
declaired as System.Web.UI.WebControls.DropDownList

Any ideas?

Cheers

Alex

---------------

Object reference not set to an instance of an object.
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:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an
object.]

----------

   'Update drop down listboxes
      Dim dreader As SqlClient.SqlDataReader
      SqlConnection1.Open()
      dreader = cmdMediaByID.ExecuteReader()

      While dreader.Read()
        ddlMedia.Items.Add(New ListItem(dreader(1).ToString(),
dreader(0).ToString()))
      End While

      dreader.Close()
      SqlConnection1.Close()
    End If

  End Sub

' SNIP

      ddlExpired.SelectedItem.Selected = False

        'Doesn't like this line
ddlExpired.Items.FindByValue(drDataReader.GetValue(1).ToString()).Selected
= True

-------------------------------------------------------------------

Note the HTML code for the drop down box that is generated is:
<select name="ddlMedia" id="ddlMedia">
        <option value="1">Paper</option>
        <option value="2">e-Mail</option>
        <option value="3">Website</option>
        <option value="4">Conference</option>

</select>

I've tested drDataReader.GetValue(1).ToString() and returned a value
of 4 (which should work right?!)

Thanks very much !!!

Alex

 
 
 

Drop down list and Object reference not set to an instance of an object

Post by Victor Garcia Apre » Wed, 04 Sep 2002 12:17:33


Hi Alex,

Quote:>>>> 'Doesn't like this line
>>>ddlExpired.Items.FindByValue(drDataReader.GetValue(1).ToString()).Selecte
d
>>>= True

If you've already checked that the DataReader is ok, then ddlExpired or its
items are the suspects. At which method are you adding the dynamically
generated ddl? Are you always adding it? or just in some specific cases
(IsPostBack,etc) ?

-Victor.

--
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
and not by private mail.


Quote:> Hi

> When I need to set the default selected value of a drop down box that
> has been dynamically loaded with values. Other drop down boxes that
> have had values statically defined using html code seen to work fine
> when I select a value.

> The error message is below, followed by the code and I've clearly
> labeled the problem area in the code.

> I tested drDataReader.GetValue(1).ToString()) and this comes up with a
> relevant value (and it's not null!). Also note that ddlMedia was
> declaired as System.Web.UI.WebControls.DropDownList

> Any ideas?

> Cheers

> Alex

> ---------------

> Object reference not set to an instance of an object.
> 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:
> > An unhandled exception was generated during the execution of the
> current web request. Information regarding the origin and location of
> the exception can be identified using the exception stack trace below.

> Stack Trace:

> [NullReferenceException: Object reference not set to an instance of an
> object.]

> ----------

>    'Update drop down listboxes
>       Dim dreader As SqlClient.SqlDataReader
>       SqlConnection1.Open()
>       dreader = cmdMediaByID.ExecuteReader()

>       While dreader.Read()
>         ddlMedia.Items.Add(New ListItem(dreader(1).ToString(),
> dreader(0).ToString()))
>       End While

>       dreader.Close()
>       SqlConnection1.Close()
>     End If

>   End Sub

> ' SNIP

>       ddlExpired.SelectedItem.Selected = False

> 'Doesn't like this line
> ddlExpired.Items.FindByValue(drDataReader.GetValue(1).ToString()).Selected
> = True

> -------------------------------------------------------------------

> Note the HTML code for the drop down box that is generated is:
> <select name="ddlMedia" id="ddlMedia">
> <option value="1">Paper</option>
> <option value="2">e-Mail</option>
> <option value="3">Website</option>
> <option value="4">Conference</option>

> </select>

> I've tested drDataReader.GetValue(1).ToString() and returned a value
> of 4 (which should work right?!)

> Thanks very much !!!

> Alex


 
 
 

Drop down list and Object reference not set to an instance of an object

Post by Alex Shirl » Wed, 04 Sep 2002 19:26:22


Quote:>At which method are you adding the dynamically
> generated ddl? Are you always adding it? or just in some specific cases
> (IsPostBack,etc) ?

Thanks for getting back to me Victor, much appreciated:

Does this reveal anything (below)?

Many thanks!

Alex

--------------------------------------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

    If Not Page.IsPostBack Then

'SNIP

While dreader.Read()
        ddlMedia.Items.Add(New ListItem(dreader(1).ToString(),
dreader(0).1ToString()))
      End While

'SNIP

End If

--------------------

 
 
 

1. System.Exception: Mid-Object reference not set to an instance of an object.

I have created two serviced components, say component A and B.  Both
component can be hosted in IIS and be activated by client thru remoting
without any error.  However, if I activate objects in the way:  Client ->
serviced component A -> serviced component B, I got the following exception:

System.Exception: Mid-Object reference not set to an instance of an object.

Note: I'm using RC3, component A is transaction new whereas component B is
transaction supported.

Thanks in advance.

2. internet freeze

3. Object reference not set to an instance of an object

4. upgrading win98 to 2000

5. Object Reference Not Set to an Instance of an Object error when calling unmanaged DLL function

6. speach in spectrum

7. Object Reference Not Set to an Instance of an Object

8. New Files Loaded in FP 26

9. SoapSuds Bug: Object reference not set to an instance of an object

10. Bug with BringToFront - System.NullReferenceException: Object reference not set to an instance of an object.

11. object reference not set to instance of an object

12. Object reference not set to an instance of an object

13. object reference not set to an instance of an object