Click to See Complete Forum and Search --> : DateTime parameter in a DataSource


renatois
09-30-2007, 08:01 PM
Hello everyone, I'm having a problem passing a DataTime parameter to a SelectCommand instruction in a DataSource.

I'm using access database and the code is:
<asp:SqlDataSource ID="sdsArtigos" runat="server" ConnectionString="<%$ ConnectionStrings:conArtigos %>"
ProviderName="<%$ ConnectionStrings:conArtigos.ProviderName %>" SelectCommand="SELECT * FROM [Artigos] WHERE ([Status] = ? AND DataInicial <= ?)">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="Status" Type="Int32" />
<asp:Parameter DefaultValue="29/09/2007" Name="Status" Type="DateTime" />
</SelectParameters>
</asp:SqlDataSource>

As you can see the DateTime is hard-coded to work fine but I need it to be dinamic from the SelectCommand, I have tried to replace "29/09/2007" for DateTime.Today but it doesn´t work.

The error I get when I replace 29/09/2007 for DateTime.Today is:
The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.

Any idea how to solve that problem?

Thanks

lmf232s
10-01-2007, 08:27 AM
DefatuValue="09/29/2007"

MM/DD/YYYY you had it DD/MM/YYYY

renatois
10-01-2007, 08:47 AM
the format is ok, Brazil

lmf232s
10-01-2007, 12:43 PM
Ok the format 29/09/2007 actually works when you hard code it but does not work when you use DateTime.Today.

Try
Date.Today()

This may not work because it will be in the wrong fromat (MM/DD/YYYY)

If thats the case youll need to create a function (if you dont already) and format the date so it displays in the correct format of DD/MM/YYYY.

renatois
10-02-2007, 08:30 AM
SOLUTION FOUND
ASPX
1 - Create an event OnSelecting, Add all the parameters but not the DefaultValue property of the two DateTime parameters.
1 - Crie um evento OnSelecting, adicione todos os parâmetros mas não a propriedade DefaultValue dos dois parâmetros DateTime

<asp:SqlDataSource ID="sdsArtigos" runat="server" ConnectionString="<%$ ConnectionStrings:conArtigos %>"
ProviderName="<%$ ConnectionStrings:conArtigos.ProviderName %>" SelectCommand="SELECT * FROM [Artigos] WHERE (([Status] = ?) AND ([DataInicial] <= ?) AND ([DataFinal] >= ?))" OnSelecting="sdsArtigos_Selecting">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="Status" Type="Int32" />
<asp:Parameter Name="DataInicial" Type="DateTime" />
<asp:Parameter Name="DataFinal" Type="DateTime" />
</SelectParameters>
</asp:SqlDataSource>

ASPX.CS
1 - In the class pass the DefaultValue property to the DateTime parameters
1 - Na Classe passe a propriedade DefaultValue para o parâmetro DateTime

protected void sdsArtigos_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
//O exemplo abaixo também funciona chamando por índice ao invés de nome (the example bellow also works)
//e.Command.Parameters[1].Value = DateTime.Today;
//e.Command.Parameters[2].Value = DateTime.Today;
e.Command.Parameters["DataInicial"].Value = DateTime.Today;
e.Command.Parameters["DataFinal"].Value = DateTime.Today;
}

Ty all