?
Solved

Automatic Values in ASP.NET Formview Insert Item Template

Posted on 2009-04-11
5
Medium Priority
?
878 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 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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

752 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