2013-01-07 69 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

抱歉,我無法理解你的問題。我有一個處理程序內的代碼。 –