Solved

Automatic Values in ASP.NET Formview Insert Item Template

Posted on 2009-04-11
5
873 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
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 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