Introduction
In this article we will retrieve data from database & display it into a label. Another way we can say that here I’ll explain about,how we can bing label dynamically.
Implementation
Create new website in asp.net. then click at the website menu then select add new item a dailogbox appeared. then select add new form then press ok button
HTML CODE
Place a label at .aspx page from toolbox named lbl_msg.
<asp:Label ID="lbl_msg" runat="server"></asp:Label>
C# CODE
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
// string id = Request.QueryString["value"];
try
{
con.Open();
if (Page.IsPostBack == false)
{
heading();
}
}
catch
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
private void heading()
{
try
{
SqlDataAdapter adp10 = new SqlDataAdapter("select Top(1)class from tbClasses order by id desc", con);
DataSet ds10 = new DataSet();
adp10.Fill(ds10);
adp10.Dispose();
// way to binding of a label in asp.net
lbl_msg.Text = Convert.ToString(ds10.Tables[0].Rows[0]["class"]);
}
// If there is no any value in the table then it will go to the catch
exception.
catch { }
}
SQL SCRIPT
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tbClasses]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tbClasses](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[class] [varchar](50) NOT NULL,
CONSTRAINT [PK_tbClasses_1] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
No comments:
Post a Comment