大学のスタッフの詳細を含むユーザー (tblUsers) のテーブルがあります。選択したモジュールに関連付けられている講師の名前をテキスト ボックスに入力しようとしています。
特定のモジュールに関連付けられているすべての UserID を取得して、ユーザーが講師であるかどうかをテストしています。講師である場合は、ID を ArrayList に追加します。
次に、この配列を反復処理し、現在の ID を通過する各反復中に以下のメソッドを呼び出します。
ただし、以下のメソッドを見ると、私は SqlDataReader を使用しており、次の行で読み取り中にエラーが発生しています。
txtLecturerName.Text += myReader["First_Name"].ToString();
エラー メッセージ: 'myReader["First_Name"]' が 'System.IndexOutOfRangeException' 型の例外をスローしました
私が使用しているテーブル レイアウトは、メソッド コードの下にあります。これについての助けがあれば大歓迎です。私は一杯のコーヒーで頭を画面から遠ざけることができます。
public void outputLecturerNames(string lecturerID)
{
// Create a new Connection object using the connection string
SqlConnection myConnection = new SqlConnection(conStr);
// If the connection is already open - close it
if (myConnection.State == ConnectionState.Open)
{
myConnection.Close();
}
// 'using' block allows the database connection to be closed
// first and then the exception handling code is triggered.
// This is a better approach than using a 'finally' block which
// would close the connection after the exception has been handled.
using (myConnection)
{
try
{
// Open connection to DB
myConnection.Open();
SqlCommand selectCommand = new SqlCommand(selectQuery, myConnection);
// Declare a new DataReader
SqlDataReader myReader;
selectQuery = "SELECT * FROM tblUsers WHERE User_ID='";
selectQuery += lecturerID + "'";
myReader = selectCommand.ExecuteReader();
while (myReader.Read())
{
txtLecturerName.Text += myReader["First_Name"].ToString();
txtLecturerName.Text += " ";
txtLecturerName.Text += myReader["Last_Name"].ToString();
txtLecturerName.Text += " , ";
}
myReader.Close();
}
catch (Exception err)
{
Console.WriteLine("Error: " + err);
}
}
}
tblUsers:
[User_ID][First_Name][Last_Name][Email_Address]