【问题标题】:Conversion from string "ACECATN000001" to type 'Integer' is not valid从字符串“ACECATN000001”到类型“整数”的转换无效
【发布时间】:2011-12-19 09:09:03
【问题描述】:

我是使用 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

【问题讨论】:

  • 字符串是字母数字......整数是数字......你想用那个字符串做什么?您不能将字母数字转换为数字。

标签: asp.net


【解决方案1】:

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

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

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

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

请注意没有单引号,因为您将在 sql 语句中注入 int 值。

【讨论】:

    【解决方案2】:

    首先:CInt(s)ss 是一个字符串)转换为整数,并且仅当s 包含数字(无字母)时才有效。

    在这一行:

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

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

    【讨论】:

    • 我想显示一个自动生成的 ACECATNOOOOO1 id ... 场景是这样的,下拉列表搜索产品类别的类别 id,当生成类别 id 时生成该类别的产品代码太...就像类别自动生成类别ID和类别ID生成产品ID???你有什么想法吗..我试图使用计数但我收到错误...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多