Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Automatic Values in ASP.NET Formview Insert Item Template

Posted on 2009-04-11
5
Medium Priority
?
891 Views
Last Modified: 2013-11-27
This has got to be the easiest question in the world but I can't find a clear solution:

There are three fields in a formview: fldID, fldName, fldDatetime

All I want to do automatically insert the current date/time into the fldDatetime field which is hidden. I've tried a ton of different things but nothing works.

- What date type should I use for fldDatetime? there's datetime, smalldate, timestamp...etc.
- Which event is correct? I thought formview1.databinding was the most logical but one source said to do it on databound
- What's the simplest and most effective way to do this? (in VB if possible)

My current code (which is painfully wrong) is below.

Thanks,
Mike


Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
        Dim txt As TextBox
        txt = FormView1.FindControl("cmtDateTextBox")
        txt.Text = Now()
    End Sub

Open in new window

0
Comment
Question by:iammrkcohen
  • 3
  • 2
5 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 200 total points
ID: 24128856
Mike,

There are significantly easier questions than that.  

I assume that you are using SQL Server (not mentioned directly), and I usually use SmallDateTime with dates, since I don't need the extra information that DateTime provides, and Timestamp is not really a date/time field type that is used.

How is that code "painfully wrong"?
0
 

Author Comment

by:iammrkcohen
ID: 24128911
Thanks for the response!

Yes, I am using SQL server. And currently the field is set to smalldatetime. The code above is yielding the following error:

Cannot insert an explicit value into a timestamp column. Use INSERT with a column list to exclude the timestamp column, or insert a DEFAULT into the timestamp column.

Even if I convert that field to varchar and try to insert a string instead (e.g. txt.Text = "Cat" ) I get the following error:

Implicit conversion from data type sql_variant to varchar is not allowed. Use the CONVERT function to run this query.]

My experience is with access databases so none of these problems seemed to exist for me before. Any ideas?

Thanks,
Mike
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24129420
That error would suggest that you have a Timestamp field, and not a SmallDateTime field...

Are you bound to an SqlDataSource?
0
 

Author Comment

by:iammrkcohen
ID: 24130656
Good point - I guess I copied the error from when I had it as timestamp by accident. This was what I got with smalldatetime:

Now I feel really stupid because it's working. I reset the sqldatasource, although didn't change anything, and refreshed the table itself to ensure it was the correct data type. Now it's working fine.

Thanks for your help!
0
 

Author Comment

by:iammrkcohen
ID: 24413655
Also, just as FYI - in C# this is how I  did this:



        TextBox txt = (TextBox)Page.FindControl("ManagerIDTextBox");
        MembershipUser curUser;
        curUser = Membership.GetUser();
        txt.Text = curUser.ToString();

Open in new window

0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question