If we have decimal columns in a file that we need to export to SQL Server, there could be an error at the point of bulk insert if the user has entered varchar values for decimal columns.
- /*
- sbc.NotifyAfter = temp.Rows.Count;
- sbc.WriteToServer(temp); /*Here we can have error regarding type casting.*/
- sbc.Close();
- */
To avoid this error at the point of bulk insert, we can raise a validation error before the execution reaches the final point of bulk insert by either run for loop and check invalid values in decimal columns or by another step as below.
- string sql = " ((Convert([" + strCropColumn1 + "], 'System.Decimal')) * 0) = 0 ";
- string sql1 = " ((Convert([" + strCropColumn2 + "], 'System.Decimal')) * 0) = 0 ";
- try
- {
- DataRow[] drSql1 = dt.Select(sql);
- }catch (Exception)
- {
- strErrorMessage = "Invalid value in \"" + strCropColumn1 + "\"";
- return;
- }
- try
- {
- DataRow[] drSql1 = dt.Select(sql1);
- }
- catch (Exception)
- {
- strErrorMessage = "Invalid value in \"" + strCropColumn2 + "\"";
- return;
- }
Happing coding.

Join the conversation! Your thoughts help the community grow.