Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public void BulkCopy(DataTable dt, string TableName)
        {

            var configurationAppSettings = new System.Configuration.AppSettingsReader();

            using (SqlBulkCopy sqlBulk = new SqlBulkCopy(((string)(configurationAppSettings.GetValue("ConnectionString", typeof(string))))))
            {
                sqlBulk.DestinationTableName = TableName;
                sqlBulk.BulkCopyTimeout = 3000;
                sqlBulk.WriteToServer(dt);
                sqlBulk.Close();

}}
this is bulk copy fuction i used i face an error when adding a column into excel sheet.. i face an error
:The given value of type String from the data source cannot be converted to type bigint of the specified target column
The code i used behind import button is that
Note.. All working fine before i adding a new column..
private bool ImportHours()
{
var result = false;
DataTable dt = new DataTable();
DataSet ds = new DataSet();

var RequiredColumns = new string[] { "SSN", "LASTNAME", "FIRSTNAME", "LEVEL1CODE", "LEVEL1DESCRIPTION", "LEVEL2CODE", "LEVEL2DESCRIPTION", "LEVEL3CODE", "LEVEL3DESCRIPTION", "LEVEL4CODE", "LEVEL4DESCRIPTION", "LEVEL5CODE", "LEVEL5DESCRIPTION", "DATEOFHIRE", "UNIONCODE", "PPE", "HOURS", "GROSSWAGES", "TERMINATIONDATE", "PAYFREQUENCY","STATUS"};

string filename = String.Format("{1}\\{2}{0}", Path.GetExtension(fuCsv.PostedFile.FileName), Server.MapPath("ImportFiles"), DateTime.Now.Ticks);

var strArray2 = new string[] { ".csv", ".xls", ".xlsx" };

if (objImport.ValidateFileExtention(filename, strArray2))
{
fuCsv.SaveAs(filename);

switch (Path.GetExtension(filename))
{

case ".xlsx":
ds = objImport.XlsxReader(filename);

break;
case ".csv":
dt = objImport.GetDataTabletFromCSVFile(filename);
dt.TrimColumnNames();
break;
}

QueryHelper.ExecuteNonQuery("DELETE FROM Tbl_PPACA_Import");
int i = 0, j = 0;
foreach (DataTable dT in ds.Tables)
{

if (dT.GetColumnNames().Length > 0)
{
i++;

dt = dT;
dt.TrimColumnNames();

var aa = string.Join(",", dt.DefaultView.ToTable(false, RequiredColumns).GetColumnNames());
var ab = string.Join(",", RequiredColumns);

if (aa == ab)
{
var Employer_ID = new System.Data.DataColumn("EmployerID", typeof(System.Int32)) { DefaultValue = EmployerID };
var PayrollActivityDateCol = new System.Data.DataColumn("PAYROLLACTIVITYDATE", typeof(System.DateTime)) { DefaultValue = PayrollActivityDate.SelectedDate };
var Change_IP = new DataColumn("ChangeIP") { DefaultValue = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] };
var ModifiedBy = new DataColumn("ModifiedBy", typeof(string)) { DefaultValue = _userInfo.Name };
var UserType = new DataColumn("UserType", typeof(string)) { DefaultValue = _userInfo.UserTypeString };
var ModifiedDate = new DataColumn("ModifiedDate", typeof(DateTime)) { DefaultValue = DateTime.Now };


dt = new DataView(dt).ToTable(false, RequiredColumns);

dt = dt.Select(string.Format("Convert([SSN],'System.String') <> '{0}'", string.Empty)).CopyToDataTable();


foreach (DataRow row in dt.Rows)
{
if (!Methods.isDate(row["DATEOFHIRE"]))
row.SetField("DATEOFHIRE", null as System.DBNull);
if (!Methods.isDate(row["PPE"]))
row.SetField("PPE", null as System.DBNull);
if (!Methods.isDate(row["TERMINATIONDATE"]))
row.SetField("TERMINATIONDATE", null as System.DBNull);
if (!Methods.isDouble(row["HOURS"]))
row["HOURS"] = 0.00;
if (!Methods.isDouble(row["GROSSWAGES"]))
row["GROSSWAGES"] = 0.00;

}

dt.Columns.AddRange(new DataColumn[] { Employer_ID, PayrollActivityDateCol, Change_IP, ModifiedBy, UserType, ModifiedDate });

objImport.BulkCopy(dt, "Tbl_PPACA_Import");

j++;
}
}

}

if (i == j)
result = true;


}
else
{
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = "Invalid File. Please upload a File with extension " + string.Join(",", strArray2);
result = false;
}

return result;
}
Posted
Comments
jdeep84 12-Jun-15 9:04am    
This clearly looks like type mismatch. if you can change SQL table schema to accommodate received value from excel which i suppose is coming as string.

or you can iterate and type cast the values in another DataTable and bulkCopy this new table.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900