Hi vereato,
Check this example. Now please take its reference and correct your code.
HTML
<asp:Label ID="lblUsername" runat="server" />
<asp:Label ID="lbl_Gender" runat="server" />
<asp:Label ID="lbl_age" runat="server" />
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["Username"] = null;
Session["Gender"] = null;
Session["Age"] = null;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT UserName,Gender,Age FROM Employees WHERE EmployeeID = 1";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
lblUsername.Text = sdr["UserName"].ToString();
lbl_Gender.Text = sdr["Gender"].ToString();
lbl_age.Text = sdr["Age"].ToString();
Session["Username"] = sdr["UserName"].ToString();
Session["Gender"] = sdr["Gender"].ToString();
Session["Age"] = sdr["Age"].ToString();
}
con.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Session("Username") = Nothing
Session("Gender") = Nothing
Session("Age") = Nothing
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT UserName,Gender,Age FROM Employees WHERE EmployeeID = 1"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query, con)
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
lblUsername.Text = sdr("UserName").ToString()
lbl_Gender.Text = sdr("Gender").ToString()
lbl_age.Text = sdr("Age").ToString()
Session("Username") = sdr("UserName").ToString()
Session("Gender") = sdr("Gender").ToString()
Session("Age") = sdr("Age").ToString()
End If
con.Close()
End Using
End If
End Sub