473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Dropdown NullReferenceException error at Page_Load

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

Jul 24 '06 #1
4 2738
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.

"ms******@bluewin.ch" wrote:
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

Jul 24 '06 #2
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

Jul 24 '06 #3
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

Jul 24 '06 #4
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.
"ms******@bluewin.ch" wrote:
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

Jul 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: mike p. | last post by:
I have a docbook xml file, and am using standard docbook 1.61.3 xsl stylesheets to do xhtml transform. Transform works fine when using MSXML. When I try to do the following using asp.net 1.1: ...
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
17
by: Rodusa | last post by:
I am getting this exception error which is driving me nuts. System.NullReferenceException - Object reference not set to an instance of an object If I comment this line, I don't get any errors:...
4
by: Brian Conway | last post by:
I need some help on binding a datareader to a dropdown box. I have included the code for the dropdown below. It builds with no errors, but returns no results. Any help would be appreciated. ...
3
by: Patrick.O.Ige | last post by:
I'm loading an Array below but getting the error "Object reference not set to an instance saying 'ItemNumber = CType(Args.Item.FindControl("ItemNumber"), TextBox).Text' is the error line. I DON'T...
5
by: Patrick.O.Ige | last post by:
What could cause the error:- System.NullReferenceException: Object reference not set to an instance of an object. Any ideas?
5
by: david | last post by:
I have a question in the following. Any one could give me a help? Thanks David The dataset is declared and created at Class level. My source code is here: Private Sub...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.