[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

NullReferenceException

Posted on 2006-03-28
3
Medium Priority
?
385 Views
Last Modified: 2012-05-05
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
Comment
Question by:Navicerts
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
appari earned 2000 total points
ID: 16316649
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
 
LVL 39

Expert Comment

by:appari
ID: 16316657
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
 
LVL 7

Author Comment

by:Navicerts
ID: 16321491
thank you sir
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question