How do I use the OnSelectedIndexChanged event of a dropdownlist?

I have created a C# web site using visual studio 2008. I am using an asp.net 3.5 listview control as the frontend to my sql server 2005 database. Within the edititemtemplate of my listview control I have a dropdownlist. when a user selects a certain value in this dropdownlist I want a textbox to become visible in my edit template so the user can enter a specific date value. I have an ajax calendar extender hooked to this textbox so the value the user enters will always be a valid datetime value for my database field which is set to allow nulls as a date is not always required depending on the dropdownlist selection. Unfortunately for some reason I keep receiving the error: "Object reference not set to an instance of an object" when I try to edit the drop down list value? Even stranger, I only seem to receive this error after I have deployed my project to a live server, within the visual studio development server the drop down edits as expected? Attached is the code for my drop down list and the date textbox in the edititemtemplate for my listview and the code for my OnSelectedIndexChanged event of my drop down. FrequencyCode is an int, FrequencyDescription is a nvarchar, StopDate is a smalldatetime. The FrequencyCode value of 5 is the only value where I don't want the textbox to be visible within the edititemtemplate.
<asp:DropDownList ID="FrequencyEditDDL" runat="server" CssClass="ddlFreqActionEvent" DataTextField="FrequencyDescription" 
                                                                DataValueField="FrequencyCode" SelectedValue='<%# Bind("FrequencyCode") %>' DataSourceID="FrequencyEditDataSource" AutoPostBack="true" OnSelectedIndexChanged="FreqEditDDL_SelectedChanged">
                                                            </asp:DropDownList>
                                                            <asp:LinqDataSource ID="FrequencyEditDataSource" ContextTypeName="TrustSystemDataContext" TableName="PaymentFrequencyLookups" runat="server"/>
                                                            <br />
                                                            <asp:TextBox ID="StopDateEditTextBox" runat="server" Text='<%# Bind("StopDate") %>' CssClass="calendarTxtBox"></asp:TextBox>
                                                            <ajaxToolKit:CalendarExtender ID="StopDateCalendarEdit" runat="server" TargetControlID="StopDateEditTextBox" Format="dd/MM/yyyy" CssClass="MyCalendar" PopupButtonID="StopDateCalendarImage">
                                                            </ajaxToolKit:CalendarExtender>
 
**************************************************************************************
 
protected void FreqEditDDL_SelectedChanged(object sender, EventArgs e)
    {
        DropDownList ddlEditFrequencyCode = (DropDownList)lvPayments.EditItem.FindControl("FrequencyEditDDL");
        TextBox tbEditStopDate = (TextBox)lvPayments.EditItem.FindControl("StopDateEditTextBox");
 
        if (ddlEditFrequencyCode.SelectedValue != "5")
        {
            tbEditStopDate.Visible = true;
        }
        else
        {
            tbEditStopDate.Visible = false;
        }
    }

Open in new window

ShepweddAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

garethh86Commented:
Try the below, hope it helps - my c# isn't the best, i mainly deal with vb
protected void FreqEditDDL_SelectedChanged(object sender, EventArgs e)
{
    DropDownList ddlEditFrequencyCode = (DropDownList)lvPayments.EditItem.FindControl("FrequencyEditDDL");
    TextBox tbEditStopDate = (TextBox)lvPayments.EditItem.FindControl("StopDateEditTextBox");
    if (ddlEditFrequencyCode.SelectedValue != "5") {
        tbEditStopDate.Visible = true;
    }
    else {
        tbEditStopDate.Visible = false;
    }
}

Open in new window

0
ShepweddAuthor Commented:
After further testing I have discovered that my edit seems to work perfectly when the item is at index 1 or above? It's just my first item (index 0) in the edititemtemplate where I receive the error?
0
ShepweddAuthor Commented:
garethh86,

Did you change any of my code? your suggestion code seems to be exactly the same as my attached code?
0
M3mph15Commented:
HI,

At what line is it giving the "Object reference not set to an instance of an object" error.

Also try using if (ddlEditFrequencyCode.SelectedValue.ToString() != "5")

Let me know how this goes

-M3mph15
0
ShepweddAuthor Commented:
This ended up being a .net 3.5 framework problem. I installed the .net 3.5 SP1 framework on the server to solve this issue.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.