Still can't see why I'm getting this error!

Discussion in 'ASP .Net' started by Guest, Jul 28, 2004.

  1. Guest

    Guest Guest

    I keep getting the following error on a dropdownlist control I have added to a datagrid! I have given the control the correct id and don't get any errors when conpiling the code but as soon as I try to access the page I receive the error show below!

    Can someone please tell me why and how I might fix it!

    Thanks!

    ERROR: Object reference not set to an instance of an object.


    ...::INLINE CODE
    <asp:datagrid id="DGPages" runat="server" OnDeleteCommand="DGPages_Delete" OnCancelCommand="DGPages_Cancel"
    OnUpdateCommand="DGPages_Update" OnEditCommand="DGPages_Edit" DataKeyField="pageID" AutoGenerateColumns="False" Width="90%" CellPadding="4" EditItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="Black" HeaderStyle-ForeColor="White" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True" ShowFooter="True">
    <Columns>
    <asp:TemplateColumn HeaderText="ID">
    <ItemTemplate>
    <%# Container.DataItem("pageID") %>
    </ItemTemplate>
    <EditItemTemplate>
    <%# Container.DataItem("pageID") %>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Description">
    <ItemTemplate>
    <%# Container.DataItem("description") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="description" Columns="5" Text='<%# Container.DataItem("description") %>' Runat="server" />
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="add_Description" Columns="5" Runat="Server" />
    </FooterTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Title">
    <ItemTemplate>
    <%# Container.DataItem("header") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="title" Columns="5" Text='<%# Container.DataItem("header") %>' Runat="server" />
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="add_title" Columns="5" Runat="Server" />
    </FooterTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Office">
    <ItemTemplate>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="office" Columns="5" Text='<%# Container.DataItem("offName") %>' Runat="server" />
    </EditItemTemplate>
    <FooterTemplate>
    <!-- Pull Office Names from Database -->
    <asp:DropDownList id="add_Office" runat="server"></asp:DropDownList>
    </FooterTemplate>
    </asp:TemplateColumn>
    <asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" EditText="Edit" HeaderText="Edit"></asp:EditCommandColumn>
    <asp:TemplateColumn HeaderText="Delete">
    <FooterTemplate>
    <asp:Button CommandName="Insert" Text="Add" ID="btnAdd" Runat="server" />
    </FooterTemplate>
    <ItemTemplate>
    <asp:Button CommandName="Delete" Text="Delete" ID="btnDel" Runat="server" />
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>

    ...:: CODE BEHIND
    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 ' *** ERROR HERE
    add_Office.DataValueField = "officeID"
    add_Office.DataTextField = "offName"
    add_Office.DataBind()

    myReader.Close()

    Myconn.Close()
    End If
    End Sub
     
    Guest, Jul 28, 2004
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.