请帮助!
当我点击水晶报表中的打印按钮时工作正常但是当我关闭报表以回到我的datagridview时会弹出一个错误 转换类型" DBNull"输入' string'无效
这是我的代码,我可以从数据库中检索数据到我的datagridview
DataGridView1.Rows.Clear()
sql = "SELECT * FROM tblfsesmis"
cmd = New MySqlCommand(sql, con)
Try
con.Open()
adapter = New MySqlDataAdapter(cmd)
adapter.Fill(dt)
For Each row In dt.Rows
Populate(row(0), row(1), row(2), row(3), row(4), row(5), row(6), row(7), row(8), row(9), row(10), row(11))
Next
con.Close()
dt.Rows.Clear()
DataGridView1.Refresh()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
答案 0 :(得分:0)
您可能需要检查DBNull值,然后再将其发送到其他地方:
If IsDBNull(row(x)) Then
value = ""
Else
value = row(x)
End If
此外,您希望使用Using阻止您的连接,因为在这里您只有在有异常时关闭它(这意味着您真的不相信自己......)
以下是一个示例(我不知道您如何创建连接):
DataGridView1.Rows.Clear()
sql = "SELECT * FROM tblfsesmis"
Try
Using con As New SQLConnection()
con.Open()
cmd = New MySqlCommand(sql, con)
adapter = New MySqlDataAdapter(cmd)
adapter.Fill(dt)
For Each row In dt.Rows
'It's in your Populate function that you want to check the DBNull values
Populate(row(0), row(1), row(2), row(3), row(4), row(5), row(6), row(7), row(8), row(9), row(10), row(11))
Next
dt.Rows.Clear()
DataGridView1.Refresh()
End Using 'con Object will be disposed automatically
Catch ex As Exception
MsgBox(ex.Message)
End Try