Link to home
Create AccountLog in
Avatar of Navicerts
NavicertsFlag for United States of America

asked on

NullReferenceException

Hello,

I am getting an error message when i show a new form and try to execute the following onload code.  The program errors out @ the declarations.  Thanks for any help.

Here is the code.

Imports System.Data.SqlServerCe
Imports System.IO
Public Class Totals
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
'removed
#End Region
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        Dim cn As SqlCeConnection
        Dim sqlEngine As SqlCeEngine
        Dim cmd As SqlCeCommand = cn.CreateCommand
        Dim da As New SqlCeDataAdapter

        Closeme.Enabled = False
        cn = New SqlCeConnection("Data Source=\My Documents\HandheldDBWeight.sdf")
        cn.Open()
        Dim NumberOfRecords As Integer

        cmd.CommandText = "SELECT Count(""Wing Band Number"") FROM TemporaryWeight WHERE ""Weight"" Is Null"
        cmd.Connection = cn
        dr = cmd.ExecuteReader
        dr.Read()
        NumberOfRecords = dr.GetValue(0)
        Incomplete.Text = NumberOfRecords

        cmd.CommandText = "SELECT Count(""Wing Band Number"") FROM TemporaryWeight WHERE ""Weight"" Is Not Null"
        cmd.Connection = cn
        dr = cmd.ExecuteReader
        dr.Read()
        NumberOfRecords = dr.GetValue(0)
        complete.Text = NumberOfRecords

        Dim table As New DataTable
        Dim SQL As String
        SQL = "SELECT ""Cage Number"", ""Wing Band Number"", Feed, Weight FROM TemporaryWeight ORDER BY ""Cage Number"""

        cmd.CommandText = SQL
        cmd.Connection = cn
        da.SelectCommand = cmd
        da.Fill(table)
        MyGrid.DataSource = table

        If cn.State <> ConnectionState.Closed Then
            cn.Close()
        End If

        MyGrid.ColumnHeadersVisible = False
        Closeme.Enabled = True
    End Sub
    Private Sub Quit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Closeme.Click
        Me.Close()
    End Sub
End Class

Error Message...

An unhandled exception of type 'System.NullReferenceException' occurred in (Project Name).exe
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
or like this

Dim cn As SqlCeConnection
        Dim sqlEngine As SqlCeEngine
        Dim cmd As SqlCeCommand '= cn.CreateCommand
        Dim da As New SqlCeDataAdapter

        Closeme.Enabled = False
        cn = New SqlCeConnection("Data Source=\My Documents\HandheldDBWeight.sdf")
        cn.Open()
cmd = cn.CreateCommand

        Dim NumberOfRecords As Integer

Avatar of Navicerts

ASKER

thank you sir