how to display database value in label in 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.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

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");
        SqlCommand cmd = new SqlCommand("Select * from login where name ='Neeraj Srivastava'", con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
            Label1.Text = dr["name"].ToString();





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

No comments:

Powered by Blogger.