467,081 Members | 1,228 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

Referencing listbox value error

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=xxxx x;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_selectedindexcha nged"
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_selectedindexc hanged"
autopostback="true" rows="10" font-size="14px"
runat="server"/>

</form>
</body>
</html>
=================================================
Nov 18 '05 #1
  • viewed: 1186
Share:
2 Replies
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

--
Regards,
Alvin Bruney
Got Tidbits? Get it here
www.networkip.net/tidbits
"Allan Horwitz" <ah******@colburn.com> wrote in message
news:e4**************************@posting.google.c om...
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=xxxx x;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_selectedindexcha nged"
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_selectedindexc hanged"
autopostback="true" rows="10" font-size="14px"
runat="server"/>

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

Nov 18 '05 #2
Thank you for the advice.

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

Sincerely,
Allan

"Alvin Bruney" <va******@hotspammailme.com> wrote in message news:<O5**************@TK2MSFTNGP12.phx.gbl>...
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

--
Regards,
Alvin Bruney
Got Tidbits? Get it here
www.networkip.net/tidbits
"Allan Horwitz" <ah******@colburn.com> wrote in message
news:e4**************************@posting.google.c om...
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=xxxx x;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_selectedindexcha nged"
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_selectedindexc hanged"
autopostback="true" rows="10" font-size="14px"
runat="server"/>

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

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by yop | last post: by
3 posts views Thread by George | last post: by
28 posts views Thread by cjobes | last post: by
11 posts views Thread by Zorpiedoman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.