我还有另外两个按钮,它们的代码几乎完全相同。但是,当我尝试运行此代码以获取所有结果时,出现错误“ 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();
}
}
代码的下部是获取错误消息的部分。我不知道其中的哪一部分“超出范围”。
答案 0 :(得分:-1)
也许您的查询结果包含一个“,”字符。 尝试通过添加替换方法来修改所有条目,以查看是否正确:
WebBrowser1