Referencing listbox value error

A

Allan Horwitz

When I try to reference the lstproducts.selecteditem.value using
autopostback from the lstproducts listbox I get an error? How can I
fix my code to get the selected value from the lstproducts listbox?

Any help will be appreciated.

Sincerely,
Allan


error code:
===============================================

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 75: response.write(lstproducts.selecteditem.value +
" ")

============================================



source code:
===========================================
<%@ Page Language="VB" debug="true"%>
<%@ import Namespace="system.data.sqlclient" %>
<script runat="server">

' Insert page code here
'
dim strbg as string="white"


sub page_load

dim strOrderBy as string

if not ispostback then

strOrderBy = "SSN"

else





if radSSN.checked then
strOrderBy = "SSN"
else
strOrderBy = "Name"
end if

end if







dim conMyData as sqlconnection
dim cmdSelect as sqlcommand
dim dtrNames as sqldatareader


conMyData = new
sqlconnection("server=xxxxxxxx;uid=xxxxxx;pwd=xxxxx;database=xxxxxx")
conMyData.open()

if strOrderBy = "SSN" then
cmdSelect = new sqlcommand("select ssn + ' | ' +
lname + ', ' + fname as person, ssn from EEHIST where deleted = 0
order by SSN", conMyData)
lblHeader.text = "Find Employee By SSN"
else
cmdSelect = new sqlcommand("select ssn + ' | ' +
lname + ', ' + fname as person, ssn from EEHIST where deleted = 0
order by lname", conMyData)
lblHeader.text = "Find Employee By Name"
end if

dtrNames = cmdSelect.ExecuteReader()

lstProducts.datasource = dtrNames
lstProducts.datatextfield = "person"
lstProducts.datavaluefield = "ssn"

lstProducts.databind()

dtrNames.close()

conMyData.close()





end sub


sub lstproducts_selectedindexchanged(s as object, e as
eventargs)

response.write(lstproducts.selecteditem.value + " ")
response.write("emps_person_plan.aspx")

end sub



sub lstcolors_selectedindexchanged(s as object, e as eventargs)

response.write(lstcolors.selecteditem.value + " ")
strbg = lstcolors.selecteditem.value
if strbg <> "white" then
response.write("not white")
end if
'response.redirect("emps.aspx")

end sub

</script>
<html>
<head>
</head>
<body bgcolor="<%=strbg%>">
<form runat="server">

<asp:listbox id="lstcolors" autopostback="true"
onselectedindexchanged="lstcolors_selectedindexchanged"
runat="server">

<asp:listitem text="select one" value="white" selected="true"/>
<asp:listitem text="red" value="red"/>
<asp:listitem text="green" value="green"/>
<asp:listitem text="blue" value="blue"/>

</asp:listbox>


<asp:radiobutton id="radSSN" checked="true" groupname="orderby"
autopostback="true" text="By SSN" runat="server"/>


<asp:radiobutton id="radName" groupname="orderby" autopostback="true"
text="By Name" runat="server"/>

<asp:label id="lblHeader" runat="server"/>


<asp:listbox id="lstproducts"
onselectedindexchanged="lstproducts_selectedindexchanged"
autopostback="true" rows="10" font-size="14px"
runat="server"/>



</form>
</body>
</html>
=================================================
 
A

Alvin Bruney

Before you access the object make sure you have tested it to see if it is
valid first. It may not be for a variety of reasons. Consider (defensive
program)

if(listbox1 != null && listbox.selectedindex > -1)
//do something

otherwise there is no listbox or it doesn't contain a valid selection
 
A

Allan Horwitz

Thank you for the advice.

I was able to fix the problem by only populating the listbox on non-postbacks.

Sincerely,
Allan
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top