FormatException for string to DateTime 但不使用 DateTime C# Revision

FormatException for string to DateTime but not using DateTime C# Revision

当我尝试 运行 我的 WindowsPhone8.1 应用程序时,出现以下错误:

An exception of type 'System.FormatException' occurred in mscorlib.ni.dll but was not handled in user code

在故障排除提示下它说

When converting a string to DateTime, parse the string to take the date before putting each variable into the DateTime object.

但是,我没有在代码中或数据库中为要检索的实体使用 DateTime。我仔细检查了我是否正在检索正确的实体,并且它只包含 intnvarchar 数据类型。
这是我抛出异常的方法:

public int stringToTeam(String Team, int start)
    {
        start = Team.IndexOf("TeamID", start) + 8;
        int end = Team.IndexOf(",", start); //start index correct
        //teamsListBox.Items.Add(end);
        int id = Convert.ToInt32(Team.Substring(start, end - start)); //throws FormatException for string to DateTime
        //String id = Team.Substring(start, (end - start));
        teamsListBox.Items.Add("id is " + id);
        start = Team.IndexOf("TeamName", start) + 11;
        end = Team.IndexOf("\"", start);
        //teamsListBox.Items.Add(end);
        String name = Team.Substring(start, (end - start));
        teamsListBox.Items.Add("name is " + name);

        String city = Team.Substring(start, (end - start));
        teamsListBox.Items.Add("city is " + city);
        start = Team.IndexOf("TeamState", start) + 12;
        end = Team.IndexOf("\"", start);
        //teamsListBox.Items.Add(end);


        //creates a Teams model obj with values pulled from string and adds it to static list of objs in Teams model
        //teamsListBox.Items.Add(Teams.TeamsList.Count);
        Teams newTeam = new Teams(id, name, city);
        Teams.TeamsList.Add(newTeam);
        //teamsListBox.Items.Add(Teams.TeamsList.Count);
        //teamsListBox.Items.Add(newTeam.ToString());

        end += 4;
        if (end <= Team.Length)
        {
            return end;
        }
        else
        {
            return -1;
        }
    }

问题似乎出在这一行:

int id = Convert.ToInt32(Team.Substring(start, end - start));

我为我的 Bars 实体使用了完全相同的代码,它工作得很好,所以我知道这一行正确地将 string 转换为 int。但是,如果我在此文件的代码或相应的数据库 table 中没有使用 DateTime,为什么说我有一个 FormatException 对应 DateTime

修订:

感谢大家的帮助。在您的帮助下,我能够修复该异常。但是,现在我有另一个问题。我似乎无法将值打印到我的 GUI 中。下面是调用上面的 stringToTeams 方法的方法:

public async void GetTeams()
    {
        using (var client = new HttpClient())
        {
            teamsListBox.Items.Add("using block entered");
            client.BaseAddress = new Uri("http://nflff.azurewebsites.net");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            teamsListBox.Items.Add("client's defaultrequestheaders done");

            HttpResponseMessage response = await client.GetAsync("api/teams");//not getting past here
            teamsListBox.Items.Add("right after response"); //not printing
            if (response.IsSuccessStatusCode)
            {
                teamsListBox.Items.Add("if entered");
                //IList<Teams> Teams = await response.Content.ReadAsAsync<IList<Teams>>();
                string teams = await response.Content.ReadAsStringAsync();

                int start = 0;
                while (start != -1)
                {
                    start = stringToTeam(teams, start); //throwing exception inside stringToTeams, which isn't being called anywhere else
                }
            }
            teamsListBox.Items.Add(Teams.TeamsList.Count);
            foreach (var team in Teams.TeamsList)
            {
                teamsListBox.Items.Add(team.ToString());
            }
        }
    }

前 2 条测试消息正在打印到 GUI;但是,"right after response" 消息和之后的任何消息或值都不会打印。真正让我感到困惑的是,正在执行 stringToTeam 中的代码,而该方法仅在 GetTeams 中被调用。此外,GetTeams 的代码与 GetBars 的代码完全相同(除了将条替换为团队),我的条代码工作正常。有任何想法吗?再次感谢您一直以来的帮助。我正在使用连接到 Web API 服务的 SQL 数据库。我的后端是 .NET。

感谢大家的建议。在我修复开始和结束索引值后,一切都开始正常工作。至于打印问题,这听起来可能很尴尬,但我忘记在测试中禁用断点。一旦我这样做了,代码就起作用了。再次感谢。