0

I am trying to use OleDbDataReader but it is returning this error:

No data exists for the row column

I am using an Access database. Can anyone help me identify the source of this error? Here is my code:

private void UpdateStudent_Load(object sender, EventArgs e)
{
    OleDbDataAdapter da = new OleDbDataAdapter();

    da.SelectCommand = new OleDbCommand(
        "select * from Students where SID= " + U_ID, con);

    con.Open();
    OleDbDataReader rd = da.SelectCommand.ExecuteReader();

    if (rd.Read())
    {
        //txtId.Text = U_ID;
        txtId.Text = rd["SID"].ToString();
    }

    rd.Close();
}
DavidRR
  • 18,291
  • 25
  • 109
  • 191
user1587902
  • 1
  • 1
  • 1
  • 2
    Consider implementing `using()` statements and try/catch. Also, you may want to change your code to `if(rd.HasRows) { while(rd.Read()) { do stuff } }`. Those suggestions dont address your actual question though – Dan Aug 09 '12 at 14:50

1 Answers1

0

You can try with this code

var queryString = "select * from Students where SID= " + U_ID, con;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
    OleDbCommand command = new OleDbCommand(queryString, connection);
    connection.Open();
    OleDbDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        txtId.Text = reader["SID"].ToString();
    }
    reader.Close();
}
Zze
  • 18,229
  • 13
  • 85
  • 118
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51