Solved

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

Posted on 2011-02-14
10
3,551 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
[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
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET data base connection 35 95
PHP $_POST vs asp request 4 57
Web page design problem 3 41
ASP.NET sqlds databinding error cascading dropdownlists ? 1 16
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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