Master/Detail Gridview Formview - null error on edit

I have a gridview (gvProjects) and a formview(fvProjects) for the same database table on my web page.  The gridview is used solely for users to view the records. The formview is used for both inserting new records and editing records. The formview is hidden until the user either clicks a button to insert a new record or clicks a link in the gridview to edit the record.

I have entered code in the ItemCreated event of the formview to assign the current date to a field in my table whenever a record is created.  

If I open the formview in Insert mode and either click the Insert button or click the Cancel button, when I try clicking my link in my gridview to open the formview in Edit Mode I get a "Object reference not set to an instance of an object."  when it tries to case the current date.

Not sure why this is doing it because there is a date value in the record.  


And for some reason when I click the Select link on the gridview to edit the record in the Formview it throws a null error form my ItemCreated code.  I don't understand why the ItemCreated event is even being called since I am not inserting but updating.  Also, I'm not sure why it would give me a null error anyway b/c there is data in these fields when the formview goes into Edti Mode.
I have included my formview code and also the ItemCreated code that is throwing the error.  

Any help would be greatly appreciated.
fvprojects_ItemCreated Event:

Protected Sub fvProjects_ItemCreated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fvProjects.ItemCreated
        'Dim StatusTextBox As TextBox = DirectCast(fvProjects.FindControl("StatusTextBox"), TextBox)
        Dim CreateDateTextBox As TextBox = DirectCast(fvProjects.FindControl("CreateDateTextBox"), TextBox)
        Dim ModifiedDateTextBox As TextBox = DirectCast(fvProjects.FindControl("ModifiedDateTextBox"), TextBox)
        Dim ModifiedByTextbox As TextBox = DirectCast(fvProjects.FindControl("ModifiedByTextBox"), TextBox)

        'StatusTextBox.Text = 1
        CreateDateTextBox.Text = System.DateTime.Now.Date
        ModifiedDateTextBox.Text = System.DateTime.Now.Date
        ModifiedByTextbox.Text = Page.User.Identity.Name.Split("\"c)(1).ToString()


    End Sub

Open in new window

imstac73Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
CodeCruiserCommented:
>Not sure why this is doing it because there is a date value in the record.  

The error is not about the data, its about the controls.

>Dim CreateDateTextBox As TextBox = DirectCast(fvProjects.FindControl("CreateDateTextBox"), TextBox)
The FindControl function above does not return any results hence the CreateDateTextBox is Nothing. When you try to use a nothing textbox, you get the error(nothing to do with date).


May be you need to use some other event such as ItemInserting or ItemInserted instead of ItemCreated

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.iteminserting.aspx
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
imstac73Author Commented:
That worked. Thanks so much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.