如何使用VB.net处理未处理的NullReference异常

时间:2015-09-07 01:19:34

标签: vb.net

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Try

        connn_start()

        Dim Query As String
        Query = "Select * from abc.abc_list"

        Dim dbcomm = New MySqlCommand(Query, dbconn)
        While dbread.Read 'error is throwing at this point
            Dim DB_VName = dbread.GetString("abc_name")
            cb_V_Name.Items.Add(DB_Name)
        End While

        Connn_stop()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)

    Finally
        dbconn.Dispose()
    End Try

如何处理此异常?

1 个答案:

答案 0 :(得分:0)

在阅读之前添加:

SqlDataReader dbread=dbcomm.ExecuteReader();
if (reader.HasRows)
{
  while (reader.Read())
  {
    ...
  }
}