Validation Error for Invalid Values in Decimal Columns

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. 

  1. /* 
  2. sbc.NotifyAfter = temp.Rows.Count; 
  3. sbc.WriteToServer(temp); /*Here we can have error regarding type casting.*/  
  4. sbc.Close();  
  5.   
  6. */  

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.

  1. string sql = " ((Convert([" + strCropColumn1 + "], 'System.Decimal')) * 0) = 0  ";   
  2.   
  3. string sql1 = " ((Convert([" + strCropColumn2 + "], 'System.Decimal')) * 0) = 0  ";   
  4.   
  5.  try   
  6.   
  7.  {   
  8.   
  9.       DataRow[] drSql1 = dt.Select(sql);   
  10.  }catch (Exception)   
  11.   
  12.  {  
  13.   
  14.     strErrorMessage = "Invalid value in \"" + strCropColumn1 + "\"";   
  15.      return;  
  16.   
  17.   }   
  18.  try   
  19.   
  20.  {   
  21.   
  22.      DataRow[] drSql1 = dt.Select(sql1);   
  23.   
  24.  }   
  25.   
  26.  catch (Exception)   
  27.   
  28.  {   
  29.   
  30.      strErrorMessage = "Invalid value in \"" + strCropColumn2 + "\"";   
  31.   
  32.      return;   
  33.   
  34.  }  
Using the preceding , we have given a restricted execution before it reaches the final bulk insert statement as well as we can give a validation message regarding a false value to the decimal columns.

Happing coding.