Object reference error

Jackpanel

Freshman
Joined
Feb 23, 2004
Messages
35
I'm trying to create a page that displays a sales opportunity. From this page, a user can select several pop-up windows to fill in various fields. For example:

Customer: ______Joe Blow______ [Change Customer}

When the user clicks the Change Customer button, a pop-up window pulls a list of all available customers, pulled from my database. The user selects one, clicks "Accept" and it returns to the main page where "Joe Blow" now reads "John Smith", and the hidden CustomerID field is updated to John Smith's ID #.

I've got this working perfectly when I manually create the customer listbox. However, when I try to fill the listbox from my database, the exact same coding returns an error.

Here's the code from my Opportunity page to pop up the Customer selection window:

Code:
<input id="CustomerID" readonly="readonly" type="hidden" runat="server" />
<input class="input1" id="lblCustomer" readonly="readonly" type="text" runat="server" />
<a href="javascript:customer_window=window.open('Customer.aspx?
formname=OpportunityForm.lblCustomer&
formid=OpportunityForm.CustomerID','customer_window',
'width=400,height=350,left=200,top=200');
customer_window.focus()">
<img alt="Select Customer" src="images/choose.gif" border="0" /></a>

The code that works:

Code:
<form runat="server">
            <asp:ListBox id="CustomerListBox" runat="server" Width="356px" Height="169px">
                <asp:ListItem Value="1">Customer 1</asp:ListItem>
                <asp:ListItem Value="2">Customer 2</asp:ListItem>
                <asp:ListItem Value="3">Customer 3</asp:ListItem>
            </asp:ListBox>
            <asp:Button id="btnAccept" onclick="btnAccept_Click" runat="server" Text="Accept">
            </asp:Button>
            <asp:Literal id="Literal1" runat="server"></asp:Literal>
    </form>


The code that fills the list box from my database and doesn't work:

Code:
    <form runat="server">
            <asp:ListBox id="CustomerListBox" runat="server" Rows="10" 
            DataValueField="ID" DataTextField="Name" Width="356px">
            </asp:ListBox>
            <asp:Button id="btnAccept" onclick="btnAccept_Click" 
            runat="server" Width="178px" Text="Accept"></asp:Button>
            <asp:Literal id="Literal1" runat="server"></asp:Literal>
    </form>


sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" &
server.mappath("database/customer.mdb"))
dbconn.Open()
sql="SELECT * FROM customer"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()

  CustomerListBox.DataSource = dbread
  CustomerListBox.DataBind()

dbread.Close()
dbconn.Close()
end sub

Both use the same page code to handle the Accept button click:

Code:
Sub btnAccept_Click(sender As Object, e As EventArgs)
    Dim strjscript as string = "<script language=""javascript"">"
    strjscript = strjscript & "window.opener." & 
    Httpcontext.Current.Request.Querystring("formname") & 
    ".value = '" & CustomerListBox.SelectedItem.Text & 
    "';window.opener." & Httpcontext.Current.Request.Querystring("formID") & 
    ".value = '" & CustomerListBox.SelectedItem.value & "';window.close();"
    strjscript = strjscript & "</script" & ">"
    Literal1.text = strjscript
End Sub



Using the database to fill the listbox works, but when I select a customer name and click the Accept button, I get the following error:

Server Error in '/' Application.
--------------------------------------------------------------------------------

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:


Line 24: Sub btnAccept_Click(sender As Object, e As EventArgs)
Line 25: Dim strjscript as string = "<script language=""javascript"">"
Line 26: strjscript = strjscript & "window.opener." &
Httpcontext.Current.Request.Querystring("formname") & ".value = '" &
CustomerListBox.SelectedItem.Text & "';window.opener." &
Httpcontext.Current.Request.Querystring("formID") & ".value = '" &
CustomerListBox.SelectedItem.value & "';window.close();"
Line 27: strjscript = strjscript & "</script" & ">"
Line 28: Literal1.text = strjscript


Any ideas on why I'm getting this error with a generated listbox, but not a manually entered listbox?
 
Im almost positive its your CustomerListBox.SelectedItem.value that is the problem. Put a page break on line 24 and step through and see if its reading the information. Is anything null? I would bet that when you populate the listbox via database that only the name is filled in and not the value.
 
I believe I know the problem. I'll bet that you're populating the
ListBox every time the page loads, right? If that's the case, then
when you click Accept the ListBox is re-populated and your
selected item is lost. What you need to do is, in Page_Load, check
Page.IsPostBack to determine if the page is being posted back
to the server (through and event like a button click) or is
being requested for the the first time.

So, in Page_Load:
Visual Basic:
If Not Page.IsPostBack Then ' If being loaded for the first time
  ' Populate the ListBox here
End If

David, even if the Name or Value of the SelectedItem were
null, an empty string would be concatenated; an error would not
be thrown.

[edit]Eek, post 666![/edit]
 
Back
Top