How to hide an asp:textfield and exclude it from the form layout, or pass a value without using the asp:textfield?

Hello Experts,

I have an application with a form that captures some information that is stored in a MS SQL database.  I would like to know who creates each record.  I currently have an asp:textbox that captures the current user with:  
creator_txt.Text = System.Web.HttpContext.Current.User.Identity.Name;

Open in new window

Then I use that text field in a SqlCommand and insert it into a MS SQL Server table using:
SqlCommand new_record_cmd = new SqlCommand("Insert Into tbl(record_nm, creator) Values @record_nm, @creator)))", lcms_cnxn);
                new_record_cmd.Parameters.AddWithValue("@record_nm", record_nm_txt.Text);
                new_record_cmd.Parameters.AddWithValue("@creator", creator_txt.Text);


Open in new window

It all works just fine, but I would like this field to be hidden - it doesn't need to be visible in the form.  
Do I need this text field at all?  
If yes, how do I hide the field from the layout and still pass the value of creator_txt to the database?
If not, how do I pass the current user to @creator without using the text field?

I hope this makes sense.  Please ask questions if it's not clear.

I am using Visual Studio 2012, SQL Server Management Studio 2012, and c#.

Who is Participating?
I often use Labels and set them to Visible = False. Then you can still use the .Text property.

If you use the HiddenField, you reference the .Value property.
Use asp:HiddenField:
<asp:HiddenField id="creator" runat="server" />

Open in new window

It will create a input type="hidden" element on the web page
Mandeep SinghDatabase AdministratorCommented:
Hi you can use Hiddenfield or Label to do this.
ferguson_jeraldAuthor Commented:
Thanks.  But what do I do to my code behind to get it to work properly?  I currently have:

creator_txt.Text = System.Web.HttpContext.Current.User.Identity.Name;

Open in new window

What do I replace the
with?  When I remove it I get
Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.HiddenField
ferguson_jeraldAuthor Commented:
Thanks - it worked as expected.
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.