Hi satabeach,
Refer below sample.
Database
For this sample I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:Label ID="Label1" runat="server" />
<br />
<asp:Label ID="Label2" runat="server" />
<br />
<asp:Label ID="Label3" runat="server" />
<br />
<asp:Label ID="Label4" runat="server" />
<br />
<asp:Label ID="Label5" runat="server" />
<br />
<asp:Label ID="Label6" runat="server" />
<br />
<asp:Label ID="Label7" runat="server" />
<br />
<asp:Label ID="Label8" runat="server" />
<br />
<asp:Label ID="Label9" runat="server" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT FirstName + ' ' + LastName FROM Employees ORDER BY NEWID()", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
Label1.Text = dt.Rows[0][0].ToString();
Label2.Text = dt.Rows[1][0].ToString();
Label3.Text = dt.Rows[2][0].ToString();
Label4.Text = dt.Rows[3][0].ToString();
Label5.Text = dt.Rows[4][0].ToString();
Label6.Text = dt.Rows[5][0].ToString();
Label7.Text = dt.Rows[6][0].ToString();
Label8.Text = dt.Rows[7][0].ToString();
Label9.Text = dt.Rows[8][0].ToString();
}
}
}
}
}
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("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT FirstName + ' ' + LastName FROM Employees ORDER BY NEWID()", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Label1.Text = dt.Rows(0)(0).ToString()
Label2.Text = dt.Rows(1)(0).ToString()
Label3.Text = dt.Rows(2)(0).ToString()
Label4.Text = dt.Rows(3)(0).ToString()
Label5.Text = dt.Rows(4)(0).ToString()
Label6.Text = dt.Rows(5)(0).ToString()
Label7.Text = dt.Rows(6)(0).ToString()
Label8.Text = dt.Rows(7)(0).ToString()
Label9.Text = dt.Rows(8)(0).ToString()
End Using
End Using
End Using
End If
End Sub
Screenshot