vb.net DBNull 到字符串错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20018609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:51:26  来源:igfitidea点击:

vb.net DBNull to string error

vb.netvisual-studio-2010

提问by user1532468

I have a sub which just adds the values from a access db to textboxes. However, some of the fields in the db contain null values or are empty and this is causing vb to throw an error, 'Cannot convert from DBNull to string'.

我有一个 sub 只是将访问数据库中的值添加到文本框。但是,db 中的某些字段包含空值或为空,这会导致 vb 抛出错误“无法从 DBNull 转换为字符串”。

How can I fix this based on my posted code or if someone could post a tutorial for this type of event, as I am a new user struggling to make sense of this. Many thanks

我如何根据我发布的代码解决这个问题,或者如果有人可以发布此类事件的教程,因为我是一个新用户,正在努力理解这一点。非常感谢

Sub add()
        While dr.Read()
            txtname.Text = dr(0).ToString()
            txtfathername.Text = dr(1).ToString()
            txtaddress.Text = dr(2).ToString()
            txtemail.Text = dr(3).ToString()
        End While

    End Sub

UPDATE:

更新:

Sub filllistview()
        Try
            'creatconn()
            cn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("Select * from Postings", cn)
            dr = cmd.ExecuteReader()
            While dr.Read()
                ListView1.Items.Add(dr(0).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(2))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(3))

            End While

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        Finally
            dr.Close()
            cn.Close()
        End Try
    End Sub

Sub showcontectsinlistview()
        Try
            'creatconn()
            cn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("select * from Postings where [Code]='" & ListView1.Text & "'", cn)
            dr = cmd.ExecuteReader()
            add()

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        Finally
            dr.Close()
            cn.Close()
        End Try
    End Sub

enter image description here

在此处输入图片说明

采纳答案by Malcolm Salvador

Detect if the data coming from the row is empty first, then replace it with other values with will make it into a string, then you can pass it onto your textbox

首先检测来自行的数据是否为空,然后将其替换为其他值将其变成字符串,然后您可以将其传递到您的文本框

Use the nothing function:

使用无功能:

dim x as string
if dr(0).value = nothing then
    x = "Null data"
else
    x = dr(0).value
end if

It seems the problems stems on the part when you read the data from the database. can you try changing your query to not output null values?

当您从数据库中读取数据时,问题似乎出在部分上。您可以尝试将查询更改为不输出空值吗?

something like

就像是

   SELECT Field
   FROM Table
   WHERE Not(Field) is null;

回答by Merijn Den Houting

I think you should just check if the values are not NULL

我认为您应该检查值是否不为 NULL

Sub add()
    While dr.Read()
        If dr(0) Then txtname.Text = dr(0).ToString()
        If dr(1) Then txtfathername.Text = dr(1).ToString()
        If dr(2) Then txtaddress.Text = dr(2).ToString()
        If dr(3) Then txtemail.Text = dr(3).ToString()
    End While
End Sub

There probably is a better way though...

虽然可能有更好的方法......

回答by Ric

You will need to check if the value IsDBNullfirst like so

您需要先检查该值是否IsDBNull像这样

Dim value As String = If(dr.IsDBNull(dr(0)), "", dr(0).ToString())

then use the value

然后使用 value

you can put this logic into a method and do the return from there.

您可以将此逻辑放入一个方法中并从那里返回。

or in your method:

或在您的方法中:

Sub add()
    While dr.Read()
        If dr(0) Then txtname.Text = If(dr.IsDBNull(dr(0)), "", dr(0).ToString())
        If dr(1) Then txtfathername.Text = If(dr.IsDBNull(dr(1)), "", dr(1).ToString())
        If dr(2) Then txtaddress.Text = If(dr.IsDBNull(dr(2)), "", dr(2).ToString())
        If dr(3) Then txtemail.Text = If(dr.IsDBNull(dr(3)), "", dr(3).ToString())
    End While
End Sub

SqlDataReader.IsDBNull

SqlDataReader.IsDBNull

also check other SO answers: SQL Data Reader - handling Null column values

还要检查其他 SO 答案:SQL Data Reader - processing Null column values

.Add(dr(0).ToString())

becomes:

变成:

.Add(If(dr.IsDBNull(dr(0)), "", dr(0).ToString()))