繁体   English   中英

附加信息:无法将“System.Int32”类型的对象转换为“System.String”类型。 - VB.NET

[英]Additional information: Unable to cast object of type 'System.Int32' to type 'System.String'. - VB.NET

我有这个错误,

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

代码运行正常,但重建后,代码不再工作

附加信息:无法将“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

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

这个片段应该可以解决这个问题。

    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 文档

解决了,谢谢 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM