2013-01-07 45 views
0

我想在运行时在窗体中添加文本框。我也想在这些框中显示数据。数据从数据库中获取。我的代码工作,但我只能够显示第一行,无法理解为什么我使用的循环只运行一次。动态地添加多个文本框使用C#的形式

而在此之前有人决定,该文本框是重叠的,请妥善检查代码并给我一个合理的解释,如何这是可能的,当我已经清楚地递增Y价值为i

多我保证你我已经试过了,已经复查了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部分它将不会工作。可以做些什么来克服这一点?

+1

什么是'Count'在for循环 – Pranav

+0

为什么'i'从3开始? –

+0

count包含ID的值,ID是表的主键,并且自动递增,当前我在表中有3行,因此count的值为5. ID从3开始递增1 –

回答

1

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

我终于发现了错误并解决它,我张贴的工作代码,希望这将有助于帮助别人谁可能面临类似的麻烦

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(); 
       } 
      } 
+0

好的工作。你有没有尝试类似于'com.Parameters.Add(“@ ID”)。Value = 123;'? –

+0

不,在当前上下文中,需要在运行时输入值,具体取决于表中的行数。不过,如果你需要我,我可以尝试看看它是否有效。虽然如果它不是什么紧急的事情,我宁愿不去碰它,因为当天有我的填充:D –

+0

我不能在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); 
      } 
+0

我同意你应该指定每个文本框的位置 –

+0

我已作出规定自动更改文本框的Y位置的值。我需要他们低于彼此,如果我改变X的位置,这将如何成为可能? –

+0

'tobj.Location = new Point(10,(40 +((i-2)* 20)));'看看Y应该如何改变 –

0

如果你的变量i只是叶小于或等于4的值,这是很明显,你的代码放入try-catch块与追赶型Exception而且没有重新抛出。

+0

是的,我没有检查,在for循环中添加SQL的东西使它只运行一次,似乎不明白为什么会发生这种情况 –

+0

所以我问你两次,现在三次。你把代码放在哪里?它在一个处理程序中吗?或者你可以测试'新开发人员'的方式,在try-catch中修改你的代码会阻止你的代码,并看看会发生什么。 –

+0

抱歉,我无法理解你的问题。我有一个处理程序内的代码。 –