Re: Problem with Object Reference?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Marina (someone_at_nospam.com)
Date: 07/27/04


Date: Tue, 27 Jul 2004 12:40:13 -0400

Is add_Office actually declared as a server side dropdown on your .aspx
page?

Basically, this kind of error happens when you declare the variable, but
don't instantiate it to an instance of an object. If the dropdown list with
the same id was on the .aspx page, asp.net would automatically match those
up and create the instance.

"Tim::.." <myatix_at_hotmail.com> wrote in message
news:2E11E00B-6C57-4618-9E9B-CE2F29111FC1@microsoft.com...
> Hi,
> I am trying to dynamically populate a dropdown list in a datagrid from a
database. However I keep getting the following error... WHY??? It seems to
be ok in the code but maybe I have to access it differently as it is in a
datagrid???
>
> dropdownlist id="add_office"
>
> Thanks for any help!
>
> ..::Error
> Object reference not set to an instance of an object.
>
> Source Error:
>
> Line 226: ' Get a new datareader from our command
> Line 227: Dim myReader As SqlDataReader = cmd.ExecuteReader()
> Line 228: add_Office.DataSource = myReader 'Error
> Line 229: add_Office.DataValueField = "officeID"
> Line 230: add_Office.DataTextField = "offName"
>
> ..:: CODE
>
> Sub PopulateDropdown()
> ' Only pull data from db on first page call.
> If Not Page.IsPostBack Then
>
>
> ' Create connection
> Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("strConn"))
> Dim cmd As New SqlCommand("SelectOffice", Myconn)
> cmd.CommandType = CommandType.StoredProcedure
>
> Myconn.Open()
>
> ' Get a new datareader from our command
> Dim myReader As SqlDataReader = cmd.ExecuteReader()
> add_Office.DataSource = myReader
> add_Office.DataValueField = "officeID"
> add_Office.DataTextField = "offName"
> add_Office.DataBind()
>
> myReader.Close()
>
> Myconn.Close()
> End If
> End Sub



Relevant Pages

  • Asp:DropDown - get selected item
    ... I have an .aspx with some controls that are created dynamically. ... items are populated into the box by setting the DataSource-property to ... selected item is always the first item in the DropDown. ...
    (microsoft.public.dotnet.languages.csharp)
  • Asp:DropDown - get selected item
    ... I have an .aspx with some controls that are created dynamically. ... items are populated into the box by setting the DataSource-property to ... selected item is always the first item in the DropDown. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Viewer Control Object is in a read only state
    ... I'm displaying an RDLC file in a Reporting Viewer Control in an aspx page. ... at System.EventHandler.Invoke(Object sender, EventArgs e) ... I do have the dropdown set to autopostback=true and the ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Dropdown in datagrid
    ... the cmd object is null or the add_Office dropdownlist is null....you sure ... the id is "add_Office" in the .aspx source code? ... > when I try to populate the following dropdown list??? ... > Sub PopulateDropdown() ...
    (microsoft.public.dotnet.framework.aspnet)
  • Custom DropDownList control in ASP.NET 2.0
    ... I've created a custom dropdown list in a web control library that my ... When I compile the .aspx I get the following error for each ... is a compilation error in the Web site. ...
    (microsoft.public.dotnet.framework.aspnet)