-2

When looping getting an error in the below code like

Index was out of range. Must be non-negative and less than the size of the collection.

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

How to set the loop here?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2500094
  • 1,033
  • 6
  • 23
  • 42
  • We need more information. What type of object is listPost? How is the mw_post table connected to the program? – called2voyage Jul 02 '13 at 13:13
  • I'd advise more descriptive names of your counting variables, such as `customerCounter` or `itrCustomer` rather than `i` and `postCounter` or `itrPost` rather than `j`, or something to that effect. That way, you can avoid mixing up `i` and `j` (or any other counter). Make Wrong Code Look wrong. www.joelonsoftware.com/articles/Wrong.html – Tory Jul 02 '13 at 13:36

3 Answers3

5

Change i to j:

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

i is used as iterator for listCustomer and j is for listPost, that is why you went out of bounds.

gzaxx
  • 17,312
  • 2
  • 36
  • 54
0

Should this:

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

be instead this:

var grt1 = listPost[j];
postId = listPost[j].PostId;
posts1 = listPost[j].Posts;
postTime = listPost[j].PostTimeStamp;
Pat Lillis
  • 1,152
  • 8
  • 19
0

Your issue seems to be that when you are looping through listPosts, your are using "j" to loop, but then referencing the items with "i". If "i" grows to big (greater than listPost.Count) you will go BOOM with the error you have seen.

Bill Gregg
  • 7,067
  • 2
  • 22
  • 39