# System.InvalidOperationException with SQlBulkCopy

c# invalidoperationexception sqlbulkcopy ### Question

I got the following error when executing bulkcopy.

``````System.InvalidOperationException
The given value of type String from the data source cannot be converted to
type decimal of the specified target column.
``````

I use the following code.

``````DataTable empTable = DataTemplate.GetEmployees();
DataRow row;
for (int i = 0; i < gv.Rows.Count;i++ )
{
row = empTable.NewRow();
string empName = gv.DataKeys[i].Values.ToString(); //first key
string hourSalary = gv.DataKeys[i].Values.ToString(); //second key
row["Emp_Name"] = empName;
row["Hour_Salary"] = Convert.ToDecimal(hourSalary);
row["Advance_amount"] = Convert.ToDecimal(0);
row["Created_Date"] = Convert.ToDateTime(System.DateTime.Now.ToString());
row["Created_By"] = Convert.ToInt64(1);
row["Is_Deleted"] = Convert.ToInt64(0);
empTable.Rows.Add(row);
}
InsertintoEmployees(empTable, "Employee");
``````

My SQL datatypes for the above fields are:

``````Emp_Name nvarchar(50) ,
Hour_Salary numeric(18, 2),
Advance_amount numeric(18, 2),
Created_Date datetime,
Created_By numeric(18, 0),
Is_Deleted numeric(18, 0)
``````

I don't know what I am doing wrong.

### Accepted Answer

Your hourSalary variable is a string that you later try to convert to a Decimal and this fails.

Most probably you have either an empty string value or an invalid value.

Depending on your requirements you must decide what to do with the invalid value. You basically have two options except failing: Store as a default value (0 maybe?) or store as a Null value.

If you want to store a default value then you can try the following when converting the salary:

``````string hourSalary = gv.DataKeys[i].Values.ToString();
double salary = 0;
if (!double.TryParse(hourSalary, out salary))
{
salary = 0; // Set your default value here
}
row["Hour_Salary"] = salary;
``````

This will ensure that you get a valid decimal value stored in your Hour_Salary column.

If you want to store a null value then you must change the code somewhat:

``````string hourSalary = gv.DataKeys[i].Values.ToString();
double salary;
object salaryValue;
if (double.TryParse(hourSalary, out salary))
{
salaryValue = salary;
}
else
{
salaryValue = DBNull.Value; // Store as a null
}
row["Hour_Salary"] = salaryValue;
``````

In this example we do the same TryParse, but if it fails we store DBNull.Value instead of the default 0 value.

Prime Library

### Expression Evaluator

More Projects...
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
{"ft_lang_label":"__label__en","ft_lang_prob":0.60613,"math_prob":0.9007864,"size":2260,"snap":"2019-35-2019-39","text_gpt3_token_len":544,"char_repetition_ratio":0.12943262,"word_repetition_ratio":0.019480519,"special_character_ratio":0.25663716,"punctuation_ratio":0.19906323,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97662157,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-08-24T13:09:43Z\",\"WARC-Record-ID\":\"<urn:uuid:f8267d40-2d89-4ff1-980c-39dda52fcd6a>\",\"Content-Length\":\"18738\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6fc138e6-5230-4309-b08f-2ad675d95c42>\",\"WARC-Concurrent-To\":\"<urn:uuid:44fff4b6-4050-4ada-a625-5d4275fa093d>\",\"WARC-IP-Address\":\"40.83.160.29\",\"WARC-Target-URI\":\"https://sqlbulkcopy-tutorial.net/knowledge-base/2027085/system-invalidoperationexception-with-sqlbulkcopy\",\"WARC-Payload-Digest\":\"sha1:VFXHPDAZEJJ5RVBOR5YPEZPESMFZI5WJ\",\"WARC-Block-Digest\":\"sha1:XIVIIDELE56R7PGDUARD3PSWWQECANZH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-35/CC-MAIN-2019-35_segments_1566027321140.82_warc_CC-MAIN-20190824130424-20190824152424-00352.warc.gz\"}"}