[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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

1 Solution
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.

ovcbsAuthor Commented:
I fount that I needed to set these values on a per-row basis.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now