Null Reference Exception

  • Thread starter Thread starter gwboolean
  • Start date Start date
G

gwboolean

Guest
I have some code here for editing and saving an existing record in an sql table.

I have had this work quite well before, but I have been moving the code around and trying to clean up a lot of crap and now I am unable to get this to work. It looks right to me, but I am obviously missing something.

The Error I get occurs at the MasterBaseConnection.Close() line of the MasterBaseClose() method.


Public Class SetConnection
Public MasterBaseConnection As SqlConnection
Public Sub MasterBaseOpen()
'Open Masterbase connection
MasterBaseConnection = New SqlConnection("Server=.\SQLEXPRESS; AttachDbFilename=F:\SiTech\MasterBase\MasterBase1.0\DataBase\MasterBase1.0.mdf; Database=MasterBaseDB.mdf;Trusted_Connection=Yes;")
MasterBaseConnection.Open()
End Sub
Public Sub MasterBaseClose()
'Close connection
MasterBaseConnection.Close()
MasterBaseConnection.Dispose()
End Sub
End Class
1431692.jpg


The process and the methods below represent what my old working process has morphed into, which is something that no longer works. I have gotten the add record and new record parts change successfully, however.

I have stepped through the code numerous times and still cannot see what I have done wrong. Only the Load event and the button events code are in the frmFileMaster Class. All of the other methods exist in the namespace nspMasterBase. I know that makes a difference, but I have worked around it before even though I am still not sure why that is.

Private Sub frmFileMaster_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Select Case _SetState
Case = "Edit"
'Setup Form
With Me
Dim setupData As New nspMasterBase.SetData
setupData.SetDataFileMaster("Edit")
Dim setupForm As New nspMasterBase.SetState
setupForm.SetStateFileMaster("Edit")
End With
End Select
Catch ex As Exception
MessageBox.Show(ex.Message, "Error in Processing SQL Query", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub




Public Class SetData
Dim FileMasterConn As New nspMasterBase.SetConnection
Public FileMasterCommand As SqlCommand
Public FileMasterAdapter As New SqlDataAdapter
Public FileMasterTable As New DataTable
Public FileMasterManager As CurrencyManager
Public FileMasterBIndingSource As New BindingSource
Public Sub SetDataFileMaster(ByVal Appstate As String)
'Dim setupData As New nspMasterBase.SetData
Select Case Appstate
Case = "Edit"
'Connect to database
With frmFileMaster
'Connect to database
'Establish command object and open connection
FileMasterConn.MasterBaseOpen()
FileMasterCommand = New SqlCommand("SELECT * FROM tblFileMaster WHERE chrFileID = '" & _strFileMasterID & "'", FileMasterConn.MasterBaseConnection)
FileMasterAdapter = New SqlDataAdapter()
FileMasterAdapter.SelectCommand = FileMasterCommand
FileMasterTable = New DataTable()
FileMasterAdapter.Fill(FileMasterTable)
'Currency Manager
FileMasterManager = DirectCast(frmFileMaster.BindingContext(FileMasterTable), CurrencyManager)
'Command Builder
Dim FileMasterCommandBuild As SqlCommandBuilder = New SqlCommandBuilder(FileMasterAdapter)
'Binding Source
FileMasterBIndingSource.DataSource = FileMasterTable
End With
'Bind Controls
With frmFileMaster
.lblFileID.DataBindings.Add("Text", FileMasterTable, "chrFileID")
.lblFileDirectory.DataBindings.Add("Text", FileMasterTable, "chrFileAddress")
.lblDate.DataBindings.Add("Text", FileMasterTable, "dteOpened", True, DataSourceUpdateMode.OnValidation, DateFormat.ShortDate)
.txtTitle.DataBindings.Add("Text", FileMasterTable, "chrFileTitle")
.txtFileDescription.DataBindings.Add("Text", FileMasterTable, "chrFileDescription")
.chkCat1.DataBindings.Add("Checked", FileMasterTable, "blnCategory1", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.chkCat2.DataBindings.Add("Checked", FileMasterTable, "blnCategory2", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.chkCat3.DataBindings.Add("Checked", FileMasterTable, "blnCategory3", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.chkCat4.DataBindings.Add("Checked", FileMasterTable, "blnCategory4", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.chkCat5.DataBindings.Add("Checked", FileMasterTable, "blnCategory5", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.chkCat6.DataBindings.Add("Checked", FileMasterTable, "blnCategory6", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.chkCat7.DataBindings.Add("Checked", FileMasterTable, "blnCategory7", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.chkCat8.DataBindings.Add("Checked", FileMasterTable, "blnCategory8", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.chkCat9.DataBindings.Add("Checked", FileMasterTable, "blnCategory9", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.chkCat0.DataBindings.Add("Checked", FileMasterTable, "blnCategory0", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.rdoCat1.DataBindings.Add("Checked", FileMasterTable, "blnSelect1", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.rdoCat2.DataBindings.Add("Checked", FileMasterTable, "blnSelect2", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.rdoCat3.DataBindings.Add("Checked", FileMasterTable, "blnSelect3", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.rdoCat4.DataBindings.Add("Checked", FileMasterTable, "blnSelect4", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
.rdoCat5.DataBindings.Add("Checked", FileMasterTable, "blnSelect5", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
'Currency Manager
FileMasterManager = DirectCast(frmFileMaster.BindingContext(FileMasterTable), CurrencyManager)
End With

End Select
End Sub


Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
btnSave.Enabled = False
Select Case _SetState

Case = "Edit"
With Me
Dim EndEdit As New nspMasterBase.SetData
EndEdit.EditRecord()
Dim setupForm As New nspMasterBase.SetState
setupForm.SetStateFileMaster("View")
Me.Show()
End With
End Select
End Sub

Public Sub EditRecord() 'Currency Manager
FileMasterManager = DirectCast(frmFileMaster.BindingContext(FileMasterTable), CurrencyManager)
FileMasterManager.EndCurrentEdit()
FileMasterAdapter.Update(FileMasterTable)
CloseRecord()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Select Case _SetState
Case = "Edit"
Me.Close()
mnuMain.Show()
End Select
End Sub







gwboolean

Continue reading...
 
Back
Top