how to use sql Bulkcopy to insert datagridview rows in table

c# datagridview sql sqlbulkcopy

Question

i want insert datagridview rows into sql server tables ... and now used this code to insert rows values from DataGridView in table :

private DataTable GetDataTableFromDGV(DataGridView dgv)
        {
            var dt = new DataTable();
            foreach (DataGridViewColumn column in dgv.Columns)
            {
                if (column.Visible)
                {
                    dt.Columns.Add();
                }
            }
            object[] cellValues = new object[dgv.Columns.Count];
            foreach (DataGridViewRow row in dgv.Rows)
            {
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    cellValues[i] = row.Cells[i].Value;
                }
                dt.Rows.Add(cellValues);
            }
            return dt;
        }

        private void InsertDTtoDB(string ConnectionString, string TableName, DataGridView DGV)
        {
            DataTable dt = new DataTable();
            dt = GetDataTableFromDGV(DGV);
            using (SqlConnection cn = new SqlConnection(ConnectionString))
            {
                cn.Open();
                using (SqlBulkCopy copy = new SqlBulkCopy(cn))
                {                        
                    copy.ColumnMappings.Add(0, 1);
                    copy.ColumnMappings.Add(1, 2);
                    copy.ColumnMappings.Add(2, 3);
                    copy.ColumnMappings.Add(3, 4);
                    copy.ColumnMappings.Add(4, 5);
                    copy.DestinationTableName = TableName;
                    copy.WriteToServer(dt);
                }
            }
        }

But have a problem to insert data : empty row (enable adding row) from datatgridview be stored in last tables row ! please help by attention to pics :

enter image description here enter image description here

Accepted Answer

First, some general observations that may help you better understand writing code in C#:

// this line is declaring a new variable named 'dt' 
// and assigning it an instance of a new DataTable
DataTable dt = new DataTable();
// this line is immediately overwriting the new DataTable 
// assigned to 'dt' with the DataTable returned from 'GetDataTableFromDGV'
dt = GetDataTableFromDGV(DGV);

The above should be written to declare the variable and then assign the value:

DataTable dt = GetDataTableFromDGV(DBV);

The following, from GetDataTableFromDGV, will be adding a column to the DataTable only if the column is visible:

foreach (DataGridViewColumn column in dgv.Columns)
{
    if (column.Visible)
    {
        dt.Columns.Add();
    }
} 

The result of the DataTable from above is then used in InsertDTtoDB which clearly is expecting a set number of columns in a pre-defined ordinal position:

copy.ColumnMappings.Add(0, 1);
// etc...

The above is creating a very brittle relationship between the DataTable created in GetDataTableFromDGV which requires the Column to be visible and then consumed in InsertDTtoDB.

Rather than using a brittle ordinal relationship between the DataTable and the SqlBulkCopy.ColumnMappings, use the name of the field to create the DataTable and then map to the SqlBulkCopy. The foreach in `GetDataTableFromDGV', then becomes:

// assumes the columns in the DataGridView are named as follows:
var columns = new[] {"Id", "Invoice_Id", "Software_Id", "Price", "Quantity", "Sum" }
var dt = new DataTable();
foreach (DataGridViewColumn column in dgv.Columns)
{
    // note: this is case-sensitive
    if (columns.Contains(column.Name))
    {
        dt.Columns.Add();
    }
}

And the ColumnMappings in InsertDTtoDB then become:

copy.ColumnMappings.Add("Invoice_Id", "DestinationCol1");
copy.ColumnMappings.Add("Software_Id", "DestinationCol2");
// etc...

When adding the rows to the DataTable in GetDataTableFromDGV, the cellValues variable is declared outside the foreach loop, but the values are continually overwritten in the inner for loop. Rather than adding an array of objects to the DataTable, use the actual column names to build out a DataRow and add the new DataRow to the DataTable.

Taking the above into account, the GetDataTableFromDGV method then becomes:

private DataTable GetDataTableFromDGV(DataGridView dgv)
{
    // use your actual columns names instead of Col1, Col2, etc...
    var columns = new[] {"Invoice_Id", "Software_Id", "Price", "Quantity", "Sum" }
    var dt = new DataTable();
    foreach (DataGridViewColumn column in dgv.Columns)
    {
        if (columns.Contains(column.Name))
        {
            dt.Columns.Add();
        }
    }

    foreach (DataGridViewRow row in dgv.Rows)
    {
        DataRow newRow = dt.NewRow();
        foreach (string columnName in columns)
        {
            newRow[columnName] = row.Cells[columnName].Value
        }

        dt.Rows.Add(newRow);
    }
    return dt;
}

And the InsertDTtoDB method then becomes:

private void InsertDTtoDB(string ConnectionString, string TableName, DataGridView DGV)
{
    DataTable dt = GetDataTableFromDGV(DGV);
    using (SqlConnection cn = new SqlConnection(ConnectionString))
    {
        cn.Open();
        using (SqlBulkCopy copy = new SqlBulkCopy(cn))
        {   
            // update the "DestinationCol[x]" values to the destination column names
            copy.ColumnMappings.Add("Invoice_Id", "DestinationCol1");
            copy.ColumnMappings.Add("Software_Id", "DestinationCol2");
            copy.ColumnMappings.Add("Price", "DestinationCol3");
            copy.ColumnMappings.Add("Quantity", "DestinationCol4");
            copy.ColumnMappings.Add("Sum", "DestinationCol5");
            copy.DestinationTableName = TableName;
            copy.WriteToServer(dt);
        }
    }
}

To answer your question as to why null rows are being added to the database, I suspect it is because there are null values in the DataGridView. Assuming that is the case, then exclude the null values from the DataGridView from being added to the DataTable. I'd key off one of the key columns such as "Invoice_Id"; the GetDataTableFromDGV method then becomes:

private DataTable GetDataTableFromDGV(DataGridView dgv)
{
    // assumes the columns in the DataGridView are named as follows:
    var columns = new[] {"Invoice_Id", "Software_Id", "Price", "Quantity", "Sum" }
    var dt = new DataTable();
    foreach (DataGridViewColumn column in dgv.Columns)
    {
        if (columns.Contains(column.Name))
        {
            dt.Columns.Add();
        }
    }

    foreach (DataGridViewRow row in dgv.Rows)
    {
        // check if the row has a null "Invoice_Id" and exclude
        if(row.Cells["Invoice_Id"] == null
        {
            continue;
        }

        DataRow newRow = dt.NewRow();
        foreach (string columnName in columns)
        {
            newRow[columnName] = row.Cells[columnName].Value
        }

        dt.Rows.Add(newRow);
    }
    return dt;
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why