472,353 Members | 1,031 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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
2 1302
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: yop | last post by:
All When I try to get the text from my listbox I am get an error which is listed below. Any ideas? Thanks Object reference not set to an...
4
by: Moe Sizlak | last post by:
Hi There, I am trying to return the value of a listbox control that is included as a user control, I can return the name of the control but I...
0
by: N. Demos | last post by:
Hello, I have a custom usercontrol, of which I have two instances of in my aspx page. Both the usercontrol and page have codebehind. In the page...
2
by: Young J. Putt | last post by:
I've got a list box bound to a Datatable, like this: lstProjects.DataSource = m_oProjectSet.DataTable lstProjects.DisplayMember = "ProjectDesc"...
3
by: George | last post by:
Sub ExcelToListBox() Dim xRange As Object Dim ary Dim xValue As String xRange = oXL.Range("A1:A9") 'has letters A-H ary = xRange.value xValue...
28
by: cjobes | last post by:
Hi all, I need to populate a listbox from a table column. The column has multiple entries of usernames and I need to pull a unique set of...
11
by: Zorpiedoman | last post by:
The problem is this: I have a list box. I set an array list as the datasource. I remove an item from the array list. I set the listbox...
6
by: Paul | last post by:
Hi All, Framework 1.1 listbox control unable to DataBind I've been googling for an answer to this query that appears quite a lot, but none, it...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.