Solved

Automatic Values in ASP.NET Formview Insert Item Template

Posted on 2009-04-11
5
875 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 50 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 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