I am trying to move on to the next record in the DataReader if a condition is not met. I am aware that DataReaders are foward-only, so what I am trying to achieve is; while data read, move on to the next record if column x has a null value;
using (OleDbDataReader dr = cmd.ExecuteReader())
{
try
{
while (dr.Read())
{
if (!dr.IsDBNull(0))
{
ID = dr.GetString(0).ToString();
}
if (!dr.IsDBNull(1))
{
REFERENCE = dr.GetString(1);
}
else
{
//this is where I need it to 'abandon' the current record and move on to the next...
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
dr.Close();
dr.Dispose();
con.Close();
}
}
I have only put in the part of the code which is relevant so that it's short and straight to the point. Any guidance would be greatly appreciated.
To skip the current record in the
DataReaderand move on to the next record if a condition is not met, you can use thecontinuestatement. Thecontinuestatement allows you to skip the remaining code within the current iteration of the loop and move on to the next iteration. Here's how you can modify your code:By using
continue, when theIsDBNullcondition for column 1 is met, the code will skip the remaining code within the current iteration of the loop and move on to the next iteration, effectively moving to the next record in theDataReader.