Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Multiple controls with the same ID 'lebelId1' were found. FindControl requires that controls have unique IDs.
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using ControlMethods_BL;
using CheckList_DAL;

namespace CheckList
{
    public partial class Home : System.Web.UI.Page
    {
        static int number = 2;
        ControlMethod controlMethod = new ControlMethod();
        string strConn = connection.cn;
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd;
        //UserRegistrationBL registration = new UserRegistrationBL();
        TaskMaster taskMaster = new TaskMaster();
        SqlDataReader rdr;
        protected void Page_Load(object sender, EventArgs e)
        {
            DataReaderMethod();
        }

        private void DataReaderMethod()
        {
            conn.ConnectionString = strConn;
            string strQuery = "select S.Id, G.GMName, T.CheckPointTaskType, T.CheckRemark, S.TaskDate from tblTaskMasterStatus S, tblTaskMaster T, tblGeneralMaster G where S.TaskId=T.Id and T.ServerId=G.ID";

            cmd = new SqlCommand(strQuery, conn);
            try
            {
                conn.Open();
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {                  
                        DynamicControlMethod(rdr);                   
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                conn.Close();
            }
        }

        private void DynamicControlMethod(SqlDataReader rdr)
        {                  
                for (int i = 1; i <number mode="hold" />                {                    
                    Table t = new Table();
                    TableRow row = new TableRow();

                    TableCell cellId = new TableCell();                   
                    cellId.Width = Unit.Pixel(1);
                    Label labelID = new Label();
                    labelID.ID = "lebelId" + i.ToString();
                    labelID.Text = rdr["Id"].ToString();
                    cellId.Controls.Add(labelID);
                    row.Cells.Add(cellId);                   
                    pnlID.Controls.Add(row);

                    TableCell cellGMName = new TableCell();
                    cellId.Width = Unit.Pixel(70);
                    Label lebelGMName = new Label();
                    lebelGMName.ID = "lebelGMName" + i.ToString();
                    lebelGMName.Text = rdr["GMName"].ToString();
                    cellGMName.Controls.Add(lebelGMName);
                    row.Cells.Add(cellGMName);                    
                    pnlServerName.Controls.Add(row);

                    TableCell cellCheckPointTaskType = new TableCell();
                    cellId.Width = Unit.Percentage(10);
                    Label lebelCheckPointTaskType = new Label();
                    lebelCheckPointTaskType.ID = "lebelCheckPointTaskType" + i.ToString();
                    lebelCheckPointTaskType.Text = rdr["CheckPointTaskType"].ToString();
                    cellCheckPointTaskType.Controls.Add(lebelCheckPointTaskType);
                    row.Cells.Add(cellCheckPointTaskType);                    
                    pnlCheckPointTaskType.Controls.Add(row);

                    TableCell cellCheckRemark = new TableCell();
                    cellId.Width = Unit.Percentage(30);
                    Label lebelCheckRemark = new Label();
                    lebelCheckRemark.ID = "lebelCheckRemark" + i.ToString();
                    lebelCheckRemark.Text = rdr["CheckRemark"].ToString();
                    cellCheckRemark.Controls.Add(lebelCheckRemark);
                    row.Cells.Add(cellCheckRemark);                   
                    pnlCheckRemark.Controls.Add(row);
                   
                    TableCell cellSpace = new TableCell();
                    cellId.Width = Unit.Percentage(10);
                    TextBox textboxSpace = new TextBox();
                    textboxSpace.ID = "textboxSpace" + i.ToString();
                    cellSpace.Controls.Add(textboxSpace);
                    row.Cells.Add(cellSpace);                    
                    pnlSpace.Controls.Add(row);

                    TableCell cellComment = new TableCell();
                    cellId.Width = Unit.Percentage(5);
                    CheckBox checkBoxComment = new CheckBox();
                    checkBoxComment.ID = "checkBoxComment" + i.ToString();
                    cellComment.Controls.Add(checkBoxComment);
                    row.Cells.Add(cellComment);                   
                    pnlCheck.Controls.Add(row);

                    TableCell cellRemarks = new TableCell();
                    cellId.Width = Unit.Percentage(10);
                    TextBox remarkTextBox = new TextBox();
                    remarkTextBox.ID = "remarkTextBox" + i.ToString();
                    cellRemarks.Controls.Add(remarkTextBox);
                    row.Cells.Add(cellRemarks);                   
                    pnlRemarks.Controls.Add(row);                
                }            
        }

        public void btnSubmit_Click(object sender, EventArgs e)
        {

            string strCon = ConfigurationManager.ConnectionStrings["RealWorldConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strCon);

            for (int i = 0; i < 1; i++)
            {
                string SpaceData = "textboxSpace" + i.ToString();
                TextBox txt = (TextBox)pnlSpace.FindControl(SpaceData);          
            }               
        }
    }
}

Kindly help I am unbale to insert data into database.
Posted
v2

1 solution

The error is pretty explicit:
"Multiple controls with the same ID 'lebelId1' were found. FindControl requires that controls have unique IDs."

So instead of naming all the controls you add the same:
C#
Label labelID = new Label();
labelID.ID = "lebelId" + i.ToString();
Create a class level integer, set it to one before you start reading from your DB:
C#
labelNumber = 1;
while (rdr.Read())
   {
   DynamicControlMethod(rdr);
   }
And use that in your label construction code:
C#
Label labelID = new Label();
labelID.ID = "lebelId" + labelNumber++.ToString();
 
Share this answer
 

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