Hi indradeo,
Below is the sample please refer it.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
ID:<asp:Label runat="server" ID="lblId" />
<br />
Name:<asp:Label runat="server" ID="lblName" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 1 EmployeeID,FirstName+' '+LastName Name FROM EMployees ORDER BY EmployeeID DESC", con))
{
cmd.CommandType = CommandType.Text;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
{
if (dr.Read())
{
this.lblId.Text = dr[0].ToString();
this.lblName.Text = dr[1].ToString();
}
}
con.Close();
}
}
}
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 1 EmployeeID,FirstName+' '+LastName Name FROM EMployees ORDER BY EmployeeID DESC", con)
cmd.CommandType = CommandType.Text
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
If True Then
If dr.Read() Then
Me.lblId.Text = dr(0).ToString()
Me.lblName.Text = dr(1).ToString()
End If
End If
con.Close()
End Using
End Using
End If
End Sub
Screenshot
