User1535942433 posted
Hi mansooraabid,
Accroding to your description and codes,I create a test and it works fine.As far as I think,SqlBulkCopy.WriteToServer(DataTable) fails with confusing messages if the column order of the DataTable differs from the column order of the table
definition in your database (when this causes a type or length incompatibility).
I suggest you could try to add like this:
sqlBulkCopy.ColumnMappings.Add("Id", "Id");
sqlBulkCopy.ColumnMappings.Add("num", "num");
sqlBulkCopy.ColumnMappings.Add("total", "total");
More details,you could refer to below codes:
protected void Button1_Click(object sender, EventArgs e)
{
string consString = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Id", typeof(string));
dt.Columns.Add("num", typeof(string));
dt.Columns.Add("total", typeof(int));
dr = dt.NewRow();
dr["Id"] = TextBox1.Text;
dr["num"] = TextBox2.Text;
dr["total"] = Convert.ToInt32(TextBox3.Text);
dt.Rows.Add(dr);
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name
sqlBulkCopy.DestinationTableName = "Test";
//[OPTIONAL]: Map the DataTable columns with that of the database table
sqlBulkCopy.ColumnMappings.Add("Id", "Id");
sqlBulkCopy.ColumnMappings.Add("num", "num");
sqlBulkCopy.ColumnMappings.Add("total", "total");
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
}
}
}
Database:

Besides,I suggest you could post your full codes and database to us.It will help us to solve your problems.
Best regards,
Yijing Sun