Dropdown NullReferenceException error at Page_Load

M

msch-prv

I get a runtime error 'NullReferenceException: Object reference not set
to an instance of an object' when I attempt to execute sub UpdGrd() in
the Page_Load event. UpdGrd() basically creates a grid (works OK) and
attempts to read the selected item of dropdown box 'ddnEmp_Emp'. The
idea is to add this value to the grid's title. (Default value should be
'All'.)

Event ddnEmp_Emp_SelectedIndexChanged() works OK and returns the
correct value.

Question: Why can't I read the selectedItem.value at load-up time? The
value should not be null. Some posts I read suggest to instantiate the
dropdown. How do I do this?

Thanks for any hints, Mark


Code snippet:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not Page.IsPostBack Then
ddnEmp_Emp.Text = "0" 'Select All
End If
UpdGrd()
End Sub

Sub UpdGrd()
Dim strEmp As String = ddnEmp_Emp.SelectedValue 'Compiler accepts
this!
Dim strEmpName As String = "" 'Input value
....
If Not IsNothing(ddnEmp_Emp.SelectedItem.ToString) Then strEmpName =
ddnEmp_Emp.SelectedItem.ToString <--- Error!
Response.Write("ddnEmp_Emp: " & strEmpName)

....
End Sub
 
G

Guest

I have recently come up against this exact same problem, in my case it seems
to have been caused by the fact that the DropDownList had not yet been data
bound in the page load event. I put the exact same code in the DataBound
event handler instead of the PageLoad event handler and it works perfectly.
 
M

msch-prv

Thanks for the reply, clicon. That might get me starting.

Are you talking about the dropdown databound event ie Protected Sub
ddnEmp_Emp_DataBound(..) or the page databind()?

What kind of code do you put in there? Will this do it:

ddnEmp_Emp.SelectedIndex = 0
or
ddnEmp_Emp.Text = "0" 'Select All

TIA, Mark
 
G

Guest

Yes i do mean Protected Sub
ddnEmp_Emp_DataBound(..) if that is the event handler for your drop down
list. I don't really speak VB im a C# man, but for me i just put exactly the
same code in my DataBound event handler that was in my PageLoad event handler
and it worked from there. You milage may vary.
 
M

msch-prv

Found it!

The problem was that the dropdown was not databound. After some
tweaking, I came up with the following code which solves the problem:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not Page.IsPostBack Then
ddnEmp_Emp.SelectedIndex = 0
ddnEmp_Emp.DataBind()
End If
UpdGrd()
End Sub

Sub UpdGrd()
.....
grdEmp.Caption = "Records: " & objDataTable.Rows.Count.ToString
& " - " & ddnEmp_Emp.SelectedItem.ToString
....
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top