Solved

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

Posted on 2011-02-14
10
3,423 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

820 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