how to display database value in label in asp.net c#

In this article, we learn about the how to fetch the database value in label, Normally we need to this for account page means after the login, we will show the username in the label or other field show in label.
Database:-





Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="labelvalue.aspx.cs" Inherits="labelvalue" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   
    </div>
    </form>
</body>
</html>


Code behind(C#);-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class labelvalue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {



        SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678");
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from login where name ='Neeraj Srivastava'", con);
         
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            Label1.Text = dr["name"].ToString();

        }
        con.Close();


    }


}

Out-Put:-


how to display database value in label in asp.net c# how to display database value in label in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 7:59:00 PM Rating: 5

No comments:

Powered by Blogger.