c# - The given value of type String from the data source cannot be converted to type varbinary of the specified target column -
i have datatable 1 column has image file path. need replace file path byte array of image. below code gives me error.
the given value of type string data source cannot converted type varbinary of specified target column.
for (int rowincrement = 0; rowincrement < dt.rows.count; rowincrement++) { byte[] photo = getphoto(dt.rows[rowincrement]["image"]); dt.rows[rowincrement]["image"] = photo; dt.acceptchanges(); } using (sqlbulkcopy copy = new sqlbulkcopy(sqlcon, sqlbulkcopyoptions.keepnulls | sqlbulkcopyoptions.keepidentity, null)) { copy.destinationtablename = "userdetails"; copy.writetoserver(dt); }
the column type should specified otherwise columns value still string when need byte[]
example:
// add new column since cannot change type dt.columns.add("image_tmp", typeof (byte[])); (int rowincrement = 0; rowincrement < dt.rows.count; rowincrement++) { byte[] photo = getphoto(dt.rows[rowincrement]["image"]); dt.rows[rowincrement]["image_tmp"] = photo; dt.acceptchanges(); } // remove image column , rename temporary column dt.columns.remove("image"); dt.columns["image_tmp"].columnname = "image";
Comments
Post a Comment