Fetch (Retrieve) data from Database based on Session value using C# and VB.Net in ASP.Net

Last Reply 8 months ago By pandeyism

Posted 8 months ago

In this code no error accured.

I am fetching database table value the data are not display in label

            try
            {
                con.Open();
                cmd11.Connection = con;
                cmd11.CommandType = CommandType.Text;

                cmd11.CommandText = "select Name,State,District,Address,Contact,Email from Company_Data199 where Email=' " + Session["Com"].ToString() + "'";
                SqlDataReader dr = cmd11.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        nam.Text = dr["Name"].ToString();
                        sta.Text = dr["State"].ToString();
                        di.Text = dr["District"].ToString();
                        Add.Text = dr["Address"].ToString();
                        co.Text = dr["Contact"].ToString();
                        ema.Text = dr["Email"].ToString();

                    }

                }
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
            finally
            {
                con.Close();
            }
Posted 8 months ago

Hi VinitaAkha,

Refer below sample.

HTML

CS.aspx

Id :
<asp:TextBox runat="server" ID="txtId" />
<br />
<asp:Button Text="Redirect" runat="server" OnClick="Redirect" />

Dafault.aspx

Name :
<asp:Label ID="lblName" runat="server" />
<br />
Country :
<asp:Label ID="lblCountry" runat="server" />

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#

CS.aspx.cs

protected void Redirect(object sender, EventArgs e)
{
    Session["Id"] = txtId.Text;
    Response.Redirect("Default.aspx");
}

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT Name, Country FROM CustomerTest WHERE CustomerId = @Id", con))
            {
                cmd.Parameters.AddWithValue("@Id", Session["Id"]);
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    lblName.Text = dr["Name"].ToString();
                    lblCountry.Text = dr["Country"].ToString();
                }
                con.Close();
            }
        }
    }
}

VB.Net

VB.aspx.vb

Protected Sub Redirect(ByVal sender As Object, ByVal e As EventArgs)
    Session("Id") = txtId.Text
    Response.Redirect("DefaultVB.aspx")
End Sub

DafaultVB.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(constr)
            Using cmd As SqlCommand = New SqlCommand("SELECT Name, Country FROM CustomerTest WHERE CustomerId = @Id", con)
                cmd.Parameters.AddWithValue("@Id", Session("Id"))
                con.Open()
                Dim dr As SqlDataReader = cmd.ExecuteReader()
                If dr.Read() Then
                    lblName.Text = dr("Name").ToString()
                    lblCountry.Text = dr("Country").ToString()
                End If
                con.Close()
            End Using
        End Using
    End If
End Sub

Screenshot