Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-14
10
Medium Priority
?
3,971 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

885 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