Microsoft KB Archive/313673

= PRB: A NullReferenceException Occurs If You Use the SelectionList Control =

Article ID: 313673

Article Last Modified on 6/14/2002

-

APPLIES TO


 * Microsoft Mobile Internet Toolkit 1.0

-



This article was previously published under Q313673



SYMPTOMS
You may receive the following error message when you are browsing a mobile Web Form that accesses the Selection property of a SelectionList control:

System.NullReferenceException: Object reference not set to an instance of an object.



CAUSE
This behavior may occur if the SelectionList is databound on each postback and the event that does the databinding is before the event that attempts to access the Selection property of the SelectionList control. In this case, the Selection property is null because the control has been re-bound. For example, the error is generated if databinding occurs in the Page_Load event, and if the Selection property is accessed in a Click event for a Command control.



RESOLUTION
In most cases, databinding code for the SelectionList should only execute the first time that a page is requested. Additional postbacks continue to populate the control with the ViewState information that is stored in the session state on the Web server. You can use the IsPostBack property of the Page class to prevent the databinding code from executing upon postback.

The following code samples illustrate the use of the IsPostBack property:

Microsoft Visual Basic .NET Sample
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) If Not Page.IsPostBack Then ' Place the Databinding code in here. End If End Sub

Microsoft Visual C# .NET Sample
private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) {   // Place the Databinding code in here. } }



STATUS
This behavior is by design.

Additional query words: data bind binding

Keywords: kbdatabinding kbprb kbpending kbservercontrols KB313673

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.