2

Using C#, .net 4, Visual Studio 2010, and SQL Server 2008.

I currently have a table called NewPeriodPareto contains the following columns:

  • PG nvarchar(50)
  • Part nvarchar(50)
  • Sales float
  • LostSales float
  • Pareto6 int
  • p6 image
  • Pareto5 int
  • p5 image
  • Pareto4 int
  • p4 image
  • Pareto3 int
  • p3 image
  • CurrentPareto int
  • pNew Image
  • NewPareto int

The following is my code that try's to fill the table:

private void CreateNewPeriod()
{
        DataSet dsDG = new DataSet();
        DataTable dt = new DataTable();

        dsDG = (DataSet)ParetoGrid.DataSource;
        dt = dsDG.Tables[0];

        string ThetableName = "NewPeriodPareto";
        BulkInsertDataTable(myConn, ThetableName, dt);
}

public static void BulkInsertDataTable(string connectionString, string tableName, DataTable table)
{
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlBulkCopy bulkCopy =
                new SqlBulkCopy
                (
                connection,
                SqlBulkCopyOptions.TableLock |
                SqlBulkCopyOptions.FireTriggers |
                SqlBulkCopyOptions.UseInternalTransaction,
                null
                );

            bulkCopy.DestinationTableName = tableName;
            connection.Open();

            bulkCopy.WriteToServer(table);
            connection.Close();
        }
}

As you can see the code provided attempts to copy the data from my datatable. The datatable is bound to the My data grids source.

The following is the error I get:

@bulkCopy.WriteToServer(table);
The given value of type Int32 from the data source cannot be converted to type image of the specified target column.

Now the first thing I thought is that There was a type set wrong in one of my fields of my SQL table, but after checking many times, there appears to be nothing wrong.

So what I'm after is a solution to this little problem, or even an alternative/simpler way of achieving the same goal, that is to fill an SQL table from a programs DataTable.

5
  • Could you post select from table adapter used to fill data? Commented Apr 30, 2012 at 13:52
  • the datagrid view is made from a stored procedure and added columns for images from code. is this what you mean? Commented Apr 30, 2012 at 13:53
  • Yes. I assumed that you are using table adapters to retrieve data. Does order of columns in grid match table definition? Commented Apr 30, 2012 at 13:55
  • yes, that was one of my first things to look at, the datatypes match as does the column order, this is what is making me confused about the error. Im thinking theres something when the datagridview.source equals the dataset Commented Apr 30, 2012 at 14:01
  • The only difference is that my procedure gets "some of the columns, All the columns except the image columns. Those are generated at runtime. So, has this go to do with the source looking at the data from the query only? Commented Apr 30, 2012 at 14:16

1 Answer 1

1

[Short of time, so i am going to hive you a hint]

you should look into ColumnMappings of the SqlBulkCopy. This allows you to explicitly map the column of your source to the target. There might be some extra columns like an ID or similar that are in the way right now.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.