2013-07-02 117 views
-2

我是新來的.net開發,當循環獲取下面的代碼中的錯誤,如c# - 索引超出範圍例外

索引超出範圍。必須是非負數且小於集合的大小。

public void postM() 
    { 
     for (int i = 0; i < listCustomer.Count; i++) 
     { 

      var grt = listCustomer[i]; 
      id = grt.UserId; 

      //When the loops come second time 

      for (int j = 0; j < listPost.Count; j++) 
      { 
       //Here I'm getting the above error 
       var grt1 = listPost[i]; 
       postId = listPost[i].PostId; 
       posts1 = listPost[i].Posts; 
       postTime = listPost[i].PostTimeStamp; 

       DbConnection.Open(); 
       DbCommand = new OleDbCommand("select count(*) from mw_post where post_id = '" + postId + "'", DbConnection); 
       OleDbDataReader DbReader1 = DbCommand.ExecuteReader(); 
       while (DbReader1.Read()) 
       { 
        count = DbReader1[0].ToString(); 
        cnt = Convert.ToInt32(count); 
        if ((cnt == 0) && (posts != "")) 
        { 
         DbCommand = new OleDbCommand("insert into mw_post(post_id,customer_id,post,post_date,community) values('" + postId + "','" + id + "','" + posts1 + "', '" + postTime + "','LinkedIn')", DbConnection); 
         DbCommand.ExecuteNonQuery(); 

         if (posts.ToUpper().Contains("Personal Loan".ToUpper())) 
         { 
          DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '2',customer_id='" + id + "' where post = '" + posts + "'", DbConnection); 
          DbCommand.ExecuteNonQuery(); 
         } 
         else if (posts.ToUpper().Contains("Credit Card".ToUpper())) 
         { 
          DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '1',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection); 
          DbCommand.ExecuteNonQuery(); 
         } 
         else if (posts.ToUpper().Contains("Home Loan".ToUpper())) 
         { 
          DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '3',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection); 
          DbCommand.ExecuteNonQuery(); 
         } 
         else if (posts.ToUpper().Contains("Car Loan".ToUpper())) 
         { 
          DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '4',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection); 
          DbCommand.ExecuteNonQuery(); 
         } 
         else if (posts.ToUpper().Contains("Deposit".ToUpper())) 
         { 
          DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '5',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection); 
          DbCommand.ExecuteNonQuery(); 
         } 
         else if (posts.ToUpper().Contains("Debit Card".ToUpper())) 
         { 
          DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '7',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection); 
          DbCommand.ExecuteNonQuery(); 
         } 
         else 
         { 
          DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '6',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection); 
          DbCommand.ExecuteNonQuery(); 
         } 
        } 
       } 
       DbReader1.Close(); 
       DbConnection.Close(); 
      } 
     } 
    } 

如何設置循環?請給我建議。

任何想法?提前致謝。

+0

我們需要更多信息。什麼類型的對象是listPost? mw_post表如何連接到程序? – called2voyage

+0

我建議您的計數變量的更多描述性名稱,比如'customerCounter'或'itrCustomer',而不是'i'和'postCounter'或'itrPost'而不是'j',或者其他類似的東西。這樣,你可以避免混淆'i'和'j'(或任何其他計數器)。使錯誤的代碼看起來不對。 www.joelonsoftware.com/articles/Wrong.html – Tory

回答

5

變化ij

var grt1 = listPost[j]; 
postId = listPost[j].PostId; 
posts1 = listPost[j].Posts; 
postTime = listPost[j].PostTimeStamp; 

i被用作迭代器listCustomerjlistPost,這就是爲什麼你出界了。

0

應該這樣:

var grt1 = listPost[i]; 
postId = listPost[i].PostId; 
posts1 = listPost[i].Posts; 
postTime = listPost[i].PostTimeStamp; 

是不是這樣的:

var grt1 = listPost[j]; 
postId = listPost[j].PostId; 
posts1 = listPost[j].Posts; 
postTime = listPost[j].PostTimeStamp; 
0

您的問題似乎是,當你通過listPosts循環,你正在使用「J」循環,但隨後引用與「我」的項目。如果「我」增長到大(大於listPost.Count),您將會看到BOOM。