Hi,
In my opinion, dates + Sql has always been a complicated couple. (You'd better not have any dates with Sql!) :-)
I have been working with the filterexpression feature of Asp.Net 2.0, and it works really well until I throw in a date. My object here is to only display records within the user-specified dates.
Here's my code (Asp.Net 2.0, VWD Express,
VB):
<asp:SqlDataSource ID="VerSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:mydbConnectionString %>"
SelectCommand="SELECT person.verid, person.fid, person.serie, person.vnr, person.datum, person.vtext, person.sign, person.med, product.vpostid, product.verid AS Expr1, product.kpnr, product.drt, product.krt, product.ptext, product.kst, product.projekt, kpstandard.kpid, kpstandard.kpnr AS Expr2, kpstandard.kpnamn, kpstandard.kptyp FROM person INNER JOIN product ON person.verid = product.verid INNER JOIN kpstandard ON product.kpnr = kpstandard.kpnr WHERE (person.fid = @fid) ORDER BY product.kpnr, person.datum, person.serie, person.vnr"
EnableCaching="True" CacheDuration="3600"
FilterExpression="datum >= '{0}' AND datum <= '{1}' ">
<SelectParameters>
<asp:SessionParameter DefaultValue="1" Name="fid" SessionField="fid" Type="Int32" />
</SelectParameters>
<FilterParameters>
<asp:ControlParameter Name="fromdatum" ControlID="FromdatumHiddenField" PropertyName="Value" />
<asp:ControlParameter Name="todatum" ControlID="TodatumHiddenField" PropertyName="Value" />
</FilterParameters>
</asp:SqlDataSource>
The date in the hiddenfield (which has a date from a textbox, simply formatted in this manner) is formatted like this "2006-06-28", and is set to date with CDate. I have tried both just sending the date as 2006-06-28 and as '2006-06-28' (and I have checked them via response.write). Whatever I do, the filterexpression gets totally omitted - the page works but the date filtering and other filtering as well is disregarded. When I do a response.write of the db's dates they are in the same format: 2006-06-28 00:00 (I've set sv-se - Swedish - in the web.config file). Note that if I only have other filtereing and no dates, it works just fine. Perhaps it has to do with the single hyphens, but I can't figure out how to do it.
Surprisingly, I can't find any posts on this site regarding this matter, using the built-in capabilities of Asp.Net. It's really neat, but I need to get the date issue fixed. Any help is greatly appreciated!
Thanks in advance,
Pettrer
BTW, I ususally write my Select statements myself, but this time I let VWD Express do it for me. It's a bunch of innner joins and much more, as can be seen above, whereas I'd only write SELECT person.name, product.shipped ... FROM person, product, kpstandard... WHERE person.pID = product.pID AND product.sID = kpstandard.sID AND ...
Which way is the best? Is VWD's way faster?
Thanks
P
Coding is indeed a nine-to-five job; nine pm to five am.