Click here to Skip to main content
15,899,634 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to create n no of label in web form according to database table column value.

If table contain 5 rows then I want to create 5 labels with text of row value & empty text box ahead of label
Posted
Comments
Suvendu Shekhar Giri 8-Dec-15 3:03am    
It's a simple task but let us see what have you tried so far.
Shrikesh_kale 8-Dec-15 4:41am    
"\n" are not working
Shrikesh_kale 8-Dec-15 4:41am    
using (SqlCommand cmd = new SqlCommand(sqlcommand))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = connection;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);

if (dt.Rows.Count > 0)
{
TableRow rw = new TableRow();
Label NewLabel = new Label();
NewLabel.ID = "Label";
for (int i = 0; i < dt.Rows.Count; i++)
{

NewLabel.Text += dt.Rows[i][0].ToString() + "\n";

}
this.form1.Controls.Add(NewLabel);

}



//SqlCommand cmd = new SqlCommand("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + tablename + " ' ORDER BY ORDINAL_POSITION", connection);

//SqlDataReader dr = cmd.ExecuteReader();

//if (dr.Read())

//{
// for (int i = 0; i < length; i++)
// {
// Label NewLabel = new Label();
// NewLabel.ID = "Label" + dr["COLUMN_NAME"].ToString();
// NewLabel.Text = dr["COLUMN_NAME"].ToString();
// this.form1.Controls.Add(NewLabel);
// }

//}

connection.Close();
}
}
}
Member 13738315 10-Apr-18 7:46am    
pls send me the coding for same problem in vb.net. I want to display the data from database to lables for attendance form. Because i registered new student details, the attendance form should be add the student name, idno automataically that are displayed in lables dynamically. Another one attendance form each student detail have a combobox for mark the attendance present or opsent. i think you did understand.pls give me the code for attendance.pls help me...

1 solution

Hi,

Please try the following code , 'n' is the row value from your table

C#
for (int i = 0; i < n; i++)
        {

            Label lbl = new Label();
            lbl.ID = "Label" + i.ToString();

            Panel1.Controls.Add(lbl); // add the label to a panel
        }
 
Share this answer
 
v2
Comments
Shrikesh_kale 8-Dec-15 4:58am    
Can you help me for table because i need text box also.
Praveen_P 8-Dec-15 7:02am    
Modify the above code by including

TextBox txtbox= new TextBox();
txtbox.ID = "txt" + i.ToString();

Panel1.Controls.Add(txtbox);
Member 13738315 10-Apr-18 7:46am    
pls send me the coding for same problem in vb.net. I want to display the data from database to lables for attendance form. Because i registered new student details, the attendance form should be add the student name, idno automataically that are displayed in lables dynamically. Another one attendance form each student detail have a combobox for mark the attendance present or opsent. i think you did understand.pls give me the code for attendance.pls help me...
Praveen_P 10-Apr-18 7:53am    
Post your above question as a separate thread with tag VB.Net , also please provide what you have tried so far and where you got stuck.. good luck
Praveen_P 10-Apr-18 8:30am    
Because i dont know VB.Net and if you post it as a separate question you would definitely got some expert advice

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