Object Reference Error

  • Thread starter Thread starter Remulac
  • Start date Start date
R

Remulac

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?
 
Post your relevant code please.

--

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

Time flies when you don't know what you're doing
 
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.AppSettings
("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
 
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
 
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.
 
sValue is not declared

--

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

Time flies when you don't know what you're doing
 
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.AppSettings
("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
 
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
 
Acutally, the problem was not having the IsPostBack logic
in the page load.

Thanks a million for your help!
 
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
 
Back
Top