其他信息:无法将“System.Int32”类型的对象强制转换为“System.String”类型VB.NET

其他信息:无法将“System.Int32”类型的对象强制转换为“System.String”类型VB.NET,vb.net,Vb.net,我有这个错误 无法将“System.Int32”类型的对象强制转换为“System.String”类型 代码正在工作,但重建后,代码不再工作 我的代码 私人分科德普兰酒店 Dim Rd作为SqlClient.SqlDataReader Dim Cmd作为SqlClient.SqlCommand 暗不蓝如弦 布伦=现在,毫米 朦胧的大浑如弦 tahun=now,yy 暗乌鲁坦线 Dim Hitung,Cari作为字符串 Cmd=New SqlClient.SqlCommandSELECT*FRO

我有这个错误

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

代码正在工作,但重建后,代码不再工作

我的代码

私人分科德普兰酒店 Dim Rd作为SqlClient.SqlDataReader Dim Cmd作为SqlClient.SqlCommand 暗不蓝如弦 布伦=现在,毫米 朦胧的大浑如弦 tahun=now,yy 暗乌鲁坦线 Dim Hitung,Cari作为字符串 Cmd=New SqlClient.SqlCommandSELECT*FROM_pembelinaheader,其中nopembellian IN&选择MAX nopembellian FROM_pembelinaheader,MyConn Rd=Cmd.ExecuteReader 里德 如果不是Rd.HasRows,那么 Urutan=PO/&bulan&tahun&/&000001 其他的 Cari=Microsoft.VisualBasic.RightRd.GetString0,6 如果Microsoft.VisualBasic.LeftRd.GetString0,8 PO/&bulan&tahun&/Then Urutan=PO/&bulan&tahun&/&000001 其他的 Hitung=Microsoft.VisualBasic.RightRd.GetString0,6+1 Urutan=PO/&bulan&tahun&/&Microsoft.VisualBasic.Right000000&Hitung,6 如果结束 如果结束 道路封闭 txtpo.SelectedText=Urutan 端接头
返回值可能为空。在调用GetString方法之前,必须调用IsDBNull检查空值

这段代码应该可以解决这个问题

Rd=Cmd.ExecuteReader 里德 如果不是Rd.HasRows,那么 Urutan=PO/&bulan&tahun&/&000001 其他的 如果Rd.IsDBNull0,则 Cari=default“如果为空,则执行某些操作” 其他的 Cari=Microsoft.VisualBasic.RightRd.GetSqlValue0.ToString,6 如果结束 注意:GetString方法不进行任何转换;因此,检索到的数据必须已经是字符串


谢谢alireza先生,我只是在代码中添加了getsqlvalue0.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

在调用read之前,是否应该检查读取器是否有行?如果使用SELECT*,则可以按任意顺序返回列。如果列顺序很重要,请列出列名而不是使用*。我想这是因为异常无法将“System.Int32”类型的对象强制转换为“System.String”类型。表示它是一个整数,则OP不会获取dbnull。也许和的组合会起作用,我同意,在这里使用GetSqlValue是一个更好的选择。我更新了答案。