【问题标题】:Additional information: Unable to cast object of type 'System.Int32' to type 'System.String'. - VB.NET附加信息:无法将“System.Int32”类型的对象转换为“System.String”类型。 - VB.NET
【发布时间】:2020-02-11 15:04:45
【问题描述】:

我有这个错误,

无法将“System.Int32”类型的对象转换为“System.String”类型。

代码可以运行,但是重建后代码不再运行

我的代码

Private Sub KodePembelian()
    Dim Rd As SqlClient.SqlDataReader
    Dim Cmd As SqlClient.SqlCommand
    Dim bulan As String
    bulan = Format(Now, "MM")
    Dim tahun As String
    tahun = Format(Now, "yy")
    Dim Urutan As String
    Dim Hitung, Cari As String

    Cmd = New SqlClient.SqlCommand("SELECT * FROM BY_PEMBELIANHEADER WHERE NOPEMBELIAN IN " & "(SELECT MAX (NOPEMBELIAN) FROM BY_PEMBELIANHEADER)", MyConn)
    Rd = Cmd.ExecuteReader
    Rd.Read()
    If Not Rd.HasRows Then
        Urutan = "PO/" & bulan & tahun & "/" & "000001"
    Else
        Cari = Microsoft.VisualBasic.Right(Rd.GetString(0), 6)
        If Microsoft.VisualBasic.Left(Rd.GetString(0), 8) <> "PO/" & bulan & tahun & "/" Then
            Urutan = "PO/" & bulan & tahun & "/" & "000001"
        Else
            Hitung = Microsoft.VisualBasic.Right(Rd.GetString(0), 6) + 1
            Urutan = "PO/" & bulan & tahun & "/" & Microsoft.VisualBasic.Right("000000" & Hitung, 6)
        End If
    End If

    Rd.Close()
    txtpo.SelectedText = Urutan
End Sub

【问题讨论】:

  • 你不应该在调用 read 之前检查阅读器是否有行吗?
  • 如果您使用SELECT *,则可以按任何顺序返回列。如果列顺序很重要,请列出列名而不是使用*

标签: vb.net


【解决方案1】:

已解决,谢谢 alireza 先生,我只是在我的代码中添加 getsqlvalue(0).tostring

 Private Sub KodePembelian()
        Dim Rd As SqlClient.SqlDataReader
        Dim Cmd As SqlClient.SqlCommand
        Dim bulan As String
        bulan = Format(Now, "MM")
        Dim tahun As String
        tahun = Format(Now, "yy")
        Dim Urutan As String
        Dim Hitung, Cari As String

        Cmd = New SqlClient.SqlCommand("SELECT * FROM BY_PEMBELIANHEADER WHERE NOPEMBELIAN IN " & "(SELECT MAX (NOPEMBELIAN) FROM BY_PEMBELIANHEADER)", MyConn)
        Rd = Cmd.ExecuteReader
        Rd.Read()
        If Not Rd.HasRows Then
            Urutan = "PO/" & bulan & tahun & "/" & "000001"
        Else
            Cari = Microsoft.VisualBasic.Right(Rd.GetSqlValue(0).ToString(), 6)
            If Microsoft.VisualBasic.Left(Rd.GetSqlValue(0).ToString(), 8) <> "PO/" & bulan & tahun & "/" Then
                Urutan = "PO/" & bulan & tahun & "/" & "000001"
            Else
                Hitung = Microsoft.VisualBasic.Right(Rd.GetSqlValue(0).ToString(), 6) + 1
                Urutan = "PO/" & bulan & tahun & "/" & Microsoft.VisualBasic.Right("000000" & Hitung, 6)
            End If
        End If

        Rd.Close()
        txtpo.SelectedText = Urutan
    End Sub

【讨论】:

    【解决方案2】:

    可能返回值为空。在调用GetString 方法之前,您必须调用IsDBNull 来检查空值。

    这个 sn-p 应该可以解决问题。

        Rd = Cmd.ExecuteReader
        Rd.Read()
        If Not Rd.HasRows Then
            Urutan = "PO/" & bulan & tahun & "/" & "000001"
        Else
            If Rd.IsDBNull(0) Then
                Cari = "default" ' do something if its null
            Else
                Cari =  Microsoft.VisualBasic.Right(Rd.GetSqlValue(0).ToString(), 6)
            End If 
    

    注意:GetString 方法不进行任何转换;因此,检索到的数据必须已经是一个字符串。

    sqldatareader.getstring documentation

    【讨论】:

    • 我猜由于异常 Unable to cast object of type 'System.Int32' to type 'System.String'. 声明它是一个整数,所以 OP 没有得到 dbnull。也许GetFieldTypeGetSqlValue 的组合会起作用
    • 我同意,在这里使用 GetSqlValue 是一个更好的选择。我更新了答案。
    猜你喜欢
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 2013-11-04
    • 2021-10-25
    • 1970-01-01
    相关资源
    最近更新 更多