Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 1st, 2011, 01:43 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Question Problem getting selected value from DropDownList using ArrayList as DataSource

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:
Code:
'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:

Code:
<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>
  #2 (permalink)  
Old December 2nd, 2011, 03:40 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dropdownlist selected value avidan ASP.NET 4 General Discussion 1 December 25th, 2010 02:56 PM
DropDownList, DataSource and Selected Items kingroon ASP.NET 2.0 Basics 2 February 25th, 2008 10:41 AM
Dropdownlist datasource jilly ASP.NET 1.0 and 1.1 Basics 1 October 22nd, 2006 10:16 AM
Arraylist is datasource for listbox. Failing. CharissaJB C# 2005 0 September 14th, 2006 10:33 AM
DropDownList Selected Index = -1?? dorothee ASP.NET 1.0 and 1.1 Basics 4 June 11th, 2004 03:08 AM



All times are GMT -4. The time now is 06:11 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.