C#:  Get Max Date from SQL Server 2005 Database

Posted on 2007-10-03
Last Modified: 2013-12-17
Using C# I need to find the maximum date in a datetime field called RateThroughDate, add one day to it, using it to compare, for validation purposes, against a date entered into a Web form.  I am using a bLINQ-generated ObjectDataSource, but will use a SqlDataSource for my Web form.  Thanks.

Question by:micjones
    LVL 2

    Accepted Solution

    Select Top(1) RateThroughDate From Table Order By RateThroughDate ASC (or DESC)


    Author Comment

    Actually, I need help setting up the command module.  This is what I have used thus far:

          protected void Page_Load(object sender, EventArgs e)
              SqlDataSource sqlDataSource = new SqlDataSource();
            sqlDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["GPCSConnectionString"].ToString();
            sqlDataSource.SelectCommand = "SELECT MAX([DEORateSchedule.RateThroughDate]) As 'highVal' FROM [DEORateSchedule]";

            System.Data.DataView sqlDataView = (DataView)sqlDataSource.Select(DataSourceSelectArguments.Empty);
            int result = (int)sqlDataView[0][0];

    It bombs out at System.Data.DataView sqlDataView = ... statement.  Thanks.


    Author Comment

    Ooops!  And how do I set result to datetime type?

    Author Comment

    Well, whadda you know?  I looked a litle closer at the error message and I removed "DEORateSchedule." from the column name.  Now I have an InvalidCastException error stating that the specified cast is not valid.

    Author Comment

    d'OH!!!!  This was too easy for me to have surrendered so early!  The following substitution for the "int result =" statement worked:

    DateTime result = (DateTime)sqlDataView[0][0];

    I had forgotten that C# is case sensitive.  I had been trying "datetime" instead.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now