0

我正在尝试在运行时在表单中添加文本框。我还想在这些框中显示数据。数据是从数据库中获取的。我的代码有效,但我只能显示第一行,不明白为什么我使用的循环只运行一次。

在有人决定文本框重叠之前,请正确检查代码并给我一个合理的解释,说明这是怎么可能的,当我清楚地Yi

我向你保证我已经尝试过,检查了 30 次,发现文本框没有相互重叠。即使我们认为文本框为了参数而相互重叠,您也必须同意我的观点,即可见的文本框将包含表格最后一行的数据,而不是像我这样的第一行数据。对不起,我厌倦了人们得出的结论,即文本框在明显不是重叠的情况下是重叠的。下面是我的代码。

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();
  • 更新:

好的,现在我已经确认循环没有正常运行,并且还确定了导致问​​题的代码块。谁能建议我如何克服这个问题?

出现在循环内的代码块即使它应该运行 3 次也只运行一次。

            //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();

删除 SQL 部分可以使循环运行 3 次,但是如果我在循环中有 SQL 部分,它将无法工作。可以做些什么来克服这个问题?

4

2 回答 2

4

你的循环运行正常。问题是创建的所有文本框都重叠。在循环中完成一轮后, 更改X文本框位置的位置。

//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;

编辑

            try
            { 
                //your entire code
            }
            catch (Exception e) 
            { 
                MessageBox.Show(e.Message); 
            }
于 2013-01-07T05:53:55.347 回答
1

正如 Ken Kin 指出的那样,问题与控件没有任何关系,而是与抛出的异常有关。

我终于发现了错误并解决了它,我正在发布工作代码,希望它可以帮助其他可能面临类似问题的人

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();
                }
            }
于 2013-01-07T14:43:46.733 回答