Hi roon3y,
Create function which will return DataTable from database using the sql query or procedure and then you can bind the datatable to label as in the below sample.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dtName = GetDataTable("SELECT TOP 1 CustomerId,Name FROM Customers", CommandType.Text);
DataTable dtCountry = GetDataTable("SELECT TOP 1 CustomerId,Country FROM Customers", CommandType.Text);
lblName.Text = dtName.Rows[0]["Name"].ToString();
lblCountry.Text = dtCountry.Rows[0]["Country"].ToString();
}
}
private DataTable GetDataTable(string query, CommandType commandType)
{
string str = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand(query, con);
cmd.CommandType = commandType;
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
con.Close();
return dt;
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dtName As DataTable = GetDataTable("SELECT TOP 1 CustomerId,Name FROM Customers", CommandType.Text)
Dim dtCountry As DataTable = GetDataTable("SELECT TOP 1 CustomerId,Country FROM Customers", CommandType.Text)
lblName.Text = dtName.Rows(0)("Name").ToString()
lblCountry.Text = dtCountry.Rows(0)("Country").ToString()
End If
End Sub
Private Function GetDataTable(query As String, commandType As CommandType) As DataTable
Dim str As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim dt As New DataTable()
Dim con As New SqlConnection(str)
Dim cmd As New SqlCommand(query, con)
cmd.CommandType = commandType
con.Open()
Dim sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
con.Close()
Return dt
End Function
Output
Mudassar Khan
India