Display last updated date in Label using C# and VB.Net in ASP.Net

indradeo
 
on Oct 01, 2021 05:10 AM
Sample_819253.zip
632 Views

Dear Sir,

i want to display last updated date in a lable in ASP.Net C#

how to do this.

namespace TS
{
    public partial class Dashboard : System.Web.UI.Page
    {
        string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string str;
        SqlCommand com;
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(strConnString);
            con.Open();
            str = "select * from ts";
            com = new SqlCommand(str, con);
            SqlDataReader reader = com.ExecuteReader();

            reader.Read();
            labelname1.Text = reader["updated_on"].ToString();

            if (!this.IsPostBack)
            {
                BindGridView();
            }
            
        }

        private void BindGridView()
        {
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("select * from ts ORDER BY Id DESC ", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Close();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Oct 01, 2021 06:44 AM

Hi indradeo,

Refer below sample.

HTML

<strong>Last Updated On :<asp:Label ID="Label1" runat="server" Text="labelname1"></asp:Label>
</strong>
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#99ccff" HeaderStyle-ForeColor="black"
    runat="server" Font-Size="medium" AutoGenerateColumns="false" Style="table-layout: auto; margin-left: 0px;" HeaderStyle-Font-Size="17px" EmptyDataText="There Is No Records In Database!"
    CssClass="auto-style7" Width="1209px">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="SL.No" Visible="true" />
        <asp:BoundField DataField="Dept" HeaderText="Department" />
        <asp:BoundField DataField="wrk_nm" HeaderText="Work Name" />
        <asp:BoundField DataField="fts_no" HeaderText="FTS No." />
        <asp:BoundField DataField="prced_by_dept_date" HeaderText="Processed by Department on" />
        <asp:BoundField DataField="estmt_cst" HeaderText="Estimated Cost Without GST  " />
        <asp:BoundField DataField="prpsl_sts" HeaderText="Proposal Status " />
        <asp:BoundField DataField="prsnt_lctn" HeaderText="Pressent Location" />
        <asp:BoundField DataField="in_prss_sinc" HeaderText="In Process Since(Date)" DataFormatString="{0:dd-MM-yyy}" ItemStyle-Width="100px" />
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGridView();
    }
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT MAX(updated_on) FROM ts",con))
        {
            con.Open();
            Label1.Text = Convert.ToString(cmd.ExecuteScalar());
            con.Close();
        }
    }
}

private void BindGridView()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("select * from ts ORDER BY Id DESC "))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}

VB.Net

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Me.BindGridView()
        End If

        Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
            Using cmd As SqlCommand = New SqlCommand("SELECT MAX(updated_on) FROM ts", con)
                con.Open()
                Label1.Text = Convert.ToString(cmd.ExecuteScalar())
                con.Close()
            End Using
        End Using
    End Sub

    Private Sub BindGridView()
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(constr)
            Using cmd As SqlCommand = New SqlCommand("select * from ts ORDER BY Id DESC ")
                Using sda As SqlDataAdapter = New SqlDataAdapter()
                    cmd.Connection = con
                    sda.SelectCommand = cmd
                    Using dt As DataTable = New DataTable()
                        sda.Fill(dt)
                        GridView1.DataSource = dt
                        GridView1.DataBind()
                    End Using
                End Using
            End Using
        End Using
    End Sub

Screenshot