从字符串“ACECATN000001”到“整数”类型的转换无效

时间:2011-12-11 06:44:29

标签: asp.net

我是使用asp.net的新手我收到了“从字符串转换”ACECATN000001“输入'整数'无效的错误。”任何人都可以帮我解决这个问题吗?提前谢谢:D

        If lbl_productcatcode.Text = "ACECATN000001" Then
            txt_productcode.Text = Format(CInt(rdr.Item(0).ToString) + 1, "1000000")

        End If
    End If
    cmd1.Connection.Close()

End Sub

2 个答案:

答案 0 :(得分:1)

你已经发布了一些代码,但没有说出哪一行有错误,我认为是这一行:

cmd1.CommandText = "Select CategoryID from CategoryTable where ProductCategory = '" & DropDownList1.Text & "'"

如果CategoryId上的CategoryTable列的类型为integer,则无法将其与字符串值进行比较。我猜你需要使用绑定到DropdownList的项目的ID,IIRC下拉列表中应该有一个SelectedValue属性,你可以使用它。

cmd1.CommandText = "Select CategoryID from CategoryTable where ProductCategory = " & DropDownList1.SelectedValue 

注意单引号的缺失,因为您将在sql语句中注入int值。

答案 1 :(得分:1)

首先:CInt(s)ss作为字符串)转换为整数,仅在s包含数字时有效(否)字母)。

在这一行:

  txt_productcode.Text = Format(CInt(rdr.Item(0).ToString) + 1, "1000000")

您正在尝试将rdr.Item(0).ToString转换为整数。根据你的代码, rdr.Item(0).ToString返回包含字母的"ACECATN000001",因此无法转换为整数。 你究竟想在那条线上做什么?