tags:

views:

349

answers:

2

I'm using the code below to insert data into a SQL table from a datagrid, the datagrid has 5 rows but only 4 are inserted into the table, the first row is missed off. I've narrowed the problem to updateTable below. Can you spot the problem? After an hour of trying I can't.

private void updateTable(OdbcConnection conn)
{
                int iCols = _DGV.ColumnCount;
                int iRows = _DGV.RowCount;
                int iIndex = 1;

                string strSql = GetInsertStatement(iCols);

                foreach(DataGridViewRow dr in _DGV.Rows)
                {
                    if (!dr.Visible)
                    {
                        continue;
                    }

                    iIndex++;

                    OdbcCommand cmd = new OdbcCommand();
                    for (int j = 0; j <= iCols - 1; j++)
                    {
                        if (_DGV.Columns[j].Visible == false)
                        {
                            continue;
                        }
                    try
                    {
                        conn.Open();
                        cmd.Connection = conn;
                        cmd.CommandText = strSql;
                        cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {

                    }
                    finally
                    {
                        conn.Close();
                    }
                }
}
A: 

Maybe:

int iIndex = 0;
lod3n
iIndex doesn't look like it's used anywhere.
Jonas
I had tried that with no luck, the code isn't mine it belongs to a colleague who is currently off work ill.
Jade M
@Jonas, you're right. How odd.@Jade M, why is he checking .Visible all the time? I'm thinking some other code not shown is hiding something.
lod3n
+1  A: 

Are you sure each row in the grid is visible? IE, at the end of your loop (as it is written), does iIndex == iRows?

Jonas