gpt4 book ai didi

c# - 为什么我得到 System.IndexOutOfRangeException

转载 作者:行者123 更新时间:2023-12-02 11:06:50 27 4
gpt4 key购买 nike

我还有另外两个按钮,代码几乎完全相同。但是,当我尝试运行此代码以获取所有结果时,出现错误“System.IndexOutOfRangeException”。

显示无效 NHS 号码按钮有效,但我不明白为什么它有效,但显示所有记录按钮无效。错误出现在较低代码块的 dgvResults 部分,但不是第一个。

 private void cmdShowInvalidNHSnumbers_Click(object sender, EventArgs e)
{
string databaseFilename;
string sql;

dgvResults.ColumnCount = 3;
dgvResults.Columns[0].Name = "NHSNumber";
dgvResults.Columns[1].Name = "Patient";
dgvResults.Columns[2].Name = "Problem";

dgvResults.Rows.Clear();

databaseFilename = Application.StartupPath + "\\nhsnumberchecker.db";

System.Data.SQLite.SQLiteConnection m_dbConnection;
m_dbConnection = new System.Data.SQLite.SQLiteConnection("Data Source=" + databaseFilename + ";Version=3;");
m_dbConnection.Open();

sql = @"
SELECT * FROM results
-- WHERE NHSNumber = 'N'
WHERE NHSNumberValid = 'N'
ORDER BY Surname;";

System.Data.SQLite.SQLiteCommand command = new System.Data.SQLite.SQLiteCommand(sql, m_dbConnection);

System.Data.SQLite.SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
dgvResults.Rows.Add(reader["NHSNumber"], reader["Surname"] + ", " + reader["GivenName"], NHSnumberError(reader.GetString(0)));
}

dgvResults.Refresh();
dgvResults.AutoResizeColumns();
m_dbConnection.Close();
}







private void cmdShowAllResults_Click(object sender, EventArgs e)
{
string databaseFilename;
string sql;

dgvResults.ColumnCount = 7;
dgvResults.Columns[0].Name = "NHSNumber";
dgvResults.Columns[1].Name = "DOB";
dgvResults.Columns[2].Name = "Title";
dgvResults.Columns[3].Name = "Surname";
dgvResults.Columns[4].Name = "GivenName";
dgvResults.Columns[5].Name = "Gender";
dgvResults.Columns[6].Name = "Problem";

dgvResults.Rows.Clear();

databaseFilename = Application.StartupPath + "\\nhsnumberchecker.db";

System.Data.SQLite.SQLiteConnection m_dbConnection;
m_dbConnection = new System.Data.SQLite.SQLiteConnection("Data Source=" + databaseFilename + ";Version=3;");
m_dbConnection.Open();

sql = @"
SELECT * FROM results
ORDER BY Surname;";

System.Data.SQLite.SQLiteCommand command = new System.Data.SQLite.SQLiteCommand(sql, m_dbConnection);

System.Data.SQLite.SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
dgvResults.Rows.Add(reader["NHSNumber"], reader["DOB"], reader["Title"], reader["Surname"] + ", " + reader["GivenName"],
reader["Gender"], reader["Problem"], NHSnumberError(reader.GetString(0))); //This section is causing the error
}

dgvResults.Refresh();
dgvResults.AutoResizeColumns();
m_dbConnection.Close();
}
}

代码的下半部分是收到错误消息的部分。我不明白它的哪一部分是“超出范围”。

最佳答案

也许您的查询结果包含一个“,”字符。
尝试修改所有条目,添加替换方法以查看是否为真:

dgvResults.Rows.Add(reader["NHSNumber"].Replace("," , "") , ............

关于c# - 为什么我得到 System.IndexOutOfRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57610486/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com