Setting a DateTime as the DefaultValue in SqlDataSource asp:parameter

Sometimes you lose way too much time on the stupidest little things. Maybe this post will save someone else some aggravation.

Using *declarative* syntax, I wanted to create a default datetime value for a SqlDataSource insert parameter. I tried all sorts of date-like values but kept getting errors.

I posted my issue to the microsoft.public.dotnet.framework.aspnet newsgroup and got an answer from Microsoft's Walter Wang to use "2007/12/24":

<insertparameters>
 <asp:parameter Name="Title" Type="String"/>
 <asp:parameter Name="Description" Type="String" />
 <asp:Parameter Name="DateAdded" Type="DateTime" DefaultValue="2007/12/24" />
</insertparameters>

That worked.

I'm still working on how to make the DefaultValue the current date using VB declarative syntax.

 

8 Comments

  • > I'm still working on how to make the DefaultValue the current date using VB declarative syntax.

    Is that a good idea? It will be evaluated when the page is compiled, which may be earlier than you want.

    Depending on your app you may be better to have a NULL default value, and alter your query to replace this by the current date - ISNULL(@DateAdded, GETDATE()) or similar.

  • Another option is to provide the parameters at sqldatasource inserting event.where u can place any type of default value in it.

  • Or, you can write your own ExpressionBuilder, so you can write DefaultValue=""

  • Jon, Sumit, Ruben, and Joel:
    Thanks very much to all for your input. I wanted to avoid digging into code or messing in the web.config file to create an expression. What I'm looking for is just:
    DefaultValue="&lt;%= DateTime.Now.ToString() %&gt;"
    However, Walter Wang of Microsoft tells me that it can't be done that way:
    "The InsertParameters of SqlDataSource is persisted as "PersistenceMode(PersistenceMode.InnerProperty)", therefore I don't think &nbsp;we're able to use an expression such as this to get the current datetime declaratively."
    I don't understand whether this is "by design", an oversight, or a bug. Anyway, the simple way I was searching for isn't available. Pity.
    Ken

  • use this in the page load
    SqlDataSource1.SelectParameters("date_t").DefaultValue = Date.Today
    asuming date_t is your parameter name

  • Briyad,

    The problem statement clearly indicates "Using *declarative* syntax". There's no problem doing this in code.

    Thanks,

    Ken

  • So, if you're using the code behind page the easiest way to do this is to handle the update event of the DataSource Object and then say e.InputParameters("ParameterID") = Now()

    Or better yet, have the database handle the setting the date to the current time.

  • I'm using the "Inserting" event of the detailsview or gridview to manipulate sql values, for example:

    ---------------------------------------------------
    Protected Sub detailsview1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles detailsview1.ItemInserting

    'if no date selected, insert current date value, no time.
    If e.Values(1) = "" Then e.Values(1) = Format(Date.Now, "d")

    End Sub
    ----------------------------------------------------

    Where e.value(n), the n represent the insert field, starting with 0 for the first one, 1 for the next and so on.

Comments have been disabled for this content.