-1

I've seen similar questions through extensive 'google-ing' but none have been able to sort this issue out, the error I'm getting from VS13 is below:

An exception of type 'System.NullReferenceException' occurred in my.dll but was not handled in user code. Additional information: Object reference not set to an instance of an object.

Simply put I want a default date value to populate within the calendar textbox so the user doesn't have to choose a date, or if they forget etc it'll put the system time in.

I've tried converting toString etc and I get the same error.

Any help would be gratefully received.

Thanks in advance.

Code behind:

Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        Dim SrtDateTxt As New TextBox
        SrtDateTxt = ListView1.FindControl("StartDateTextBox")
        SrtDateTxt.Text = DateTime.Now
    End If
End Sub

And the aspx that the above code behind relates to:

<asp:TextBox ID="StartDateTextBox" runat="server" Text='<%# Bind("StartDate") %>' Width="150px"/>
<act:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="StartDateTextBox" Format="yyyy-MM-dd"></act:CalendarExtender>
Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
SelrekJohn
  • 476
  • 2
  • 6
  • 21
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bjørn-Roger Kringsjå Jul 30 '14 at 11:06
  • 2
    I strongly recommend to set `Option Strict` to "On" in your project's settings. – Tim Schmelter Jul 30 '14 at 11:09
  • @Bjørn-RogerKringsjå - I'm looking at this now, unsure though, the reference would be null initially but it's on page load that I want to set the datetime. Tim - that could have a large array of issues to the rest of the project, this is just adding functionality to an existing website. – SelrekJohn Jul 30 '14 at 11:12
  • @AdrianoRepetti - I thought I did to reference the control otherwise how else would it know where to adjust the date? – SelrekJohn Jul 30 '14 at 11:15

1 Answers1

2

First i want to suggest to change Option Strict to "On" in your project's settings. That won't you prevent from compiler errors but from nasty runtime errors. Fixing the compiler errors will teach you much about the types in the .NET framework.

Then for example this will not compile anymore:

SrtDateTxt.Text = DateTime.Now

You need to assign a string instead of a Date:

SrtDateTxt.Text = DateTime.Now.ToString() ' or ToShortDateString()

Next, this will not prevent you from a NullRefernceException:

Dim SrtDateTxt As New TextBox

If you assign another value to this variable one line afterwards:

SrtDateTxt = ListView1.FindControl("StartDateTextBox")

So i assume that SrtDateTxt is Nothing because there is no control with that ID in the ListView but in one of it's ListViewItems(which are different NamingContainers).

Therefore you get the exception at the next line when you try to access a property

SrtDateTxt.Text = DateTime.Now ' at .Text

A ListView is meant to be used for multiple items. So you need to use the correct ListViewItem.

For Each item As ListViewItem In ListView1.Items
    Dim StartDateTextBox = DirectCast(item.FindControl("StartDateTextBox"), TextBox)
    '  ... '
Next

This loops all items, i don't know if that is what you want to do.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939