Solved

ASP.Net putting the current date as a defaultvalue of a parameter

Posted on 2011-02-14
10
3,223 Views
Last Modified: 2012-05-11
I have a formview on a page and when I do an insert or update of a record I need to put the current date into the database. Below is not working

<asp:Parameter Name="Date" Type="DateTime" DefaultValue='<%=Now()%>' />
<asp:Parameter Name="Date" Type="DateTime" DefaultValue="<%=Now()%>" />

what is the replacement for Now() in an ASP page?

Thanks.
0
Comment
Question by:md0333
10 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34892841
0
 

Author Comment

by:md0333
ID: 34893074
still not working...

I pulled a value out of the database and copied it on to my asp page

2010-04-27 22:04:54.000

Then used this to try to come up with a compatible value to insert
'<%= Format(Date.Now(), "yyyy-MM-dd HH:hh:mm.FFF")%>'

which came out to
2011-02-14 17:05:14.185

looks like I nailed the formatting but still tells me it's not a valid date format when I try to do an update or an insert. The thing that is different is that the "Date" in Date.Now() is highlighted when it's on the page but when it's in the Parameter DefaultValue field it is not highlighted. Is it not valid to use this way?
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34893146
I have no way to test it (sorry) but try this and see what it does:

<asp:Parameter Name="Date" Type="DateTime" DefaultValue='<%=Date()%>' />

Open in new window

If this comes close, then we can add a call to Time() and then deal with reformatting if any.
0
 

Author Comment

by:md0333
ID: 34893171
changed to your code...
<asp:Parameter Name="Date" Type="DateTime" DefaultValue='<%=Date()%>'/>

got this error - same error

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: String was not recognized as a valid DateTime.
0
 
LVL 4

Expert Comment

by:zylver
ID: 34893246
dont worry of the control, set it up on the table, in the field just set with an sql function "getdate()"
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 34893256
Is this c#?

Then try this:
<asp:Parameter Name="Date" Type="DateTime" DefaultValue='<%=(DateTime)Date()%>'/>

Open in new window

0
 

Author Comment

by:md0333
ID: 34893264
No... this is ASP.NET. I'm using VB as codebehind but I'm trying to do this in a formview and not use codebehind. If I were doing this in VB then Now() would work fine.
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34893266

I can see no reason why your original code shouldn't have worked, except that the issue appears to be deeper than at first glance.
 
Now() is a valid function, but see this discussion:

http://weblogs.asp.net/kencox/archive/2007/06/19/setting-a-datetime-as-the-defaultvalue-in-sqldatasource-asp-parameter.aspx

There must be a way to do it but sorry I cannot be of more help.
0
 

Assisted Solution

by:md0333
md0333 earned 0 total points
ID: 34893319
I got it based on the article that @sjklein42: gave me.

Walter Wang of Microsoft says that it can't be done that way:

"The InsertParameters of SqlDataSource is persisted as "PersistenceMode(PersistenceMode.InnerProperty)", therefore I don't think  we're able to use an expression such as this to get the current datetime declaratively."


So in my Page.Load event I did this

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        sqlLenders.UpdateParameters("Date").DefaultValue = CType(Now(), DateTime)
        sqlLenders.InsertParameters("Date").DefaultValue = CType(Now(), DateTime)

    End Sub

Which worked....
0
 

Author Closing Comment

by:md0333
ID: 34932219
I gave sjklein42 the points but wanted to actually point out the solution in the thread.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

12 Experts available now in Live!

Get 1:1 Help Now