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();
}
4

1 回答 1

0

您可以尝试使用此代码

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();
}
于 2012-08-09T14:52:08.953 回答