• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

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
0
Navicerts
Asked:
Navicerts
  • 2
1 Solution
 
appariCommented:
you are trying to access cn object before its instance is created.
try shifting the declaration of "cmd" to after "cn=new ..." 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")
Dim cmd As SqlCeCommand = cn.CreateCommand
        cn.Open()
        Dim NumberOfRecords As Integer

0
 
appariCommented:
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

0
 
NavicertsAuthor Commented:
thank you sir
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now