Solved

Automatic Values in ASP.NET Formview Insert Item Template

Posted on 2009-04-11
5
862 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 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now