[英]Why am I getting a “Index was outside the bounds of the array” error when loading a Crystal Report?
[英]Why am I getting index out of bounds error from database
我知道什么是越界指标。 当我调试时,我也明白为什么。 基本上发生的事情是我对数据库进行了筛选以查找可能的/待处理的记录。 然后,我收集这些数字的数组,将它们发送到另一台服务器,以检查这些数字是否已升级为销售产品。 如果已升级为销售,服务器将以新的销售订单ID和我的旧的待处理销售订单ID(SourceID)进行响应。 然后,我在该列表上执行for循环以将其过滤为特定的SourceID,并将SourceID更新为销售订单ID,并更改其他几个值。 问题是,当我在第一个过滤器上使用该过滤器时,它将抛出索引超出范围错误。 我检查了过滤器返回的结果,结果为0。我觉得有点奇怪,因为我从列表中取出了销售订单号,所以它应该在那儿。 所以我不知道这是什么交易。 这是引发错误的相关代码。 而且并非一直如此。 就像我今天早上才运行代码一样,它没有引发错误。 但是昨晚在我回家之前就做到了。
filter.RowFilter = string.Format("Stage = '{0}'", Potential.PotentialSale);
if (filter.Count > 0)
{
var Soids = new int[filter.Count];
Console.Write("Searching for Soids - (");
for (int i = 0; i < filter.Count; i++)
{
Console.Write(filter[i][1].ToString() + ",");
Soids[i] = (int)filter[i][1];
}
Console.WriteLine(")");
var pendingRecords = Server.GetSoldRecords(Soids);
var updateRecords = new NameValueCollection();
for (int i = 0; i < pendingRecords.Length; i++)
{
filter.RowFilter = "Soid = " + pendingRecords[i][1];
filter[0].Row["Soid"] = pendingRecords[i][0];
filter[0].Row["SourceId"] = pendingRecords[i][1];
filter[0].Row["Stage"] = Potential.ClosedWon;
var potentialXML = Potential.GetUpdatePotentialXML(filter[0].Row["Soid"].ToString(), filter[0].Row["Stage"].ToString());
updateRecords.Add(filter[0].Row["ZohoID"].ToString(), potentialXML);
}
如果我要算右边的第17行是抛出错误的错误。 endingRecords是一个object [] []数组。 endingRecords [i]是单个记录。 endingRecords [i] [0]是新的销售订单ID(SOID),pendingRecords [i] [1]是旧的SOID(现在是SourceID)
任何帮助吗? 是因为我将SOID更改为新的SOID,并且过滤器会自动更新? 我只是不知道
好吧,我最终改变了它们的工作方式,现在实际上使它更好了。 由于要返回的表的结构,我要发布的代码有很多硬编码的数字。 对于那个很抱歉。 从那时起我就学会了不这样做,但是我现在正在从事一个不同的项目,当我不得不更改程序时,我将对此进行更改。 但是这里是解决方案。
var potentials = Server.GetNewPotentials(); //loads all records from server
for (int i = 0; i < potentials.Length; i++)
{
var filter = AllPotentials.DefaultView;
var result1 = CheckSoidOrSource(potentials[i].Soid, true);
var result2 = CheckSoidOrSource(potentials[i].SourceID,false) ;
//This potential can't be found at all so let's add it to our table
if (result1+result2==0)
{
Logger.WriteLine("Found new record. Adding it to DataTable and sending it to Zoho");
AllPotentials.Add(potentials[i]);
filter.RowFilter = string.Format("Soid = '{0}'", potentials[i].SourceID);
var index = AllPotentials.Rows.IndexOf(filter[0].Row);
ZohoPoster posterInsert = new ZohoPoster(Zoho.Fields.Potentials, Zoho.Calls.insertRecords);
AllPotentials.Rows[index]["ZohoID"] = posterInsert.PostNewPotentialRecord(3, filter[0].Row);
}
//This potential is not found, but has a SourceId that matches a Soid of another record.
if (result1==0 && result2 == 1)
{
Logger.WriteLine("Found a record that needs to be updated on Zoho");
ZohoPoster posterUpdate = new ZohoPoster(Zoho.Fields.Potentials, Zoho.Calls.updateRecords);
filter.RowFilter = string.Format("Soid = '{0}'", potentials[i].SourceID);
var index = AllPotentials.Rows.IndexOf(filter[0].Row);
AllPotentials.Rows[index]["Soid"] = potentials[i].Soid;
AllPotentials.Rows[index]["SourceId"] = potentials[i].SourceID;
AllPotentials.Rows[index]["PotentialStage"] = potentials[i].PotentialStage;
AllPotentials.Rows[index]["UpdateRecord"] = true;
AllPotentials.Rows[index]["Amount"] = potentials[i].Amount;
AllPotentials.Rows[index]["ZohoID"] = posterUpdate.UpdatePotentialRecord(3, filter[0].Row);
}
}
AllPotentials.AcceptChanges();
}
private int CheckSoidOrSource(string Soid, bool checkSource)
{
var filter = AllPotentials.DefaultView;
if (checkSource)
filter.RowFilter = string.Format("Soid = '{0}' OR SourceId = '{1}'",Soid, Soid);
else
filter.RowFilter = string.Format("Soid = '{0}'", Soid);
return filter.Count;
}
基本上发生的是,当我以这种方式过滤数据时,我注意到了一些关于数据的信息。 这两个结果只会返回以下结果(0,0)(0,1)和(1,0)(0,0)意味着该表中根本不存在该记录,因此我需要添加它。 (1,0)表示销售订单ID(Soid)与表中的另一个Soid相匹配,因此它已经存在。 最后(0,1)表示此表中不存在Soid,但我发现了一条记录,其中以Soid作为源...对我而言,这意味着将Soid作为源的记录已升级。潜在的销售机会,这又意味着我必须更新唱片和Zoho。 这对我来说减少了很多工作,因为现在我不必搜索获胜和丢失的记录,而只需要搜索丢失的记录。 更少的代码相同的结果总是一件好事:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.