Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-14
10
Medium Priority
?
3,834 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
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

670 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