如何处理:索引超出了数组错误的范围 [英] How to approach: Index was outside the bounds of the array error

查看:128
本文介绍了如何处理:索引超出了数组错误的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将提供给我的数据导入到连接的数据库中。但是,不是使用向导,而是要求我使用Web表单进行导入。给我的数据不是最好的格式。



我的代码在下面发布,以及代码正在做什么的评论和我遇到问题的评论



 受保护  void  uxImportBtn_Click( object  sender,EventArgs e)
{
var accountTrackerImportSVC = new AccountTrackerImportSVC();
// Imports Table
var dtInfo = GetContent();

foreach (DataRow drItem in dtInfo.Rows)
{
// 从LastName_PreferredFirstName获取工人数量
var dataCustodian = drItem [ DataCustodian]。ToString();
var fullName = dataCustodian.Split(' '' /');
var firstName = fullName [ 0 ];
var lastName = fullName [ 1 ]; // 错误抛出此处

var dtWorkerNum = AccountTrackerImportDAL.ViewWorkerNumByLastNameFirstName(lastName,firstName);



当我调试我的网页时,我可以看到承包商正在传递给dataCustodian变量。使用我拥有的代码和特定字段的格式,我将无法拆分空格或/。



我的var firstName ='Contractor'和抓取的lastName是下一个条目的一个。我没有中间名,中间名首字母或外国名字。我只需要知道如何解决这个问题。



关于如何在我的代码中解释这一点的任何想法?

解决方案

这是我现在正在经历的确切问题。我认为测试条件是if else语句对吗?对不起,如果我的问题看起来很简单,我还在学习C#的来龙去脉。

是:

<前lang =cs> 字符串 dataCustodian = drItem [ DataCustodian]。ToString();
string [] fullName = dataCustodian.Split(' '' /');
if (fullName.Length > = 2
{
string firstName = fullName [ 0 ] ;
string lastName = fullName [ 1 ];
...


I'm importing data given to me into a connected database. However rather than use the wizard it's required that I do the import using a web form. The data given to me is not in the best format.

My code is posted below along with comments of what the code is doing and comments of where I am having issues

protected void uxImportBtn_Click(object sender, EventArgs e)
        {
            var accountTrackerImportSVC = new AccountTrackerImportSVC();
            // Imports Table
            var dtInfo = GetContent();

            foreach (DataRow drItem in dtInfo.Rows)
            {
                // Get Worker Num From LastName_PreferredFirstName
                var dataCustodian = drItem["DataCustodian"].ToString();
                var fullName = dataCustodian.Split(' ','/');
                var firstName = fullName[0];
                var lastName = fullName[1]; // Error Thrown Here

                var dtWorkerNum = AccountTrackerImportDAL.ViewWorkerNumByLastNameFirstName(lastName, firstName);


When I debug my web page I can see that 'Contractor' is being passed on to the dataCustodian variable. With the code that I have and by the format of that particular field, I won't be able to split on a 'space' or '/'.

My var firstName = 'Contractor' and the lastName be grabbed is one for the next entry. I don't have middle names, middle initials, or foreign names. I just need to know how to solve this issue.

Any ideas on how account for this in my code?

解决方案

"That's the exact issue that I'm going through right now. I take it a test condition would consist of if else statement right? Sorry if my question seems elementary, I'm still learning the ins and outs of C#."
Yes:

string dataCustodian = drItem["DataCustodian"].ToString();
string[] fullName = dataCustodian.Split(' ','/');
if (fullName.Length >= 2)
   {
   string firstName = fullName[0];
   string lastName = fullName[1];
   ...


这篇关于如何处理:索引超出了数组错误的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆