Do not use on any
shared computer
May 17, 2008 02:02am pdt
05.09.2008 at 11:52AM PDT, ID: 23390481 | Points: 500
[x]
Attachment Details
"Object reference not set to an instance of an object" - Simple? ASP.NET Issue, calling Code Behind Method...
I just don't get it.  Can someone figure out why I'm getting this exception?

All these controls are inside a FormView control.  The idea is when the user changes the value of a drop down, today's date is insert into a text box using a VB.NET method in the Code Behind method.  This process works fine if I use an update panel with a trigger (like I do inside the InsertItemTemplate), but if I don't use an updatepanel and instead set the OnSelectedIndexChanged property of the Drop Down (causing a refresh) I get this error when the program executes the VB Code Behind method.

Can anyone help? It feels like I'm missing something really simple...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
<!--ASP CODE, Inside edititemtemplate of a FormView control.  InsertItem uses AJAX to set the date, using same VB Method, which works fine-->
 
<asp:DropDownList ID="cboFinalResult" runat="server" SelectedValue='<%# Bind("finalResult") %>' AutoPostBack="True" OnSelectedIndexChanged="cboFinalResult_SelectedIndexChanged">
<asp:ListItem Value="">not entered</asp:ListItem>
<asp:ListItem Value="Contact">Destroyed</asp:ListItem>
<asp:ListItem Value="No Contact">Re-Exported</asp:ListItem>
<asp:ListItem Value="Sale">Treated</asp:ListItem>
</asp:DropDownList>
                            
 
<br />
Final Result Date<asp:CompareValidator ID="cmpFinalResultDate" runat="server" ControlToValidate="txtFinalResultDate"Operator="DataTypeCheck" Type="date" ErrorMessage="*date" Font-Size=".8em"></asp:CompareValidator><br />
 
<asp:TextBox ID="txtFinalResultDate" runat="server" Text='<%# Bind("finalResultdate", "{0:d}") %>'></asp:TextBox>
 
'VB.NET CODE, WHICH THROWS THE EXCEPTION
 
Protected Sub cboFinalResult_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboFinalResult.SelectedIndexChanged
If CType(fvSales.FindControl("cboFinalResult"), DropDownList).Text <> "Not Entered" Then
Me.txtFinalResultDate.Text = Today.ToShortDateString 'THIS LINE CAUSES THE EXCEPTION*********************************
End If
End Sub
 
 
<!-- EXCEPTION INFORMATION-->
 
System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="App_Web_qy1swmje"
  StackTrace:
       at saleEntry.cboFinalResult_SelectedIndexChanged(Object sender, EventArgs e) in c:\inetpub\wwwroot\web\saleEntry.aspx.vb:line 390
       at System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e)
       at System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent()
       at System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent()
       at System.Web.UI.Page.RaiseChangedEvents()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: synapse88
Question Asked On: 05.09.2008
Participating Experts: 1
Points: 500
Views: 0
Translate:
Loading Advertisement...
05.09.2008 at 12:02PM PDT, ID: 21535591

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 12:22PM PDT, ID: 21535739

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 12:31PM PDT, ID: 21535789

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 03:40PM PDT, ID: 21536832

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 06:43AM PDT, ID: 21546893

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 07:36AM PDT, ID: 21547342

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 07:37AM PDT, ID: 21547356

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 07:47AM PDT, ID: 21547438

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.09.2008 at 12:02PM PDT, ID: 21535591

Rank: Master

You mention that all the controls are inside a formview - but you don't cast the txtFinalResultDate as one inside the formview.

Dim txtFinalResultDate.Text As TextBox = DirectCAst(Formview1.Findcontrol("txtFinalResultDate"), TextBox)
txtFinalResultDate.Text = Today.ToShortDateString
 
05.09.2008 at 12:22PM PDT, ID: 21535739
I couldn't use "Dim txtFinalResultDate.Text......", if so I got an "end of statement expected" error

So I I tried the following code (using your suggesting), and got the same result.

1:
2:
3:
4:
Dim txtFinalResultDate As TextBox = DirectCAst(Formview1.Findcontrol("txtFinalResultDate"), TextBox)
 
 
txtFinalResultDate.Text = Today.ToShortDateString
Open in New Window
 
05.09.2008 at 12:31PM PDT, ID: 21535789

Rank: Master

Yeah, that was my fault - was trying to type quickb efore I left my desk.  Is your formview called formview1?  I notice in your other declaration you use findcontrol on a "fvSales"

So try this if that is referencing your formview:

Dim txtFinalResultDate As TextBox = DirectCAst(fvSales.Findcontrol("txtFinalResultDate"), TextBox)
 
 
txtFinalResultDate.Text = Today.ToShortDateString
 
05.09.2008 at 03:40PM PDT, ID: 21536832
Well yeah I had changed the names so they were correct, but I got the same exception on the second statement on the code (same place, same exception)
 
05.12.2008 at 06:43AM PDT, ID: 21546893

Rank: Master

Interesting... you said the drop down list was also in the edit template?  So the only time you could be changing it would be if you were in edit mode?  I did a simple test with a formview and a textbox that is inside edititem template and it works fine to access the textbox inside the the change of a dropdownlist - but I put my dropdownlist outside the formview.  That shouldn't make a difference though.  SO the only thing I'm thinking is maybe the dropdownlist is outside the formview's edit item template, and you might be changing it before the formview is in edit mode?  If not we need to find out what is different about your setup than a normal setup.  Has to be something missing.

Try surrounding it with something simliar to this to make sure it only fires off when the formview is in editmode (and hence we know the textbox is there)

1:
2:
3:
4:
5:
If fvSales.CurrentMode = FormViewMode.Edit Then
            Dim tb1 As TextBox = DirectCast(fvSales.FindControl("txtFinalResultDate"), TextBox)
            tb1.Text = "Today.ToShortDateString
 
        End I
Open in New Window
 
05.12.2008 at 07:36AM PDT, ID: 21547342
The drop down list is in both templates (insert and edit).  You can change it in either template.

so that code  you posted works fine for you?  can i see all your code?  the ASP code and the VB code (so i can run an identical test with that code).
 
05.12.2008 at 07:37AM PDT, ID: 21547356

Rank: Master

Sure, I didn't save my code - but it only took me like 5minutes to set up a small test.  Give me a few and I'll post the complete code.
 
05.12.2008 at 07:47AM PDT, ID: 21547438

Rank: Master

Here's a simple test, obviously to test on your side you'll need to change the sqldatasource to pull data from your database, but it's a dropdownlist inside the editItemtemplate of a formview, and I change the text of a textbox that is also inside the editItemTemplate of the same formview on post back.... So I wonder what is different about your setup.  Should use the same concept even if it s a little more complicated.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test2.aspx.vb" MaintainScrollPositionOnPostback="true" Inherits="test2" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        &nbsp;<asp:FormView ID="FormView1" runat="server" CellPadding="4" DataKeyNames="WorkoutPersonID"
            DataSourceID="SqlDataSource1" ForeColor="#333333">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1">
                    <asp:ListItem>- Select Item - </asp:ListItem>
                    <asp:ListItem Value="Changed">Change 1 made</asp:ListItem>
                    <asp:ListItem Value="Changed">Change 2 made</asp:ListItem>
                </asp:DropDownList><br />
                WorkoutPersonID:
                <asp:Label ID="WorkoutPersonIDLabel1" runat="server" Text='<%# Eval("WorkoutPersonID") %>'></asp:Label><br />
                WorkoutPerson:
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("WorkoutPerson") %>'></asp:TextBox><br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                    Text="Update"></asp:LinkButton>
                <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
            <InsertItemTemplate>
                WorkoutPerson:
                <asp:TextBox ID="WorkoutPersonTextBox" runat="server" Text='<%# Bind("WorkoutPerson") %>'>
                </asp:TextBox><br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                    Text="Insert">
                </asp:LinkButton>
                <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel">
                </asp:LinkButton>
            </InsertItemTemplate>
            <ItemTemplate>
                WorkoutPersonID:
                <asp:Label ID="WorkoutPersonIDLabel" runat="server" Text='<%# Eval("WorkoutPersonID") %>'></asp:Label><br />
                WorkoutPerson:
                <asp:Label ID="WorkoutPersonLabel" runat="server" Text='<%# Bind("WorkoutPerson") %>'></asp:Label><br />
                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit"></asp:LinkButton>
                <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                    Text="Delete"></asp:LinkButton>
                <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                    Text="New"></asp:LinkButton>
            </ItemTemplate>
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
        </asp:FormView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DavesDesignConnectionString %>"
            DeleteCommand="DELETE FROM [WorkoutPerson] WHERE [WorkoutPersonID] = @WorkoutPersonID"
            InsertCommand="INSERT INTO [WorkoutPerson] ([WorkoutPerson]) VALUES (@WorkoutPerson)"
            SelectCommand="SELECT [WorkoutPersonID], [WorkoutPerson] FROM [WorkoutPerson]"
            UpdateCommand="UPDATE [WorkoutPerson] SET [WorkoutPerson] = @WorkoutPerson WHERE [WorkoutPersonID] = @WorkoutPersonID">
            <DeleteParameters>
                <asp:Parameter Name="WorkoutPersonID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="WorkoutPerson" Type="String" />
                <asp:Parameter Name="WorkoutPersonID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="WorkoutPerson" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        <br />
        &nbsp;
 
    </form>
</body>
</html>
 
 
 
***************CODE BEHIND
 
 
 
Partial Class test2
    Inherits System.Web.UI.Page
 
 
    Protected Sub DropDownList1_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
        If FormView1.CurrentMode = FormViewMode.Edit Then
            Dim ddl1 As DropDownList = DirectCast(FormView1.FindControl("DropDownList1"), DropDownList)
            If ddl1.SelectedValue <> "- Select Item -" Then
                Dim tb1 As TextBox = DirectCast(FormView1.FindControl("TextBox1"), TextBox)
                tb1.Text = Today.ToShortDateString
            End If
        End If
    End Sub
End Class
Open in New Window
 
 
20080206-EE-VQP-25 / EE_QW_2_20070628