Just playing around here and trying to get this to work. I have subroutine (CreateCustomerAdapter) that creates a data adapter with a select command. The Fill works if I call it from the form load but not if I call it from code on a button click (Button2). What am I doing wrong? I get error System.NullReferenceException: Object Reference not set to an instance of an object
Auguy
Sylvania/Toledo Ohio
Code:
Imports MySql.Data.MySqlClient
Public Class Form1
Public dbconn As New MySqlConnection
Public sql As String
Public dbcomm As MySqlCommand
Public command As MySqlCommand
Public dbread As MySqlDataReader
Public ds As DataSet
Public Phys As DataTable
Public adapter As MySqlDataAdapter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbconn = New MySqlConnection("Data Source=localhost ; user id=root ; password=sa7486 ; database=Referrals;")
Try
dbconn.Open()
Catch ex As Exception
MsgBox("Error in connection, please check Database and connection server.")
End Try
Dim adapter As New MySqlDataAdapter
adapter = CreateCustomerAdapter(dbconn)
Dim ds As New DataSet
Dim Phys As New DataTable
adapter.Fill(ds, "Phys")
For Each pRow As DataRow In ds.Tables("Phys").Rows
ListBox1.Items.Add(pRow("LastName") & ", " & pRow("FirstName"))
Next
End Sub
Public Function CreateCustomerAdapter(ByVal connection As MySqlConnection) As MySqlDataAdapter
Dim adapter As MySqlDataAdapter = New MySqlDataAdapter()
' Create the SelectCommand.
Dim command As MySqlCommand = New MySqlCommand( _
"SELECT * FROM Physician", connection)
adapter.SelectCommand = command
Return adapter
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Clear()
Dim ds As New DataSet
Dim Phys As New DataTable
'ds.Clear()
Try
adapter.Fill(ds, "Phys")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
For Each pRow As DataRow In ds.Tables("Phys").Rows
ListBox1.Items.Add(pRow("LastName") & ", " & pRow("FirstName"))
Next
End Sub
Auguy
Sylvania/Toledo Ohio