Where do I set the value for a hidden textbox before a record insert?

I have a page with a formview on it. The formview is only used for inserts. I have 2 hidden textboxes which I use to set a username and datetime before the record insert. I cannot get these to work without an error. I just need to get these values into the record being written so if this is the wrong approach please advise. The error I get is:
 Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 69:         t = c
Line 70:         ' This line is for testing
Line 71:         t.Text = "This is a test"
Line 72:         t.Text = ugw.Membership.GetCurrentUserName(Page)
Line 73:         c = frmEditInsert.FindControl("fldDateModifiedTextBox")

 Source File:  c:\websites\MyWebsite\\Myfile.aspx.vb    Line:  71

I added the test line to verify that the error was not from the function ugw.Membership.GetCurrentUserName(Page) which returns a string.

If the solution is to set the Insert parameters values that would work for me also.

Please advise which is best, to set bound textbox values or to set insert parameters, and which event to set them in.
Thanks in advance.
Protected Sub frmEditInsert_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles frmEditInsert.DataBound
        Dim c As System.Web.UI.Control
        Dim t As TextBox
        c = frmEditInsert.FindControl("fldModifiedByTextBox")
        t = c
        ' This line is for testing
        t.Text = "This is a test"
        t.Text = ugw.Membership.GetCurrentUserName(Page)
        c = frmEditInsert.FindControl("fldDateModifiedTextBox")
        t = c
        t.Text = DateTime.Now.ToLocalTime
    End Sub

Open in new window

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.

Hi There

I spott several things that could be wrong with the code. I can't tell the exact context of the code or why you need the hidden text boxes.

The first thing you shoud check is that after line 4. c should not be null, just set a break point and inspect c.

Secondly, c is of type control and t is of type textbox. You don't really need 2 objects but rather just cast to textbox when you find the control.

I do however suspect that c is null when you find the control and therefor t will also be null.

Do some debugging and step thru the code line for line and inspect each object after a line executed, and see if the objects are what you expect them to be.

Hope this helps.


Experts Exchange Solution brought to you by

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
ovcbsAuthor Commented:
I fount that I needed to set these values on a per-row basis.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.