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:
The code that works:
The code that fills the list box from my database and doesn't work:
Both use the same page code to handle the Accept button click:
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?
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?