Wednesday, 8 August 2012

How To find last updated date in datatable values in c#.net

I've Answered : http://forums.asp.net/t/1830090.aspx/1?how+to+find+last+updated+date+in+datatable+values+in+c+net

Question :

Hi friends,
    I have requirement like i need to find last updated date from DataView . I have data like Project ID,UpdatedDate If same ProjectID i have 4 records,with different updated dates.So i need to find last updated value and compare other date. How can i get this last updated table using Dataview or data table.Can you share is there any simple method to do this..
Answer:
Its Example
I have Column "ReceiptDate"
ReceiptDate
4/21/2012
4/21/2012
4/21/2012
NULL
4/21/2012
4/21/2012
NULL
4/21/2012
NULL
4/21/2012
NULL
5/23/2012
4/23/2012
NULL
5/2/2012
5/27/2012
NULL
5/27/2012
6/9/2012
NULL
6/9/2012
NULL
6/9/2012
So, I know 6/9/2012 Is Highest Date So, I can use This Query
SELECT MAX(ReceiptDate) FROM MyTable
Result : 6/9/2012
SqlConnection Conn = new SqlConnection(@"Data Source=(local);Initial Catalog=YOUR_DATABASE;Integrated Security=True");
        SqlCommand Comm1 = new SqlCommand("SELECT MAX(ReceiptDate) FROM YourTable", Conn);
        Conn.Open();
        SqlDataReader DR1 = Comm1.ExecuteReader();
        if (DR1.Read())
        {
            int Val = Convert.ToInt32(DR1.GetValue(0).ToString());
            ResultLabel.Text = Convert.ToString(Val);
        }
        Conn.Close();
Dont Foget to Import namespace
using System.Data.SqlClient;
Good Luck`



0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More