将nvarchar值'p'转换为数据类型int时转换失败

时间:2018-05-16 11:54:25

标签: asp.net vb.net asp.net-web-api

我有两个表是FixedAssetMaster和ScanningData表。单击“更新”按钮时,它会自动将数据保存到两个表中。当我将数据保存到ScanningTable时,没有问题。但是当我想将数据保存到FixedAssetMaster表时,错误就出来了。目前我将UNIT_NO设置为nvarchar。当我将其更改为int时没有问题。这是我的源代码。 UNIT_NO数据样本是002 保存到扫描数据:

cmd = New SqlCommand
cmd.CommandText = "SELECT COUNT(*) FROM ScanningData WHERE ASSET_NUMBER=" & AssetTxt.Text & " AND LOC_DEPT=" & DeptCodeTxt.Text & " AND UNIT_NO='" & UnitNoTxt.Text & "';"
         cmd.Connection = Conn
    rdmysql = cmd.ExecuteReader
    If rdmysql.Read = True Then
        reccount = Val(rdmysql.GetInt32(0))
    End If
    cmd.Dispose()
    rdmysql.Close()

    If reccount = 0 Then
        strsql = "INSERT INTO ScanningData"
        strsql += "(ASSET_NUMBER,LOC_DEPT,DEPT_DESCRIPTION,MAJOR_CATEGORY,DATE_PLACED_IN_SERVICE,LOC_AREA,IT_TAG_NO,UNITS_ASSIGNED,DESCRIPTION,MYR_COST_BY_UNIT,COST_BY_UNIT,DEPRN_RESERVE_BY_UNIT,DATE_RETIRED,ASSET_KEY_SEGMENT3,UNIT_NO,SERIAL_NUMBER,NBV_BY_UNIT_USD,UPDATE_DATE,UPDATE_BY) VALUES "
        strsql += "(@ID, @CODE, @DEPT, @CATEGORY, @SERVICEDATE, @AREA, @TAG, @UNIT, @DESCRIPTION, @MYRCOST, @USDCOST, @DEPR, @RETIREDDATE, @LocDesc, @UnitNo, @SN, @NBV, @UPDATEDATE, @NAME);"
        cmd = New SqlCommand
        cmd.Connection = Conn
        cmd.Parameters.AddWithValue("@ID", AssetTxt.Text)
        cmd.Parameters.AddWithValue("@CODE", DeptCodeTxt.Text)
        cmd.Parameters.AddWithValue("@DEPT", DeptTxt.Text)
        cmd.Parameters.AddWithValue("@CATEGORY", CategoryTxt.Text)
        cmd.Parameters.AddWithValue("@SERVICEDATE", ServiceTxt.Text)
        cmd.Parameters.AddWithValue("@AREA", LocTxt.Text)
        cmd.Parameters.AddWithValue("@TAG", TagTxt.Text)
        Dim unit As Integer = Integer.Parse(UnitTxt.Text)
        cmd.Parameters.AddWithValue("@UNIT", unit)
         cmd.Parameters.AddWithValue("@DESCRIPTION", DescTxt.Text)
        Dim MYRCost As Decimal = Decimal.Parse(CostMYRTxt.Text)
        cmd.Parameters.AddWithValue("@MYRCOST", MYRCost)
        Dim USDCost As Decimal = Decimal.Parse(CostUSDTxt.Text)
        cmd.Parameters.AddWithValue("@USDCOST", USDCost)
         Dim Depr As Decimal = Decimal.Parse(DeprTxt.Text)
        cmd.Parameters.AddWithValue("@DEPR", Depr)
        cmd.Parameters.AddWithValue("@RETIREDDATE", RetiredDateTxt.Text)
        cmd.Parameters.AddWithValue("@LocDesc", LocDescTxt.Text)
        cmd.Parameters.AddWithValue("@UnitNo", UnitNoTxt.Text)
        cmd.Parameters.AddWithValue("@SN", SNTxt.Text)
        cmd.Parameters.AddWithValue("@NBV", NBVUSDTxt.Text)
        cmd.Parameters.AddWithValue("@UPDATEDATE", ServerDate)
        cmd.Parameters.AddWithValue("@NAME", UserIDLabel.Text)
        cmd.CommandText = strsql
        cmd.ExecuteNonQuery()
    End If

保存到FixedAssetMaster:

 cmd = New SqlCommand
    'cmd.CommandText = "SELECT COUNT(*) FROM ScanningData WHERE ASSET_NUMBER=" & AssetTxt.Text & " AND LOC_DEPT=" & DeptCodeTxt.Text & " and UPDATE_DATE='" & NEWYEAR & "';"
    cmd.CommandText = "SELECT COUNT(*) FROM FixedAssetMaster WHERE ASSET_NUMBER=" & AssetTxt.Text & " AND LOC_DEPT=" & DeptCodeTxt.Text & " AND UNIT_NO='" & UnitNoTxt.Text & "';"
    'cmd.CommandText = "SELECT COUNT(*) FROM ScanningData WHERE ASSET_NUMBER=" & AssetTxt.Text & " AND LOC_DEPT='" & DeptCodeTxt.Text & "';"
    ' cmd.CommandText = "SELECT COUNT(*) FROM ScanningData WHERE ASSET_NUMBER=" & AssetTxt.Text & " AND LOC_DEPT=" & DeptCodeTxt.Text & " AND UPDATE_DATE BETWEEN'" & StartDate & "' AND '" & EndDate & "';"
    cmd.Connection = Conn
    rdmysql = cmd.ExecuteReader
    If rdmysql.Read = True Then
        reccount = Val(rdmysql.GetInt32(0))
    End If
    cmd.Dispose()
    rdmysql.Close()

    If reccount = 0 Then
        strsql3 = "INSERT INTO FixedAssetMaster "
        strsql3 += "(ASSET_NUMBER,DESCRIPTION,MAJOR_CATEGORY,UNITS_ASSIGNED,LOC_DEPT,DEPT_DESCRIPTION,LOC_AREA,ASSET_KEY_SEGMENT3,SERIAL_NUMBER,DATE_PLACED_IN_SERVICE,COST_BY_UNIT,MYR_COST_BY_UNIT,DATE_RETIRED,DEPRN_RESERVE_BY_UNIT,UNIT_NO,UPDATE_DATE,UPDATE_BY,NBV_BY_UNIT_USD,IT_TAG_NO) VALUES "
        strsql3 += "(@ID, @DESCRIPTION, @CATEGORY, @UNIT, @CODE, @DEPT, @AREA, @LocDesc, @SN, @SERVICEDATE, @USDCOST, @MYRCOST, @RETIREDDATE, @DEPR, @UnitNo, @UPDATEDATE, @NAME, @NBV, @TAG);"
        cmd = New SqlCommand
        cmd.Connection = Conn
        cmd.Parameters.AddWithValue("@ID", AssetTxt.Text)
        cmd.Parameters.AddWithValue("@DESCRIPTION", DescTxt.Text)
        cmd.Parameters.AddWithValue("@CATEGORY", CategoryTxt.Text)
        Dim unit As Integer = Integer.Parse(UnitTxt.Text)
        cmd.Parameters.AddWithValue("@UNIT", unit)
        cmd.Parameters.AddWithValue("@CODE", DeptCodeTxt.Text)
        cmd.Parameters.AddWithValue("@DEPT", DeptTxt.Text)
        cmd.Parameters.AddWithValue("@AREA", LocTxt.Text)
        cmd.Parameters.AddWithValue("@LocDesc", LocDescTxt.Text)
        cmd.Parameters.AddWithValue("@SN", SNTxt.Text)
        cmd.Parameters.AddWithValue("@SERVICEDATE", ServiceTxt.Text)
        Dim USDCost As Decimal = Decimal.Parse(CostUSDTxt.Text)
        cmd.Parameters.AddWithValue("@USDCOST", USDCost)
        Dim MYRCost As Decimal = Decimal.Parse(CostMYRTxt.Text)
        cmd.Parameters.AddWithValue("@MYRCOST", MYRCost)
        cmd.Parameters.AddWithValue("@RETIREDDATE", RetiredDateTxt.Text)
        Dim Depr As Decimal = Decimal.Parse(DeprTxt.Text)
        cmd.Parameters.AddWithValue("@DEPR", Depr)
        cmd.Parameters.AddWithValue("@UnitNo", UnitNoTxt.Text)
        cmd.Parameters.AddWithValue("@UPDATEDATE", ServerDate)
        cmd.Parameters.AddWithValue("@NAME", UserIDLabel.Text)
        cmd.Parameters.AddWithValue("@NBV", NBVUSDTxt.Text)
        cmd.Parameters.AddWithValue("@TAG", TagTxt.Text)
        cmd.CommandText = strsql3
        cmd.ExecuteNonQuery()
    End If

0 个答案:

没有答案