无法将参数值从String转换为Int32 visual studio 2008

时间:2013-05-03 08:22:10

标签: sql vb.net visual-studio-2008

错误指向cmd.ExecuteNonQuery()

当我尝试将字段保留为null(主键字段OEID

除外)时发生

错误

任何语法修订建议都将非常感谢

        Dim cmd As SqlCommand = sqlconn.CreateCommand
        sqlconn.Open()

        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "AddOfficeEquipmentProfile"


        cmd.Parameters.Add("@OE_SerialNo", SqlDbType.VarChar, 35, "sgh5960").Value = DBNull.Value
        cmd.Parameters.Add("@OE_PropertyNo", SqlDbType.VarChar, 35, "j7h7h6g6f2").Value = DBNull.Value
        cmd.Parameters.Add("@OE_MacAddress", SqlDbType.VarChar, 100, "j7h7:h6g6f2").Value = DBNull.Value
        cmd.Parameters.Add("@OE_Static_IP", SqlDbType.VarChar, 15, "192.168.1.5").Value = DBNull.Value
        cmd.Parameters.Add("@OE_Vendor", SqlDbType.VarChar, 35, "ADWAYS").Value = DBNull.Value

        cmd.Parameters.Add("@OE_PurchaseDate", SqlDbType.DateTime).Value = DBNull.Value
        cmd.Parameters.Add("@OE_WarrantyInclusiveYear", SqlDbType.Int).Value = DBNull.Value
        cmd.Parameters.Add("@OE_WarrantyStatus", SqlDbType.Char, 2, "IN").Value = DBNull.Value
        cmd.Parameters.Add("@OE_Status", SqlDbType.VarChar, 15, "Good").Value = DBNull.Value
        cmd.Parameters.Add("@OE_Dept_Code", SqlDbType.Char, 3, "ADM").Value = DBNull.Value
        cmd.Parameters.Add("@OE_Location_Code", SqlDbType.Char, 8, "ADM_OFC").Value = DBNull.Value
        cmd.Parameters.Add("@OE_Remarks", SqlDbType.VarChar, 1000, "ACTIVE").Value = DBNull.Value

        cmd.Parameters("@OE_SerialNo").Value = txtSerialNo.Text
        cmd.Parameters("@OE_PropertyNo").Value = txtPropertyNo.Text
        cmd.Parameters("@OE_MacAddress").Value = txtMacAddress.Text
        cmd.Parameters("@OE_Static_IP").Value = txtStaticIP.Text
        cmd.Parameters("@OE_Vendor").Value = txtVendor.Text
        cmd.Parameters("@OE_PurchaseDate").Value = dtpPurchaseDate.Value
        cmd.Parameters("@OE_WarrantyInclusiveYear").Value = txtWarrantyInclusiveYear.Text
        cmd.Parameters("@OE_WarrantyStatus").Value = txtWarrantyStatus.Text
        cmd.Parameters("@OE_Status").Value = txtStatus.Text
        cmd.Parameters("@OE_Dept_Code").Value = cmbDeptCode.Text
        cmd.Parameters("@OE_Location_Code").Value = cmbLocationCode.Text
        cmd.Parameters("@OE_Remarks").Value = txtRemarks.Text
        cmd.ExecuteNonQuery()
        sqlconn.Close()

1 个答案:

答案 0 :(得分:0)

Null和“”之间存在差异。

“”是一个空字符串,您将空字符串分配给数字列,可能是WarrantyInclusiveYear

在进行分配之前,您需要检查列是否为空,因此......

If txtWarrantyInclusiveYear.Text.Trim <> "" Then
   cmd.Parameters("@OE_WarrantyInclusiveYear").Value = txtWarrantyInclusiveYear.Text
End If

但这不是很好的代码,你应该做的是......

Dim nValue As Integer = 0

If Integer.TryParse(txtWarrantyInclusiveYear.Text,nValue) Then
    cmd.Parameters("@OE_WarrantyInclusiveYear").Value = nValue
End If