Listbox & Object Not Referenced

  • Thread starter Thread starter yop
  • Start date Start date
Y

yop

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 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 83: 'Need to bind the textboxes from the
choice picked out from the list
Line 84: ' Dim ListItem As ListItem =
lstUsers.SelectedItem
Line 85: UserName.Text = stUsers.SelectedItem.Text
Line 86:
Line 87: 'Get the user details and fill textboxes

Source File: C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb Line: 85

Stack Trace:


[NullReferenceException: Object reference not set to an
instance of an object.]
AssetRegister.ViewUser.lstUsers_SelectedIndexChanged
(Object sender, EventArgs e) in C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb:85

System.Web.UI.WebControls.ListControl.OnSelectedIndexChang
ed(EventArgs e)

System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackD
ataHandler.RaisePostDataChangedEvent()
System.Web.UI.Page.RaiseChangedEvents()
System.Web.UI.Page.ProcessRequestMain()
 
Kevin

I have an item selected alright, daft idea but could it
be something to do with postback or something?

I click on the item and call the following:

Private Sub lstUsers_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles lstUsers.SelectedIndexChanged

'Need to bind the textboxes from the choice picked out
from the list
UserName.Text = lstUsers.SelectedItem.Text


thanks
-----Original Message-----
Unless an Item has been selected, this value will be null.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

yop said:
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 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 83: 'Need to bind the textboxes from the
choice picked out from the list
Line 84: ' Dim ListItem As ListItem =
lstUsers.SelectedItem
Line 85: UserName.Text = stUsers.SelectedItem.Text
Line 86:
Line 87: 'Get the user details and fill textboxes

Source File: C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb Line: 85

Stack Trace:


[NullReferenceException: Object reference not set to an
instance of an object.]
AssetRegister.ViewUser.lstUsers_SelectedIndexChanged
(Object sender, EventArgs e) in C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb:85

System.Web.UI.WebControls.ListControl.OnSelectedIndexChang
ed(EventArgs e)

System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackD
ataHandler.RaisePostDataChangedEvent()
System.Web.UI.Page.RaiseChangedEvents()
System.Web.UI.Page.ProcessRequestMain()


.
 
Try using .SelectedValue property.....

TextBox1.Text = ListBox1.SelectedValue()

If it fails too, i would check whether it is selected the
first time user comes on the web page. If that is also
set, and it is not working, i will try to see
whether "value" property returns anything. If that fails,
i have no idea why its not working.....

Since it is going to "SelectedIndexChanged" event i am
hoping that the "AutoPostBack" is set to true for your
control.

Thanks,

Tapasvi




-----Original Message-----
Kevin

I have an item selected alright, daft idea but could it
be something to do with postback or something?

I click on the item and call the following:

Private Sub lstUsers_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles lstUsers.SelectedIndexChanged

'Need to bind the textboxes from the choice picked out
from the list
UserName.Text = lstUsers.SelectedItem.Text


thanks
-----Original Message-----
Unless an Item has been selected, this value will be null.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

yop said:
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 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 83: 'Need to bind the textboxes from the
choice picked out from the list
Line 84: ' Dim ListItem As ListItem =
lstUsers.SelectedItem
Line 85: UserName.Text = stUsers.SelectedItem.Text
Line 86:
Line 87: 'Get the user details and fill textboxes

Source File: C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb Line: 85

Stack Trace:


[NullReferenceException: Object reference not set to an
instance of an object.]
AssetRegister.ViewUser.lstUsers_SelectedIndexChanged
(Object sender, EventArgs e) in C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb:85

System.Web.UI.WebControls.ListControl.OnSelectedIndexChang
ed(EventArgs e)

System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackD
ataHandler.RaisePostDataChangedEvent()
System.Web.UI.Page.RaiseChangedEvents()
System.Web.UI.Page.ProcessRequestMain()


.
.
 
Is the ListBox's AutoPostBack property set to True?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

yop said:
Kevin

I have an item selected alright, daft idea but could it
be something to do with postback or something?

I click on the item and call the following:

Private Sub lstUsers_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles lstUsers.SelectedIndexChanged

'Need to bind the textboxes from the choice picked out
from the list
UserName.Text = lstUsers.SelectedItem.Text


thanks
-----Original Message-----
Unless an Item has been selected, this value will be null.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

yop said:
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 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 83: 'Need to bind the textboxes from the
choice picked out from the list
Line 84: ' Dim ListItem As ListItem =
lstUsers.SelectedItem
Line 85: UserName.Text = stUsers.SelectedItem.Text
Line 86:
Line 87: 'Get the user details and fill textboxes

Source File: C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb Line: 85

Stack Trace:


[NullReferenceException: Object reference not set to an
instance of an object.]
AssetRegister.ViewUser.lstUsers_SelectedIndexChanged
(Object sender, EventArgs e) in C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb:85

System.Web.UI.WebControls.ListControl.OnSelectedIndexChang
ed(EventArgs e)

System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackD
ataHandler.RaisePostDataChangedEvent()
System.Web.UI.Page.RaiseChangedEvents()
System.Web.UI.Page.ProcessRequestMain()


.
 
Another possiblity: Is the ListBox dynamically added to the Page?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

yop said:
Kevin

I have an item selected alright, daft idea but could it
be something to do with postback or something?

I click on the item and call the following:

Private Sub lstUsers_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles lstUsers.SelectedIndexChanged

'Need to bind the textboxes from the choice picked out
from the list
UserName.Text = lstUsers.SelectedItem.Text


thanks
-----Original Message-----
Unless an Item has been selected, this value will be null.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

yop said:
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 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 83: 'Need to bind the textboxes from the
choice picked out from the list
Line 84: ' Dim ListItem As ListItem =
lstUsers.SelectedItem
Line 85: UserName.Text = stUsers.SelectedItem.Text
Line 86:
Line 87: 'Get the user details and fill textboxes

Source File: C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb Line: 85

Stack Trace:


[NullReferenceException: Object reference not set to an
instance of an object.]
AssetRegister.ViewUser.lstUsers_SelectedIndexChanged
(Object sender, EventArgs e) in C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb:85

System.Web.UI.WebControls.ListControl.OnSelectedIndexChang
ed(EventArgs e)

System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackD
ataHandler.RaisePostDataChangedEvent()
System.Web.UI.Page.RaiseChangedEvents()
System.Web.UI.Page.ProcessRequestMain()


.
 
Yes it is a property. Here is the code that works fine on
my machine.....

Code behind >>>>>

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles ListBox1.SelectedIndexChanged
TextBox1.Text = ListBox1.SelectedValue()
End Sub

ASPX code >>>>>

<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft
Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual
Basic .NET 7.1">
<meta name="vs_defaultClientScript"
content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post"
runat="server">
<asp:ListBox id="ListBox1"
style="Z-INDEX: 101; LEFT: 288px; POSITION: absolute; TOP:
168px"
runat="server"
Width="160px" Height="168px" AutoPostBack="True">
<asp:ListItem
Value="Tapasvi">Tapasvi</asp:ListItem>
<asp:ListItem Value="Tap
at Work">Tap at Work</asp:ListItem>
</asp:ListBox>
<asp:TextBox id="TextBox1"
style="Z-INDEX: 102; LEFT: 288px; POSITION: absolute; TOP:
120px"

runat="server"></asp:TextBox>
</form>
</body>
</HTML>

Thanks,

Tap



-----Original Message-----
Aye it is set alright.

The SelectedValue is not a property of the listbox?
-----Original Message-----
Is the ListBox's AutoPostBack property set to True?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

yop said:
Kevin

I have an item selected alright, daft idea but could it
be something to do with postback or something?

I click on the item and call the following:

Private Sub lstUsers_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles lstUsers.SelectedIndexChanged

'Need to bind the textboxes from the choice picked out
from the list
UserName.Text = lstUsers.SelectedItem.Text


thanks

-----Original Message-----
Unless an Item has been selected, this value will be
null.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

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 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 83: 'Need to bind the textboxes from the
choice picked out from the list
Line 84: ' Dim ListItem As ListItem =
lstUsers.SelectedItem
Line 85: UserName.Text =
stUsers.SelectedItem.Text
Line 86:
Line 87: 'Get the user details and fill
textboxes

Source File: C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb Line: 85

Stack Trace:


[NullReferenceException: Object reference not set to an
instance of an object.]
AssetRegister.ViewUser.lstUsers_SelectedIndexChanged
(Object sender, EventArgs e) in C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb:85


System.Web.UI.WebControls.ListControl.OnSelectedIndexChang
ed(EventArgs e)


System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackD
ataHandler.RaisePostDataChangedEvent()
System.Web.UI.Page.RaiseChangedEvents()
System.Web.UI.Page.ProcessRequestMain()







.


.
.
 
No the listbox is added at design time but the items are
added from the DB at run time. Maybe that is an issue.
Extremely frustating!! :(

Also the selectedvalue property is telling it is "not a
member of System.Web.UI.WebControls.Listbox.

I am using Framework 1.1, there is an issue with using
the webcontrols and this version I think??

Would I be better of using a 3rd party list or data list
or something?



-----Original Message-----
Yes it is a property. Here is the code that works fine on
my machine.....

Code behind >>>>>

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles ListBox1.SelectedIndexChanged
TextBox1.Text = ListBox1.SelectedValue()
End Sub

ASPX code >>>>>

<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft
Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual
Basic .NET 7.1">
<meta name="vs_defaultClientScript"
content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post"
runat="server">
<asp:ListBox id="ListBox1"
style="Z-INDEX: 101; LEFT: 288px; POSITION: absolute; TOP:
168px"
runat="server"
Width="160px" Height="168px" AutoPostBack="True">
<asp:ListItem
Value="Tapasvi">Tapasvi</asp:ListItem>
<asp:ListItem Value="Tap
at Work">Tap at Work</asp:ListItem>
</asp:ListBox>
<asp:TextBox id="TextBox1"
style="Z-INDEX: 102; LEFT: 288px; POSITION: absolute; TOP:
120px"

runat="server"></asp:TextBox>
</form>
</body>
</HTML>

Thanks,

Tap




-----Original Message-----
Aye it is set alright.

The SelectedValue is not a property of the listbox?
-----Original Message-----
Is the ListBox's AutoPostBack property set to True?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

Kevin

I have an item selected alright, daft idea but could it
be something to do with postback or something?

I click on the item and call the following:

Private Sub lstUsers_SelectedIndexChanged(ByVal
sender
As
Object, ByVal e As System.EventArgs) _
Handles lstUsers.SelectedIndexChanged

'Need to bind the textboxes from the choice picked out
from the list
UserName.Text = lstUsers.SelectedItem.Text


thanks

-----Original Message-----
Unless an Item has been selected, this value will be
null.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

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 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 83: 'Need to bind the textboxes from the
choice picked out from the list
Line 84: ' Dim ListItem As ListItem =
lstUsers.SelectedItem
Line 85: UserName.Text =
stUsers.SelectedItem.Text
Line 86:
Line 87: 'Get the user details and fill
textboxes

Source File: C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb Line: 85

Stack Trace:


[NullReferenceException: Object reference not set to an
instance of an object.]
AssetRegister.ViewUser.lstUsers_SelectedIndexChanged
(Object sender, EventArgs e) in C:\Dev\Asset Register
SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
Register\AssetRegister\ViewUsers.aspx.vb:85
System.Web.UI.WebControls.ListControl.OnSelectedIndexCha ng
ed(EventArgs e)
System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBac kD
ataHandler.RaisePostDataChangedEvent()
System.Web.UI.Page.RaiseChangedEvents()
System.Web.UI.Page.ProcessRequestMain()







.



.
.
.
 
eventually found the fix after trawling the forums!!

"I was loading the listbox dynamically on the page_load
event.
Eventually I discovered - that was too late,
The items showed on the page but weren't really
there..... (figure that one
out)
When I moved the list populate routine to the page_init
event everything
worked great."

Thanks for all your help
 
Back
Top