下面给出代码的导入表单错误

时间:2015-06-11 17:12:45

标签: c# asp.net

我导入的文件工作正常但是当我向表单和数据库添加另一列时,我面临下面给出的错误:

  

错误详细信息:数据源中String类型的给定值无法转换为指定目标列的bigint类型。

按钮导入后面的代码是......

protected void btnImport_Click(object sender, EventArgs e)
        {
            try
            {
                if (ImportHours()) 
                {
                    objImport.ProcessImport(LoginEmployerId);
                    AsyncImport.ProcessAssignmentImport((int)LoginEmployerId);
                    ScriptManager.RegisterStartupScript(Page, GetType(), "Message1", "alert('File has been Imported Successfully.');", true);
                }
            }
            catch (Exception ex)
            {


                lblMsg.Visible = true;
                lblMsg.ForeColor = Color.Red;
                lblMsg.Text = "Error Detail:" + ex.Message.ToString();

            }
        }

        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 > 1)
                    {
                        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 };
                            var Status = new DataColumn("Status", typeof(String)) { DefaultValue ="STATUS"};



                            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;
        }

更新 TABLE架构&gt;&gt;&gt;&gt;

USE [ppacahelper]
GO

/****** Object:  Table [dbo].[Tbl_PPACA_Import]    Script Date: 6/12/2015 9:06:19 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Tbl_PPACA_Import](
    [SSN] [varchar](max) NULL,
    [LASTNAME] [varchar](max) NULL,
    [FIRSTNAME] [varchar](max) NULL,
    [LEVEL1CODE] [varchar](max) NULL,
    [LEVEL1DESCRIPTION] [varchar](max) NULL,
    [LEVEL2CODE] [varchar](max) NULL,
    [LEVEL2DESCRIPTION] [varchar](max) NULL,
    [LEVEL3CODE] [varchar](max) NULL,
    [LEVEL3DESCRIPTION] [varchar](max) NULL,
    [LEVEL4CODE] [varchar](max) NULL,
    [LEVEL4DESCRIPTION] [varchar](max) NULL,
    [LEVEL5CODE] [varchar](max) NULL,
    [LEVEL5DESCRIPTION] [varchar](max) NULL,
    [DATEOFHIRE] [date] NULL,
    [UNIONCODE] [varchar](max) NULL,
    [PPE] [date] NULL,
    [HOURS] [float] NULL,
    [GROSSWAGES] [float] NULL,
    [TerminationDate] [date] NULL,
    [PayFrequency] [varchar](max) NULL,
    [EmployerId] [bigint] NULL,
    [PayrollActivityDate] [datetime] NULL,
    [ChangeIP] [varchar](500) NULL,
    [ModifiedBy] [varchar](500) NULL,
    [UserType] [varchar](500) NULL,
    [ModifiedDate] [datetime] NULL,
    [Status] [varchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

PROCEDURE&GT;&GT;&GT;&GT;

Update Tbl_PPACA_Import set LASTNAME=LTRIM(RTRIM(A.LASTNAME)),FIRSTNAME=LTRIM(RTRIM(A.FIRSTNAME))
            ,LEVEL1CODE=LTRIM(RTRIM(ISNULL(A.LEVEL1CODE,ISNULL((SELECT TOP 1 Location_Code FROM Tbl_Location WHERE Employer_Id=@Employer_Id AND Location like '%'+LTRIM(RTRIM(A.LEVEL1DESCRIPTION))+'%' ),LTRIM(RTRIM(A.LEVEL1DESCRIPTION)))))),
            LEVEL1DESCRIPTION=LTRIM(RTRIM(isnull(A.LEVEL1DESCRIPTION,A.LEVEL1CODE)))
            ,LEVEL2CODE=LTRIM(RTRIM(A.LEVEL2CODE)),LEVEL2DESCRIPTION=LTRIM(RTRIM(A.LEVEL2DESCRIPTION))
            ,LEVEL3CODE=LTRIM(RTRIM(A.LEVEL3CODE)),LEVEL3DESCRIPTION=LTRIM(RTRIM(A.LEVEL3DESCRIPTION))
            ,LEVEL4CODE=LTRIM(RTRIM(A.LEVEL4CODE)),LEVEL4DESCRIPTION=LTRIM(RTRIM(A.LEVEL4DESCRIPTION))
            ,LEVEL5CODE=LTRIM(RTRIM(A.LEVEL5CODE)),LEVEL5DESCRIPTION=LTRIM(RTRIM(A.LEVEL5DESCRIPTION))
            ,UNIONCODE=LTRIM(RTRIM(A.UNIONCODE)),SSN=LTRIM(RTRIM(REPLACE(A.SSN,'-',''))),
            [STATUS]=LTRIM(RTRIM(A.[STATUS]))
            from Tbl_PPACA_Import A

1 个答案:

答案 0 :(得分:0)

而不是Int32Int64

使用EmployerId

例:

var Employer_ID = new System.Data.DataColumn("EmployerID", typeof(System.Int64)) { DefaultValue = EmployerID };

无论您在何处使用EmployerId