By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,639 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,639 IT Pros & Developers. It's quick & easy.

Object Reference Error

P: n/a
Hello,

I'm trying to get the value out of a dropdown list box and
assign it to a variable. When I click on the list box, I
invoke this line of code. I get the error, "Object
reference not set to an instance of an object".

The same list box is referenced successfully in the form
load. Does anyone know what might cause this problem?
Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Post your relevant code please.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Remulac" <an*******@discussions.microsoft.com> wrote in message
news:08****************************@phx.gbl...
Hello,

I'm trying to get the value out of a dropdown list box and
assign it to a variable. When I click on the list box, I
invoke this line of code. I get the error, "Object
reference not set to an instance of an object".

The same list box is referenced successfully in the form
load. Does anyone know what might cause this problem?

Nov 20 '05 #2

P: n/a
Here's the code:

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents ListBox1 As
System.Web.UI.WebControls.ListBox
Protected WithEvents Button1 As
System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is
required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web
Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnVortex As New SqlClient.SqlConnection
(System.Configuration.ConfigurationSettings.AppSet tings
("ConnectionString"))
Dim cdAnimalTypes As New SqlClient.SqlCommand
("usp_GetAnimalTypes", cnVortex)
'cdAnimalTypes.CommandType =
CommandType.StoredProcedure
cnVortex.Open()
Dim daAnimalTypes As New SqlClient.SqlDataAdapter
(cdAnimalTypes)
Dim dsAnimalTypes As New DataSet
daAnimalTypes.Fill(dsAnimalTypes, "AnimalTypes")

ListBox1.DataSource = dsAnimalTypes
ListBox1.DataTextField = "ANIMAL_TYPE"
ListBox1.DataValueField = "ANIMAL_TYPE_ID"
ListBox1.DataBind()
End Sub

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Try
Response.Write(Session("AnimalTypeID"))
Catch ex As Exception
Response.Write(ex.Message)
End Try
'Server.Transfer("EditAnimalTypes.aspx")
End Sub


-----Original Message-----
Post your relevant code please.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Remulac" <an*******@discussions.microsoft.com> wrote in messagenews:08****************************@phx.gbl...
Hello,

I'm trying to get the value out of a dropdown list box and assign it to a variable. When I click on the list box, I invoke this line of code. I get the error, "Object
reference not set to an instance of an object".

The same list box is referenced successfully in the form
load. Does anyone know what might cause this problem?

.

Nov 20 '05 #3

P: n/a
Thanks, but you did not include the SelectedIndexChanged event handler which
is where you code is failing

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Remulac" <an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
Here's the code:

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents ListBox1 As
System.Web.UI.WebControls.ListBox
Protected WithEvents Button1 As
System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is
required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web
Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnVortex As New SqlClient.SqlConnection
(System.Configuration.ConfigurationSettings.AppSet tings
("ConnectionString"))
Dim cdAnimalTypes As New SqlClient.SqlCommand
("usp_GetAnimalTypes", cnVortex)
'cdAnimalTypes.CommandType =
CommandType.StoredProcedure
cnVortex.Open()
Dim daAnimalTypes As New SqlClient.SqlDataAdapter
(cdAnimalTypes)
Dim dsAnimalTypes As New DataSet
daAnimalTypes.Fill(dsAnimalTypes, "AnimalTypes")

ListBox1.DataSource = dsAnimalTypes
ListBox1.DataTextField = "ANIMAL_TYPE"
ListBox1.DataValueField = "ANIMAL_TYPE_ID"
ListBox1.DataBind()
End Sub

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Try
Response.Write(Session("AnimalTypeID"))
Catch ex As Exception
Response.Write(ex.Message)
End Try
'Server.Transfer("EditAnimalTypes.aspx")
End Sub


-----Original Message-----
Post your relevant code please.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Remulac" <an*******@discussions.microsoft.com> wrote in

message
news:08****************************@phx.gbl...
Hello,

I'm trying to get the value out of a dropdown list box and assign it to a variable. When I click on the list box, I invoke this line of code. I get the error, "Object
reference not set to an instance of an object".

The same list box is referenced successfully in the form
load. Does anyone know what might cause this problem?

.

Nov 20 '05 #4

P: n/a

Oops! My bad...
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Try
Response.Write(Session("AnimalTypeID"))
sValue = ListBox1.Items
(ListBox1.SelectedIndex).Value
Response.Write(sValue)
Catch ex As Exception
Response.Write(ex.Message)
End Try
'Server.Transfer("EditAnimalTypes.aspx")
End Sub
I'm getting the following error:

"Index was out of range. Must be non-negative and less
than the size of the collection. Parameter name: index "

When I take out all references to the ADO.NET objects and
populate the listbox manually, I don't get the error any
more.

Thanks for your help.

-----Original Message-----
Thanks, but you did not include the SelectedIndexChanged event handler whichis where you code is failing

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Remulac" <an*******@discussions.microsoft.com> wrote in messagenews:0c****************************@phx.gbl...
Here's the code:

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents ListBox1 As
System.Web.UI.WebControls.ListBox
Protected WithEvents Button1 As
System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is
required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnVortex As New SqlClient.SqlConnection
(System.Configuration.ConfigurationSettings.AppSet tings
("ConnectionString"))
Dim cdAnimalTypes As New SqlClient.SqlCommand
("usp_GetAnimalTypes", cnVortex)
'cdAnimalTypes.CommandType =
CommandType.StoredProcedure
cnVortex.Open()
Dim daAnimalTypes As New SqlClient.SqlDataAdapter (cdAnimalTypes)
Dim dsAnimalTypes As New DataSet
daAnimalTypes.Fill(dsAnimalTypes, "AnimalTypes")

ListBox1.DataSource = dsAnimalTypes
ListBox1.DataTextField = "ANIMAL_TYPE"
ListBox1.DataValueField = "ANIMAL_TYPE_ID"
ListBox1.DataBind()
End Sub

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Try
Response.Write(Session("AnimalTypeID"))
Catch ex As Exception
Response.Write(ex.Message)
End Try
'Server.Transfer("EditAnimalTypes.aspx")
End Sub


>-----Original Message-----
>Post your relevant code please.
>
>--
>
>OHM ( Terry Burns )
> . . . One-Handed-Man . . .
>
>Time flies when you don't know what you're doing
>
>"Remulac" <an*******@discussions.microsoft.com> wrote in
message
>news:08****************************@phx.gbl...
>> Hello,
>>
>> I'm trying to get the value out of a dropdown list
box and
>> assign it to a variable. When I click on the list
box, I
>> invoke this line of code. I get the error, "Object
>> reference not set to an instance of an object".
>>
>> The same list box is referenced successfully in the

form >> load. Does anyone know what might cause this problem? >
>
>.
>

.

Nov 20 '05 #5

P: n/a
sValue is not declared

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Remulac" <an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...

Oops! My bad...
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Try
Response.Write(Session("AnimalTypeID"))
sValue = ListBox1.Items
(ListBox1.SelectedIndex).Value
Response.Write(sValue)
Catch ex As Exception
Response.Write(ex.Message)
End Try
'Server.Transfer("EditAnimalTypes.aspx")
End Sub
I'm getting the following error:

"Index was out of range. Must be non-negative and less
than the size of the collection. Parameter name: index "

When I take out all references to the ADO.NET objects and
populate the listbox manually, I don't get the error any
more.

Thanks for your help.

-----Original Message-----
Thanks, but you did not include the SelectedIndexChanged

event handler which
is where you code is failing

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Remulac" <an*******@discussions.microsoft.com> wrote in

message
news:0c****************************@phx.gbl...
Here's the code:

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents ListBox1 As
System.Web.UI.WebControls.ListBox
Protected WithEvents Button1 As
System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is
required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnVortex As New SqlClient.SqlConnection
(System.Configuration.ConfigurationSettings.AppSet tings
("ConnectionString"))
Dim cdAnimalTypes As New SqlClient.SqlCommand
("usp_GetAnimalTypes", cnVortex)
'cdAnimalTypes.CommandType =
CommandType.StoredProcedure
cnVortex.Open()
Dim daAnimalTypes As New SqlClient.SqlDataAdapter (cdAnimalTypes)
Dim dsAnimalTypes As New DataSet
daAnimalTypes.Fill(dsAnimalTypes, "AnimalTypes")

ListBox1.DataSource = dsAnimalTypes
ListBox1.DataTextField = "ANIMAL_TYPE"
ListBox1.DataValueField = "ANIMAL_TYPE_ID"
ListBox1.DataBind()
End Sub

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Try
Response.Write(Session("AnimalTypeID"))
Catch ex As Exception
Response.Write(ex.Message)
End Try
'Server.Transfer("EditAnimalTypes.aspx")
End Sub

>-----Original Message-----
>Post your relevant code please.
>
>--
>
>OHM ( Terry Burns )
> . . . One-Handed-Man . . .
>
>Time flies when you don't know what you're doing
>
>"Remulac" <an*******@discussions.microsoft.com> wrote in message
>news:08****************************@phx.gbl...
>> Hello,
>>
>> I'm trying to get the value out of a dropdown list box and
>> assign it to a variable. When I click on the list box, I
>> invoke this line of code. I get the error, "Object
>> reference not set to an instance of an object".
>>
>> The same list box is referenced successfully in the form >> load. Does anyone know what might cause this problem? >
>
>.
>

.

Nov 20 '05 #6

P: n/a
Yeah it is. It's scoped outside of that function. Here's
the whole kit & kaboodle:
Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents ListBox1 As
System.Web.UI.WebControls.ListBox
Protected WithEvents Button1 As
System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is
required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web
Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Protected nValue As Integer
Protected sValue As String

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnVortex As New SqlClient.SqlConnection
(System.Configuration.ConfigurationSettings.AppSet tings
("ConnectionString"))
Dim cdAnimalTypes As New SqlClient.SqlCommand
("usp_GetAnimalTypes", cnVortex)
'cdAnimalTypes.CommandType =
CommandType.StoredProcedure
cnVortex.Open()
Dim daAnimalTypes As New SqlClient.SqlDataAdapter
(cdAnimalTypes)
Dim dsAnimalTypes As New DataSet
daAnimalTypes.Fill(dsAnimalTypes, "AnimalTypes")

ListBox1.DataSource = dsAnimalTypes
ListBox1.DataTextField = "ANIMAL_TYPE"
ListBox1.DataValueField = "ANIMAL_TYPE_ID"
ListBox1.DataBind()

' ListBox1.Items.Add("Test1")
' ListBox1.Items.Add("Test2")
End Sub

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Try
Response.Write(Session("AnimalTypeID"))
sValue = ListBox1.Items
(ListBox1.SelectedIndex).Value
Response.Write(sValue)
Catch ex As Exception
Response.Write(ex.Message)
End Try
'Server.Transfer("EditAnimalTypes.aspx")
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged

End Sub
End Class
-----Original Message-----
sValue is not declared

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Remulac" <an*******@discussions.microsoft.com> wrote in messagenews:0c****************************@phx.gbl...

Oops! My bad...
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Try
Response.Write(Session("AnimalTypeID"))
sValue = ListBox1.Items
(ListBox1.SelectedIndex).Value
Response.Write(sValue)
Catch ex As Exception
Response.Write(ex.Message)
End Try
'Server.Transfer("EditAnimalTypes.aspx")
End Sub
I'm getting the following error:

"Index was out of range. Must be non-negative and less
than the size of the collection. Parameter name: index "

When I take out all references to the ADO.NET objects and populate the listbox manually, I don't get the error any
more.

Thanks for your help.

>-----Original Message-----
>Thanks, but you did not include the SelectedIndexChanged
event handler which
>is where you code is failing
>
>--
>
>OHM ( Terry Burns )
> . . . One-Handed-Man . . .
>
>Time flies when you don't know what you're doing
>
>"Remulac" <an*******@discussions.microsoft.com> wrote
in message
>news:0c****************************@phx.gbl...
>> Here's the code:
>>
>>
>>
>> 'This call is required by the Web Form Designer.
>> <System.Diagnostics.DebuggerStepThrough()>
Private Sub
>> InitializeComponent()
>>
>> End Sub
>> Protected WithEvents ListBox1 As
>> System.Web.UI.WebControls.ListBox
>> Protected WithEvents Button1 As
>> System.Web.UI.WebControls.Button
>>
>> 'NOTE: The following placeholder declaration is
>> required by the Web Form Designer.
>> 'Do not delete or move it.
>> Private designerPlaceholderDeclaration As

System.Object
>>
>> Private Sub Page_Init(ByVal sender As
System.Object, >> ByVal e As System.EventArgs) Handles MyBase.Init
>> 'CODEGEN: This method call is required by the

Web
>> Form Designer
>> 'Do not modify it using the code editor.
>> InitializeComponent()
>> End Sub
>>
>> #End Region
>>
>> Private Sub Page_Load(ByVal sender As System.Object, >> ByVal e As System.EventArgs) Handles MyBase.Load
>> Dim cnVortex As New SqlClient.SqlConnection
>> (System.Configuration.ConfigurationSettings.AppSet tings >> ("ConnectionString"))
>> Dim cdAnimalTypes As New SqlClient.SqlCommand
>> ("usp_GetAnimalTypes", cnVortex)
>> 'cdAnimalTypes.CommandType =
>> CommandType.StoredProcedure
>> cnVortex.Open()
>> Dim daAnimalTypes As New

SqlClient.SqlDataAdapter
>> (cdAnimalTypes)
>> Dim dsAnimalTypes As New DataSet
>> daAnimalTypes.Fill (dsAnimalTypes, "AnimalTypes") >>
>> ListBox1.DataSource = dsAnimalTypes
>> ListBox1.DataTextField = "ANIMAL_TYPE"
>> ListBox1.DataValueField = "ANIMAL_TYPE_ID"
>> ListBox1.DataBind()
>> End Sub
>>
>> Private Sub Button1_Click(ByVal sender As
>> System.Object, ByVal e As System.EventArgs) Handles
>> Button1.Click
>> Try
>> Response.Write(Session("AnimalTypeID"))
>> Catch ex As Exception
>> Response.Write(ex.Message)
>> End Try
>> 'Server.Transfer("EditAnimalTypes.aspx")
>> End Sub
>>
>>
>>
>>
>>
>> >-----Original Message-----
>> >Post your relevant code please.
>> >
>> >--
>> >
>> >OHM ( Terry Burns )
>> > . . . One-Handed-Man . . .
>> >
>> >Time flies when you don't know what you're doing
>> >
>> >"Remulac" <an*******@discussions.microsoft.com> wrote in
>> message
>> >news:08****************************@phx.gbl...
>> >> Hello,
>> >>
>> >> I'm trying to get the value out of a dropdown list

box
>> and
>> >> assign it to a variable. When I click on the list

box,
>> I
>> >> invoke this line of code. I get the
error, "Object >> >> reference not set to an instance of an object".
>> >>
>> >> The same list box is referenced successfully in

the form
>> >> load. Does anyone know what might cause this

problem?
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 20 '05 #7

P: n/a
This is because your data is bound. I dont have time to look at this further
at the moment as I'm off to work, but as a test, put the vales into the
collection for this control using designed and comment out the DataBind()
you will see this then works.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

<an*******@discussions.microsoft.com> wrote in message
news:0d****************************@phx.gbl...
Yeah it is. It's scoped outside of that function. Here's
the whole kit & kaboodle:
Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents ListBox1 As
System.Web.UI.WebControls.ListBox
Protected WithEvents Button1 As
System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is
required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web
Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Protected nValue As Integer
Protected sValue As String

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnVortex As New SqlClient.SqlConnection
(System.Configuration.ConfigurationSettings.AppSet tings
("ConnectionString"))
Dim cdAnimalTypes As New SqlClient.SqlCommand
("usp_GetAnimalTypes", cnVortex)
'cdAnimalTypes.CommandType =
CommandType.StoredProcedure
cnVortex.Open()
Dim daAnimalTypes As New SqlClient.SqlDataAdapter
(cdAnimalTypes)
Dim dsAnimalTypes As New DataSet
daAnimalTypes.Fill(dsAnimalTypes, "AnimalTypes")

ListBox1.DataSource = dsAnimalTypes
ListBox1.DataTextField = "ANIMAL_TYPE"
ListBox1.DataValueField = "ANIMAL_TYPE_ID"
ListBox1.DataBind()

' ListBox1.Items.Add("Test1")
' ListBox1.Items.Add("Test2")
End Sub

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Try
Response.Write(Session("AnimalTypeID"))
sValue = ListBox1.Items
(ListBox1.SelectedIndex).Value
Response.Write(sValue)
Catch ex As Exception
Response.Write(ex.Message)
End Try
'Server.Transfer("EditAnimalTypes.aspx")
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged

End Sub
End Class
-----Original Message-----
sValue is not declared

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Remulac" <an*******@discussions.microsoft.com> wrote in

message
news:0c****************************@phx.gbl...

Oops! My bad...
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Try
Response.Write(Session("AnimalTypeID"))
sValue = ListBox1.Items
(ListBox1.SelectedIndex).Value
Response.Write(sValue)
Catch ex As Exception
Response.Write(ex.Message)
End Try
'Server.Transfer("EditAnimalTypes.aspx")
End Sub
I'm getting the following error:

"Index was out of range. Must be non-negative and less
than the size of the collection. Parameter name: index "

When I take out all references to the ADO.NET objects and populate the listbox manually, I don't get the error any
more.

Thanks for your help.
>-----Original Message-----
>Thanks, but you did not include the SelectedIndexChanged event handler which
>is where you code is failing
>
>--
>
>OHM ( Terry Burns )
> . . . One-Handed-Man . . .
>
>Time flies when you don't know what you're doing
>
>"Remulac" <an*******@discussions.microsoft.com> wrote in message
>news:0c****************************@phx.gbl...
>> Here's the code:
>>
>>
>>
>> 'This call is required by the Web Form Designer.
>> <System.Diagnostics.DebuggerStepThrough()> Private Sub
>> InitializeComponent()
>>
>> End Sub
>> Protected WithEvents ListBox1 As
>> System.Web.UI.WebControls.ListBox
>> Protected WithEvents Button1 As
>> System.Web.UI.WebControls.Button
>>
>> 'NOTE: The following placeholder declaration is
>> required by the Web Form Designer.
>> 'Do not delete or move it.
>> Private designerPlaceholderDeclaration As
System.Object
>>
>> Private Sub Page_Init(ByVal sender As System.Object, >> ByVal e As System.EventArgs) Handles MyBase.Init
>> 'CODEGEN: This method call is required by the
Web
>> Form Designer
>> 'Do not modify it using the code editor.
>> InitializeComponent()
>> End Sub
>>
>> #End Region
>>
>> Private Sub Page_Load(ByVal sender As System.Object, >> ByVal e As System.EventArgs) Handles MyBase.Load
>> Dim cnVortex As New SqlClient.SqlConnection
>> (System.Configuration.ConfigurationSettings.AppSet tings >> ("ConnectionString"))
>> Dim cdAnimalTypes As New SqlClient.SqlCommand
>> ("usp_GetAnimalTypes", cnVortex)
>> 'cdAnimalTypes.CommandType =
>> CommandType.StoredProcedure
>> cnVortex.Open()
>> Dim daAnimalTypes As New
SqlClient.SqlDataAdapter
>> (cdAnimalTypes)
>> Dim dsAnimalTypes As New DataSet
>> daAnimalTypes.Fill (dsAnimalTypes, "AnimalTypes") >>
>> ListBox1.DataSource = dsAnimalTypes
>> ListBox1.DataTextField = "ANIMAL_TYPE"
>> ListBox1.DataValueField = "ANIMAL_TYPE_ID"
>> ListBox1.DataBind()
>> End Sub
>>
>> Private Sub Button1_Click(ByVal sender As
>> System.Object, ByVal e As System.EventArgs) Handles
>> Button1.Click
>> Try
>> Response.Write(Session("AnimalTypeID"))
>> Catch ex As Exception
>> Response.Write(ex.Message)
>> End Try
>> 'Server.Transfer("EditAnimalTypes.aspx")
>> End Sub
>>
>>
>>
>>
>>
>> >-----Original Message-----
>> >Post your relevant code please.
>> >
>> >--
>> >
>> >OHM ( Terry Burns )
>> > . . . One-Handed-Man . . .
>> >
>> >Time flies when you don't know what you're doing
>> >
>> >"Remulac" <an*******@discussions.microsoft.com> wrote in
>> message
>> >news:08****************************@phx.gbl...
>> >> Hello,
>> >>
>> >> I'm trying to get the value out of a dropdown list
box
>> and
>> >> assign it to a variable. When I click on the list
box,
>> I
>> >> invoke this line of code. I get the error, "Object >> >> reference not set to an instance of an object".
>> >>
>> >> The same list box is referenced successfully in the form
>> >> load. Does anyone know what might cause this
problem?
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 20 '05 #8

P: n/a

Acutally, the problem was not having the IsPostBack logic
in the page load.

Thanks a million for your help!
-----Original Message-----
This is because your data is bound. I dont have time to look at this furtherat the moment as I'm off to work, but as a test, put the vales into thecollection for this control using designed and comment out the DataBind()you will see this then works.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

<an*******@discussions.microsoft.com> wrote in message
news:0d****************************@phx.gbl...
Yeah it is. It's scoped outside of that function. Here's the whole kit & kaboodle:
Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents ListBox1 As
System.Web.UI.WebControls.ListBox
Protected WithEvents Button1 As
System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is
required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Protected nValue As Integer
Protected sValue As String

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnVortex As New SqlClient.SqlConnection
(System.Configuration.ConfigurationSettings.AppSet tings
("ConnectionString"))
Dim cdAnimalTypes As New SqlClient.SqlCommand
("usp_GetAnimalTypes", cnVortex)
'cdAnimalTypes.CommandType =
CommandType.StoredProcedure
cnVortex.Open()
Dim daAnimalTypes As New SqlClient.SqlDataAdapter (cdAnimalTypes)
Dim dsAnimalTypes As New DataSet
daAnimalTypes.Fill(dsAnimalTypes, "AnimalTypes")

ListBox1.DataSource = dsAnimalTypes
ListBox1.DataTextField = "ANIMAL_TYPE"
ListBox1.DataValueField = "ANIMAL_TYPE_ID"
ListBox1.DataBind()

' ListBox1.Items.Add("Test1")
' ListBox1.Items.Add("Test2")
End Sub

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Try
Response.Write(Session("AnimalTypeID"))
sValue = ListBox1.Items
(ListBox1.SelectedIndex).Value
Response.Write(sValue)
Catch ex As Exception
Response.Write(ex.Message)
End Try
'Server.Transfer("EditAnimalTypes.aspx")
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged

End Sub
End Class
>-----Original Message-----
>sValue is not declared
>
>--
>
>OHM ( Terry Burns )
> . . . One-Handed-Man . . .
>
>Time flies when you don't know what you're doing
>
>"Remulac" <an*******@discussions.microsoft.com> wrote in
message
>news:0c****************************@phx.gbl...
>>
>> Oops! My bad...
>>
>>
>> Private Sub Button1_Click(ByVal sender As
>> System.Object, ByVal e As System.EventArgs) Handles
>> Button1.Click
>> Try
>> Response.Write(Session("AnimalTypeID"))
>> sValue = ListBox1.Items
>> (ListBox1.SelectedIndex).Value
>> Response.Write(sValue)
>> Catch ex As Exception
>> Response.Write(ex.Message)
>> End Try
>> 'Server.Transfer("EditAnimalTypes.aspx")
>> End Sub
>>
>>
>> I'm getting the following error:
>>
>> "Index was out of range. Must be non-negative and
less >> than the size of the collection. Parameter name: index " >>
>> When I take out all references to the ADO.NET objects

and
>> populate the listbox manually, I don't get the error any >> more.
>>
>> Thanks for your help.
>>
>>
>> >-----Original Message-----
>> >Thanks, but you did not include the

SelectedIndexChanged
>> event handler which
>> >is where you code is failing
>> >
>> >--
>> >
>> >OHM ( Terry Burns )
>> > . . . One-Handed-Man . . .
>> >
>> >Time flies when you don't know what you're doing
>> >
>> >"Remulac" <an*******@discussions.microsoft.com> wrote in
>> message
>> >news:0c****************************@phx.gbl...
>> >> Here's the code:
>> >>
>> >>
>> >>
>> >> 'This call is required by the Web Form

Designer. >> >> <System.Diagnostics.DebuggerStepThrough()>

Private
>> Sub
>> >> InitializeComponent()
>> >>
>> >> End Sub
>> >> Protected WithEvents ListBox1 As
>> >> System.Web.UI.WebControls.ListBox
>> >> Protected WithEvents Button1 As
>> >> System.Web.UI.WebControls.Button
>> >>
>> >> 'NOTE: The following placeholder declaration is >> >> required by the Web Form Designer.
>> >> 'Do not delete or move it.
>> >> Private designerPlaceholderDeclaration As
>> System.Object
>> >>
>> >> Private Sub Page_Init(ByVal sender As

System.Object,
>> >> ByVal e As System.EventArgs) Handles MyBase.Init
>> >> 'CODEGEN: This method call is required by the >> Web
>> >> Form Designer
>> >> 'Do not modify it using the code editor.
>> >> InitializeComponent()
>> >> End Sub
>> >>
>> >> #End Region
>> >>
>> >> Private Sub Page_Load(ByVal sender As

System.Object,
>> >> ByVal e As System.EventArgs) Handles MyBase.Load
>> >> Dim cnVortex As New SqlClient.SqlConnection >> >>

(System.Configuration.ConfigurationSettings.AppSet tings
>> >> ("ConnectionString"))
>> >> Dim cdAnimalTypes As New SqlClient.SqlCommand >> >> ("usp_GetAnimalTypes", cnVortex)
>> >> 'cdAnimalTypes.CommandType =
>> >> CommandType.StoredProcedure
>> >> cnVortex.Open()
>> >> Dim daAnimalTypes As New
>> SqlClient.SqlDataAdapter
>> >> (cdAnimalTypes)
>> >> Dim dsAnimalTypes As New DataSet
>> >> daAnimalTypes.Fill

(dsAnimalTypes, "AnimalTypes")
>> >>
>> >> ListBox1.DataSource = dsAnimalTypes
>> >> ListBox1.DataTextField = "ANIMAL_TYPE"
>> >> ListBox1.DataValueField = "ANIMAL_TYPE_ID"
>> >> ListBox1.DataBind()
>> >> End Sub
>> >>
>> >> Private Sub Button1_Click(ByVal sender As
>> >> System.Object, ByVal e As System.EventArgs) Handles >> >> Button1.Click
>> >> Try
>> >> Response.Write(Session ("AnimalTypeID")) >> >> Catch ex As Exception
>> >> Response.Write(ex.Message)
>> >> End Try
>> >> 'Server.Transfer("EditAnimalTypes.aspx")
>> >> End Sub
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> >-----Original Message-----
>> >> >Post your relevant code please.
>> >> >
>> >> >--
>> >> >
>> >> >OHM ( Terry Burns )
>> >> > . . . One-Handed-Man . . .
>> >> >
>> >> >Time flies when you don't know what you're doing
>> >> >
>> >> >"Remulac" <an*******@discussions.microsoft.com>

wrote
>> in
>> >> message
>> >> >news:08****************************@phx.gbl...
>> >> >> Hello,
>> >> >>
>> >> >> I'm trying to get the value out of a dropdown list >> box
>> >> and
>> >> >> assign it to a variable. When I click on the list >> box,
>> >> I
>> >> >> invoke this line of code. I get the

error, "Object
>> >> >> reference not set to an instance of an object".
>> >> >>
>> >> >> The same list box is referenced successfully in

the
>> form
>> >> >> load. Does anyone know what might cause this
>> problem?
>> >> >
>> >> >
>> >> >.
>> >> >
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 20 '05 #9

P: n/a
Really, so what was ur postback logic then ?

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Remulac" <an*******@discussions.microsoft.com> wrote in message
news:13****************************@phx.gbl...

Acutally, the problem was not having the IsPostBack logic
in the page load.

Thanks a million for your help!
-----Original Message-----
This is because your data is bound. I dont have time to

look at this further
at the moment as I'm off to work, but as a test, put the

vales into the
collection for this control using designed and comment

out the DataBind()
you will see this then works.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

<an*******@discussions.microsoft.com> wrote in message
news:0d****************************@phx.gbl...
Yeah it is. It's scoped outside of that function. Here's the whole kit & kaboodle:
Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents ListBox1 As
System.Web.UI.WebControls.ListBox
Protected WithEvents Button1 As
System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is
required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Protected nValue As Integer
Protected sValue As String

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnVortex As New SqlClient.SqlConnection
(System.Configuration.ConfigurationSettings.AppSet tings
("ConnectionString"))
Dim cdAnimalTypes As New SqlClient.SqlCommand
("usp_GetAnimalTypes", cnVortex)
'cdAnimalTypes.CommandType =
CommandType.StoredProcedure
cnVortex.Open()
Dim daAnimalTypes As New SqlClient.SqlDataAdapter (cdAnimalTypes)
Dim dsAnimalTypes As New DataSet
daAnimalTypes.Fill(dsAnimalTypes, "AnimalTypes")

ListBox1.DataSource = dsAnimalTypes
ListBox1.DataTextField = "ANIMAL_TYPE"
ListBox1.DataValueField = "ANIMAL_TYPE_ID"
ListBox1.DataBind()

' ListBox1.Items.Add("Test1")
' ListBox1.Items.Add("Test2")
End Sub

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Try
Response.Write(Session("AnimalTypeID"))
sValue = ListBox1.Items
(ListBox1.SelectedIndex).Value
Response.Write(sValue)
Catch ex As Exception
Response.Write(ex.Message)
End Try
'Server.Transfer("EditAnimalTypes.aspx")
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged

End Sub
End Class

>-----Original Message-----
>sValue is not declared
>
>--
>
>OHM ( Terry Burns )
> . . . One-Handed-Man . . .
>
>Time flies when you don't know what you're doing
>
>"Remulac" <an*******@discussions.microsoft.com> wrote in message
>news:0c****************************@phx.gbl...
>>
>> Oops! My bad...
>>
>>
>> Private Sub Button1_Click(ByVal sender As
>> System.Object, ByVal e As System.EventArgs) Handles
>> Button1.Click
>> Try
>> Response.Write(Session("AnimalTypeID"))
>> sValue = ListBox1.Items
>> (ListBox1.SelectedIndex).Value
>> Response.Write(sValue)
>> Catch ex As Exception
>> Response.Write(ex.Message)
>> End Try
>> 'Server.Transfer("EditAnimalTypes.aspx")
>> End Sub
>>
>>
>> I'm getting the following error:
>>
>> "Index was out of range. Must be non-negative and less >> than the size of the collection. Parameter name: index " >>
>> When I take out all references to the ADO.NET objects
and
>> populate the listbox manually, I don't get the error any >> more.
>>
>> Thanks for your help.
>>
>>
>> >-----Original Message-----
>> >Thanks, but you did not include the
SelectedIndexChanged
>> event handler which
>> >is where you code is failing
>> >
>> >--
>> >
>> >OHM ( Terry Burns )
>> > . . . One-Handed-Man . . .
>> >
>> >Time flies when you don't know what you're doing
>> >
>> >"Remulac" <an*******@discussions.microsoft.com> wrote in
>> message
>> >news:0c****************************@phx.gbl...
>> >> Here's the code:
>> >>
>> >>
>> >>
>> >> 'This call is required by the Web Form Designer. >> >> <System.Diagnostics.DebuggerStepThrough()>
Private
>> Sub
>> >> InitializeComponent()
>> >>
>> >> End Sub
>> >> Protected WithEvents ListBox1 As
>> >> System.Web.UI.WebControls.ListBox
>> >> Protected WithEvents Button1 As
>> >> System.Web.UI.WebControls.Button
>> >>
>> >> 'NOTE: The following placeholder declaration is >> >> required by the Web Form Designer.
>> >> 'Do not delete or move it.
>> >> Private designerPlaceholderDeclaration As
>> System.Object
>> >>
>> >> Private Sub Page_Init(ByVal sender As
System.Object,
>> >> ByVal e As System.EventArgs) Handles MyBase.Init
>> >> 'CODEGEN: This method call is required by the >> Web
>> >> Form Designer
>> >> 'Do not modify it using the code editor.
>> >> InitializeComponent()
>> >> End Sub
>> >>
>> >> #End Region
>> >>
>> >> Private Sub Page_Load(ByVal sender As
System.Object,
>> >> ByVal e As System.EventArgs) Handles MyBase.Load
>> >> Dim cnVortex As New SqlClient.SqlConnection >> >>
(System.Configuration.ConfigurationSettings.AppSet tings
>> >> ("ConnectionString"))
>> >> Dim cdAnimalTypes As New SqlClient.SqlCommand >> >> ("usp_GetAnimalTypes", cnVortex)
>> >> 'cdAnimalTypes.CommandType =
>> >> CommandType.StoredProcedure
>> >> cnVortex.Open()
>> >> Dim daAnimalTypes As New
>> SqlClient.SqlDataAdapter
>> >> (cdAnimalTypes)
>> >> Dim dsAnimalTypes As New DataSet
>> >> daAnimalTypes.Fill
(dsAnimalTypes, "AnimalTypes")
>> >>
>> >> ListBox1.DataSource = dsAnimalTypes
>> >> ListBox1.DataTextField = "ANIMAL_TYPE"
>> >> ListBox1.DataValueField = "ANIMAL_TYPE_ID"
>> >> ListBox1.DataBind()
>> >> End Sub
>> >>
>> >> Private Sub Button1_Click(ByVal sender As
>> >> System.Object, ByVal e As System.EventArgs) Handles >> >> Button1.Click
>> >> Try
>> >> Response.Write(Session ("AnimalTypeID")) >> >> Catch ex As Exception
>> >> Response.Write(ex.Message)
>> >> End Try
>> >> 'Server.Transfer("EditAnimalTypes.aspx")
>> >> End Sub
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> >-----Original Message-----
>> >> >Post your relevant code please.
>> >> >
>> >> >--
>> >> >
>> >> >OHM ( Terry Burns )
>> >> > . . . One-Handed-Man . . .
>> >> >
>> >> >Time flies when you don't know what you're doing
>> >> >
>> >> >"Remulac" <an*******@discussions.microsoft.com>
wrote
>> in
>> >> message
>> >> >news:08****************************@phx.gbl...
>> >> >> Hello,
>> >> >>
>> >> >> I'm trying to get the value out of a dropdown list >> box
>> >> and
>> >> >> assign it to a variable. When I click on the list >> box,
>> >> I
>> >> >> invoke this line of code. I get the
error, "Object
>> >> >> reference not set to an instance of an object".
>> >> >>
>> >> >> The same list box is referenced successfully in
the
>> form
>> >> >> load. Does anyone know what might cause this
>> problem?
>> >> >
>> >> >
>> >> >.
>> >> >
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.