• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3056
  • Last Modified:

C#: Get Max Date from SQL Server 2005 Database

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.

mj
0
micjones
Asked:
micjones
  • 4
1 Solution
 
BakuRetsu_XCommented:
Select Top(1) RateThroughDate From Table Order By RateThroughDate ASC (or DESC)

0
 
micjonesAuthor Commented:
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.

0
 
micjonesAuthor Commented:
Ooops!  And how do I set result to datetime type?
0
 
micjonesAuthor Commented:
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.
0
 
micjonesAuthor Commented:
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now