Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I m creating website in that i want to display data from sql table into label in asp.net.but i want that label should be added dynamically and bind data to label.
if the sql table contains 2 rows the label should be 2 added and bind data
Posted

try This one ....

C#
Label lblfirst = new Label();

Label lblsecond = new Label();

lblfirst.Text="bind data from DB";

lblsecond.Text="bind data from DB";
 
Share this answer
 
v2
Comments
Manohar Khillare 9-May-12 8:03am    
i want dynamically add label
Sandeep Mewara 9-May-12 8:27am    
This does exactly what you asked for. Now, if this is not what you need than you need to read and then post your questions using right words.

5! to answer.
Hi,

You can use following code to add as many labels as rows in your table dynamically:
C#
protected void Page_Load(object sender, EventArgs e)
        {
            AddDynamicLabels();
        }

        private void AddDynamicLabels()
        {
            string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            SqlConnection con = new SqlConnection(ConString);
            string CmdString = "Select EmployeeID, FirstName, HireDate FROM Employees ORDER BY Salary DESC";
            SqlCommand cmd = new SqlCommand(CmdString, con);
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Label lbl = new Label();
                lbl.Text = reader["FirstName"].ToString();
                lbl.BackColor = System.Drawing.Color.Orange;
                lbl.BorderStyle = BorderStyle.Solid;
                lbl.BorderWidth = 1;
                this.Controls.Add(lbl);
            }
            con.Close();
        }
 
Share this answer
 
Hello ...

Considering the label id as LblOne and SqlDataReader rd1 you can simply code this :

in the design page :
<asp:Label ID="LblOne" runat="server" Text="" />


in the code behind : VB.Net code

VB
LblOne.Text = String.Empty  ' to clear the label

While rd1.read()  
LblOne.Text += rd1("firldName").ToString 

End While
 
Share this answer
 
v2
Oooohh .. i've misunderstood your ask...

sorry



Cyclopic
 
Share this answer
 
Comments
Manohar Khillare 9-May-12 8:04am    
is there way to add label in table td tag in source file
cyclopic 9-May-12 8:46am    
look at solution 4

Cyclopic
cyclopic 9-May-12 8:51am    
The function add label in td dynamicaly created but if you want to add in "static" td then :
<table>
<tr>
<td runat="server" id="td01"></td>
</tr>
</table>

and in codebehind..;
Dim TmpLabel As New Label
TmpLabel.Text = "Vive la France"
td01.Controls.Add(TmpLabel)

Bye
Try This..

C#
Label lbl1 = new Label();
 
Label lbl2 = new Label();
 
lbl1 .Text="bind data from DB";
 
lbl2 .Text="bind data from DB";

p1.Controls.Add(lab);
pa.Controls.Add(lab);


.ASPX Page
XML
<p id = "P1" runat = "server"><p>

<p id = "P2" runat="server">
<p>
 
Share this answer
 
v2
it's me again..

follow these few steps

1 ) in the design page create a table :

ASP.NET
<table runat="server" id="theTable">

</table> 


2 ) in the code behind :

VB
Function GenerateLabel(ByVal pSqlReader As SqlDataReader) As Boolean
        Dim IndexOfLabel As Integer = 1
        Dim tmpTr As New HtmlTableRow
        Dim tmpTd As New HtmlTableCell
        While pSqlReader.Read()
            Dim TmpLabel As New Label
            ' you may not use id if you just display the labels
            TmpLabel.ID = "Label_" & IndexOfLabel.ToString
            TmpLabel.Text = pSqlReader.Item("FieldName").ToString

            tmpTd.Controls.Add(TmpLabel)
            tmpTr.Controls.Add(tmpTd)
            theTable.Controls.Add(TmpTr)
            IndexOfLabel+= 1
        End While
    End Function


3 ) add your try / catch

this code will display a table with as much rows as there are rows in your datatable


Hope helping you

Cyclopic
 
Share this answer
 
Comments
Manohar Khillare 9-May-12 8:05am    
can you write in c# i dont understand vb language
cyclopic 9-May-12 8:58am    
public bool GenerateLabel(SqlDataReader pSqlReader)
{
int IndexOfLabel = 1;
HtmlTableRow tmpTr = new HtmlTableRow();
HtmlTableCell tmpTd = new HtmlTableCell();
while (pSqlReader.Read()) {
Label TmpLabel = new Label();
// you may not use id if you just display the labels
TmpLabel.ID = "Label_" + IndexOfLabel.ToString();
TmpLabel.Text = pSqlReader.Item("FieldName").ToString;

tmpTd.Controls.Add(TmpLabel);
tmpTr.Controls.Add(tmpTd);
theTable.Controls.Add(tmpTr);
IndexOfLabel += 1;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900