Reputation: 73
I need to update the DB based on the fields in excel, but i need to check if 5 fields in a row in one column are empty, then stop updating the DB.
foreach (var sheets in workbook.Worksheets)
{
SqlCommand comm = new SqlCommand(query, conn);
comm.CommandType = System.Data.CommandType.Text;
var start = sheets.Dimension.Start;
var end = sheets.Dimension.End;
for (int i = start.Row + 1; i < end.Row; i++)
{
object columnE = sheets.Cells[i, 4].Value;
string aa = columnE.ToString();
if (aa.StartsWith("E") && aa.EndsWith("ETO"))
{
comm.Parameters.AddWithValue("@columnE", columnE);
object columnB = sheets.Cells[i, 1].Value;
comm.Parameters.AddWithValue("@columnB", columnB);
}
else if (string.IsNullOrEmpty(aa) || !aa.StartsWith("E") && !aa.EndsWith("ETO"))
{
conn.Close();
}
comm.ExecuteNonQuery();
comm.Parameters.Clear();
}
}
I have a logic when the first fields is null or empty or not what is intended to be to stop updating, but i can't find a way to check that for 5 fields in a row.
I have found the solution, here it is for the future people.
foreach (var sheets in workbook.Worksheets)
{
conn.Open();
result = sheets.ToString();
SqlCommand comm = new SqlCommand(query, conn);
comm.CommandType = System.Data.CommandType.Text;
var start = sheets.Dimension.Start;
var end = sheets.Dimension.End;
int counter = 0;
for (int i = start.Row + 1; i < end.Row; i++)
{
object columnE = sheets.Cells[i, 5].Value;
string aa = "";
if (columnE != null)
{
aa = columnE.ToString();
}
object columnB = sheets.Cells[i, 2].Value;
if (aa.StartsWith("E") && aa.EndsWith("ETO") && conn.State == System.Data.ConnectionState.Open)
{
comm.Parameters.AddWithValue("@columnE", columnE);
comm.Parameters.AddWithValue("@columnB", columnB);
comm.ExecuteNonQuery();
comm.Parameters.Clear();
}
if (columnE == null)
{
counter++;
}
if (counter == 5)
{
conn.Close();
}
}
}
Upvotes: 0
Views: 56
Reputation: 1162
Just count to 5 where you have true logic otherwise reset the counter to start over.
Int counter =0;
for (){
If (logictrue()){
counter++;
if(counter==5)
Update();
} else
counter=0;
}
Upvotes: 1