简体   繁体   中英

dynamically add multiple textBoxes in the form using C#

I am trying to add textBoxes in the form during runtime. I also want to display data in those boxes. The data is fetched from a database. My code works but i am only able to display the first row, can't understand why the loop i am using runs only once.

And before someone decides that the textBoxes are overlapping, please check the code properly and give me a logical explanation as to how that is possible, when i have clearly incremented the value of Y as a multiple of i

I assurre you i have tried, checked rechecked like 30 times already and found out the textBoxes are not overlapping each other. Even if we consider that the textBoxes were overlapping each other for argument sake, you must agree with me that then the textBoxes that are visible would contain data from the last row of the table and not the first as is the case with me. Sorry for the rant but i am tired of people concluding that the textBoxes are overlapping when clearly it isnt. Below is my code.

var count=5; // dependent 

//SQL connection and data read begins
SqlCeConnection conn=new SqlCeConnection();
conn.ConnectionString=connection; //connection is a string variable which has the connection string details
conn.Open();
SqlCeCommand com=new SqlCeCommand();
com.Connection=conn;
com.CommandType=CommandType.Text;

for(int i=3; i<=count; i++) {
    com.CommandText="SELECT pname, cname, budget, advance, ddate FROM data WHERE (ID = @id)";
    com.Parameters.AddWithValue("@id", i);
    com.ExecuteNonQuery();
    SqlCeDataReader rd=com.ExecuteReader();

    while(rd.Read()) {
        pname=(rd["pname"].ToString());
        cname=(rd["cname"].ToString());
        budget=(rd["budget"].ToString());
        advance=(rd["advance"].ToString());
        ddate=(rd["ddate"].ToString());
    }

    TextBox tobj=new TextBox();
    tobj.Location=new Point(10, (40+((i-2)*20)));
    tobj.Tag=1;
    tobj.Text=pname;
    tobj.AutoSize=false;
    tobj.Width=150;
    tobj.ReadOnly=true;
    this.Controls.Add(tobj);

    TextBox tobj1=new TextBox();
    tobj1.Location=new Point(160, (40+((i-2)*20)));
    tobj1.Tag=2;
    tobj1.Text=cname;
    tobj1.AutoSize=false;
    tobj1.Width=150;
    tobj1.ReadOnly=true;
    this.Controls.Add(tobj1);

    TextBox tobj2=new TextBox();
    tobj2.Location=new Point(310, (40+((i-2)*20)));
    tobj2.Tag=3;
    tobj2.Text=budget;
    tobj2.AutoSize=false;
    tobj2.Width=100;
    tobj2.ReadOnly=true;
    this.Controls.Add(tobj2);

    TextBox tobj3=new TextBox();
    tobj3.Location=new Point(410, (40+((i-2)*20)));
    tobj3.Tag=4;
    tobj3.Text=advance;
    tobj3.AutoSize=false;
    tobj3.Width=100;
    tobj3.ReadOnly=true;
    this.Controls.Add(tobj3);

    TextBox tobj4=new TextBox();
    tobj4.Location=new Point(510, (40+((i-2)*20)));
    tobj4.Tag=5;
    tobj4.Text=ddate;
    tobj4.AutoSize=false;
    tobj4.Width=100;
    tobj4.ReadOnly=true;
    this.Controls.Add(tobj4);
}

com.Dispose();
conn.Close();
  • update:

Ok now i have confirmed that the loop is not running properly and also identified the block of codes which is causing the trouble. Could anyone suggest me on how to overcome this?

The block of codes which when present inside the loop makes it run only once even when it should have ran 3 times.

            //SQL connection and data read begins

            int count =5;
            SqlCeConnection conn = new SqlCeConnection();
            conn.ConnectionString = connecion; //connection is a string variable which has the connection string details
            conn.Open();
            SqlCeCommand com = new SqlCeCommand();
            com.Connection = conn;
            com.CommandType = CommandType.Text;

            MessageBox.Show("The value of count just before the loop is " + count.ToString());
            for (int i = 3; i <= count; i++)
            {

                com.CommandText = "SELECT pname, cname, budget, advance, ddate FROM data WHERE (ID = @id)";
                com.Parameters.AddWithValue("@id", i);
                com.ExecuteNonQuery();
                SqlCeDataReader rd = com.ExecuteReader();
                while (rd.Read())
                {
                    pname = (rd["pname"].ToString());
                    cname = (rd["cname"].ToString());
                    budget = (rd["budget"].ToString());
                    advance = (rd["advance"].ToString());
                    ddate = (rd["ddate"].ToString());
                }
            }

            com.Dispose();
            conn.Close();

Removing the SQL part enables the loop to run 3 times, how ever if i have the SQL part inside the loop it wont work. What can be possibly done to overcome this?

Your loop runs ok. The issue is all the text box created overlapped. Change the X possision of text boxes location once competed a round in the loop.

//Before strat loop
int xCoorCons=10;  
...
...
//inside loop    
tobj.Location = new Point(xCoorCons, (40+((i-2)*20)));  
...  
...
//at end of the loop
xCoorCons=xCoorCons+20;

EDIT

            try
            { 
                //your entire code
            }
            catch (Exception e) 
            { 
                MessageBox.Show(e.Message); 
            }

The problem was as is pointed out by Ken Kin didn't have anything to do with the control but rather the exception that was being thrown.

I have finally found out the error and solved it, I am posting the working code, hope it will serve to help someone else who might face similar troubles

SqlCeConnection conn = null;
                SqlCeCommand com = null;
                try
                {
                    //SQL connection and data read begins

                    conn = new SqlCeConnection();
                    conn.ConnectionString = connecion; //connection is a string variable which has the connection string details
                    conn.Open();
                    com = new SqlCeCommand();
                    com.Connection = conn;
                    com.CommandType = CommandType.Text;
                    com.CommandText = "SELECT pname, cname, budget, advance, ddate FROM data WHERE (ID = @id)";
                    com.Parameters.Add("@ID", SqlDbType.Int);
                    com.Prepare();
                    MessageBox.Show("The value of count just before the loop is " + count.ToString());

                    for (int i = 3; i <= count; i++)
                    {
                        com.Parameters[0].Value = i;
                        using (SqlCeDataReader rd = com.ExecuteReader())
                        if (rd.Read())
                        {
                            pname = (rd["pname"].ToString());
                            cname = (rd["cname"].ToString());
                            budget = (rd["budget"].ToString());
                            advance = (rd["advance"].ToString());
                            ddate = (rd["ddate"].ToString());

                            TextBox tobj = new TextBox();
                            tobj.Location = new Point(10, (40 + ((i - 2) * 20)));
                            tobj.Tag = 1;
                            tobj.Text = pname;
                            tobj.AutoSize = false;
                            tobj.Width = 150;
                            tobj.ReadOnly = true;
                            this.Controls.Add(tobj);

                            TextBox tobj1 = new TextBox();
                            tobj1.Location = new Point(160, (40 + ((i - 2) * 20)));
                            tobj1.Tag = 2;
                            tobj1.Text = cname;
                            tobj1.AutoSize = false;
                            tobj1.Width = 150;
                            tobj1.ReadOnly = true;
                            this.Controls.Add(tobj1);

                            TextBox tobj2 = new TextBox();
                            tobj2.Location = new Point(310, (40 + ((i - 2) * 20)));
                            tobj2.Tag = 3;
                            tobj2.Text = budget;
                            tobj2.AutoSize = false;
                            tobj2.Width = 100;
                            tobj2.ReadOnly = true;
                            this.Controls.Add(tobj2);

                            TextBox tobj3 = new TextBox();
                            tobj3.Location = new Point(410, (40 + ((i - 2) * 20)));
                            tobj3.Tag = 4;
                            tobj3.Text = advance;
                            tobj3.AutoSize = false;
                            tobj3.Width = 100;
                            tobj3.ReadOnly = true;
                            this.Controls.Add(tobj3);

                            TextBox tobj4 = new TextBox();
                            tobj4.Location = new Point(510, (40 + ((i - 2) * 20)));
                            tobj4.Tag = 5;
                            tobj4.Text = ddate;
                            tobj4.AutoSize = false;
                            tobj4.Width = 100;
                            tobj4.ReadOnly = true;
                            this.Controls.Add(tobj4);

                        }
                    }



                    //SQL operation ends
                }
                finally
                {
                    if (null != com) com.Dispose();
                    if (null != conn) conn.Dispose();
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM