We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

NullReferenceException

Navicerts
Navicerts asked
on
Medium Priority
399 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
Comment
Watch Question

Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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

Author

Commented:
thank you sir
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.