我是.net开发的新手,当循环在下面的代码中得到错误,如
指数超出范围。必须是非负数且小于集合的大小。
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 :(得分:5)
将i
更改为j
:
var grt1 = listPost[j];
postId = listPost[j].PostId;
posts1 = listPost[j].Posts;
postTime = listPost[j].PostTimeStamp;
i
用作listCustomer
的迭代器,j
用于listPost
,这就是你越界的原因。
答案 1 :(得分: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;
答案 2 :(得分:0)
您的问题似乎是当您循环浏览listPosts时,您正在使用&#34; j&#34;循环,但然后使用&#34; i&#34;引用项目。如果&#34;我&#34;增长到大(大于listPost.Count)你会看到你看到的错误。