Jump to content
kwilliams

Problem Getting Selected Value From Dropdownlist Using Arraylist As Datasource

Recommended Posts

I'm having a problem getting the selected value from a DropDownList that uses an ArrayList as a DataSource. When I select a year and click on the submit button, I get the following error message: Object reference not set to an instance of an object. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. I'm not understanding why this isn't working. The years (1900 - 1993) are getting populated into the DropDownList without a problem, but on the btnSubmit_Click job gets submitted, I get that error. And and all help would be appreciated. Hopefully it's something simple that I'm missing. Here's the code I'm using: VB.NET CODE-BEHIND:

'Assign date variables	Dim dtCurrDate As DateTime = DateTime.Now 'Assign current date	Dim intCurrYear As Integer = DateTime.Now.Year 'Assign current year	Dim intVotingYear As Integer = intCurrYear - 18 'Assign voting year Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Load 	If Not Page.IsPostBack Then 'loads with page 			'Declare an array for the dynamic dropdownlist			Dim colArrayList As New System.Collections.ArrayList() 			'Declare loop variables			Dim i As Integer = 1900			Dim intDisplayYear As Integer = i 			'Loop through years			For i = 1900 To intVotingYear				colArrayList.Add(i)				colArrayList.TrimToSize()				colArrayList.Sort() 'Sort years				colArrayList.Reverse() 'Reverse sort/descending order			Next 			'Bind data			ddlYear.DataSource = colArrayList			ddlYear.DataBind() 			'Add a new listitem to the beginning of the dropdownlist			ddlYear.Items.Insert(0, New ListItem("- Year -", "")) 'Text, value			ddlYear.SelectedIndex = ddlYear.Items.IndexOf(ddlYear.Items.FindByText(Session("SelectedYear"))) 		End If End Sub Sub btnSubmit_Click(ByVal Sender As Object, ByVal E As EventArgs) 	'Declare form variables	Dim strFormYear As String = ddlYear.SelectedItem.Text '<<<<-----THIS IS WHERE I GET THE ERROR 	'NOTE: I've also tried changing the above line to:	'Dim strFormYear As String = ddlYear.SelectedItem.Value	'...but that resulted in the same error. 	'Declare session variables	Session("SelectedYear") = dob_year_form 	If Page.IsValid Then		Try...		...	End If End Sub

ASP.NET PAGE:

<form id="form1" method="post" runat="server"> 	<asp:DropDownList id="ddlYear" runat="server"></asp:DropDownList> 	<asp:Button ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" runat="server" /> </form>

Share this post


Link to post
Share on other sites

I thought that posting my solution might help others, so here it goes:After getting some advice from another forum user, I was able to solve the problem by taking a step back. I did that by using a simple example of the ArrayList at W3Schools at http://www.w3schools.com/aspnet/aspnet_arraylist.asp to compare it to my code. The issue ended up being in the page declaration. I had EnableViewState="false", and I changed it to EnableViewState="true". Once I did that, and I placed the initial code within an "If Not Page.IsPostBack Then...", it worked as it should. :good:

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...