title: Need help getting MAX ID and filling in data and error on ExecuteReader
body: <p>Ok, so basically what I am trying to accomplish is to get the MAX ID + 1 and display it in the text box and let the user input first name and last name and should be inserted into the database. Currently, I am in the middle of implementing DataAccess.vb as <code>Dim da As DataAccess</code> to be used in CustomerForm.vb. Editing customer works fine, the data is being filled in the box and succeeds. However, when Adding customer it does not fill in the box when trying to get the max Id + 1. I get an error when debugging: </p>
  
  <blockquote>
    <p>System.InvalidOperationException: 'ExecuteReader: Connection property has not been initialized.'</p>
  </blockquote>
  
  <p>I did try looking online to fix this issue but, and mention that the SqlCommand is not correct. I am getting this error on DataAccess.vb on line <code>objDataTable.Load(objSqlCommand.ExecuteReader())</code> . Also would like to note that if editMode is true or false, they both open up CustomerForm.vb</p>
  
  <p><strong>DataAccess.vb</strong></p>
  
  <pre><code>Imports System
  Imports System.Data
  Imports System.Data.SqlClient
  Imports System.Data.SqlTypes
  Imports System.Data.OleDb
  Imports System.Configuration
  Imports System.Linq
  Imports System.Text
  Imports System.Windows.Forms
  Imports System.Collections
  
  Public Class DataAccess
      Private connectionString As String
      Private objConnection As SqlConnection
      Public objSqlCommand As SqlCommand = New SqlCommand()
      Private commandText As String
      Public objDataTable As DataTable = New DataTable()
  
      Public ReadOnly Property Connection As SqlConnection
          Get
              Return Me.objConnection
          End Get
      End Property
  
      Public Sub New()
          connectionString = ("server=CASHAMERICA; Trusted_Connection=yes; database=ProductsDatabase2; connection timeout=30")
      End Sub
  
      Public Sub OpenDBConnection()
          objConnection = New SqlConnection(connectionString)
          objConnection.Open()
      End Sub
  
      Public Sub Parameters(ByVal name As String, ByVal val As Decimal)
          objSqlCommand.Parameters.AddWithValue(name, val)
      End Sub
  
      Public Sub Parameters(ByVal name As String, ByVal val As String)
          objSqlCommand.Parameters.AddWithValue(name, val)
      End Sub
  
      Public Sub Parameters(ByVal name As String, ByVal val As Boolean)
          objSqlCommand.Parameters.AddWithValue(name, val)
      End Sub
  
      Public Sub CreateCommandObject()
          objSqlCommand = objConnection.CreateCommand()
          objSqlCommand.CommandText = commandText
          objSqlCommand.CommandType = CommandType.Text
      End Sub
  
      Public Function FillDataTable() As DataTable
          objDataTable = New DataTable()
          objSqlCommand.CommandText = commandText
          objSqlCommand.Connection = objConnection
          objSqlCommand.CommandType = CommandType.Text
          objDataTable.Load(objSqlCommand.ExecuteReader()) //issue here
          objConnection.Close()
          Return objDataTable
      End Function
  
      Public Sub CloseConnection()
          If objConnection IsNot Nothing Then objConnection.Close()
      End Sub
  
  
  End Class
  </code></pre>
  
  <p><strong>CustomerForm.vb</strong></p>
  
  <pre><code>Imports System.Data.SqlClient
  
      Public Class CustomerForm
      Private objConnection As SqlConnection
      Dim da As DataAccess = New DataAccess()
      Public objDataTable As DataTable = New DataTable()
      Public objSqlCommand As SqlCommand = New SqlCommand()
      Public editMode As Boolean = False
      Public id As String = ""
      Public FName As String = ""
      Public LName As String = ""
      Public PhNumber As String = ""
  
      Public ReadOnly Property Connection As SqlConnection
          Get
              Return Me.objConnection
          End Get
      End Property
  
      Dim CS As String = ("server=CASHAMERICA;Trusted_Connection=yes;database=ProductsDatabase2; connection timeout=30")
  
      Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
  
          Try
              If editMode = False Then
                  If txtFirstName.Text = "" Then
                      MessageBox.Show("First Name Is required")
                  ElseIf txtLastName.Text = "" Then
                      MessageBox.Show("Last Name Is required")
                  Else
                      Dim con As New SqlConnection(CS)
                      con.Open()
                      Dim cmd As New SqlCommand("INSERT INTO Customers(FIrstName,LastName,PhoneNumber) VALUES(@FName, @LName, @PhNumber)", con)
                      cmd.Parameters.AddWithValue("@FName", txtFirstName.Text)
                      cmd.Parameters.AddWithValue("@LName", txtLastName.Text)
                      cmd.Parameters.AddWithValue("@PhNumber", txtPhoneNumber.Text)
                      cmd.ExecuteNonQuery()
                      MessageBox.Show("Customer has been added!")
                      con.Close()
                  End If
              ElseIf editMode = True Then
                  If txtFirstName.Text = "" Then
                      MessageBox.Show("First Name Is required")
                  ElseIf txtLastName.Text = "" Then
                      MessageBox.Show("Last Name Is required")
                  Else
                      Dim con As New SqlConnection(CS)
                      con.Open()
                      Dim cmd As New SqlCommand("UPDATE Customers SET FIrstName = @FName, LastName = @LName, PhoneNumber = @PhNumber WHERE Id = @Id", con)
                      cmd.Parameters.AddWithValue("@Id", id)
                      cmd.Parameters.AddWithValue("@FName", txtFirstName.Text)
                      cmd.Parameters.AddWithValue("@LName", txtLastName.Text)
                      cmd.Parameters.AddWithValue("@PhNumber", txtPhoneNumber.Text)
                      cmd.ExecuteNonQuery()
                      MessageBox.Show("Customer has been edited.")
                      con.Close()
                  End If
              End If
  
          Catch ex As Exception
              MessageBox.Show(Me, ex.Message, "Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
          End Try
  
      End Sub
  
      Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
          Me.Close()
      End Sub
  
      Private Sub CustomerForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          If editMode = False Then
              GetId()
          Else
              textId.Text = id
              txtFirstName.Text = FName
              txtLastName.Text = LName
              txtPhoneNumber.Text = PhNumber
          End If
      End Sub
  
      Private Sub GetId()
  
      Dim cmd As New SqlCommand("SELECT MAX(id)+1 FROM Customers")
      da.OpenDBConnection()
      da.CreateCommandObject()
      textId.Text = da.FillDataTable().Rows(0)(0).ToString()
      If textId.Text = "" Then
          textId.Text = "1"
      End If
  
  End Sub
  
  
  
  End Class
  </code></pre>