I saw remarks here about how slow forms load and such on these PPC devices.

So I decided to make my life easy and have just one label, one textbox and one button to process activity on a single form.

First time you click the BUTTON it opens the connection to the SQL CE database. Then it changes the label caption to LAST NAME.

Next time you click the BUTTON it reads the label caption to determine what happens next - unfortunately the DCN connection object has become NOTHING. I'm confused.

I've not done a lot of VS2005 work yet - so I cannot figure out why my DCN connection object is not persisting. I've made it PUBLIC up top - but that did not change anything.

I've stepped through the code - when I get to line 18 the DCN object is nothing. If I put the DIM...NEW in the IF/block that initially creates/opens it - then it gets worse. I tried IF DCN IS NOTHING in front of the DIM - but that's not doing it either.

VB Code:
  1. Imports System.Data.SqlServerCe
  2. Imports System.Data.SqlClient
  3.  
  4. Public Class APC
  5.  
  6.     Private Dcn As SqlCeConnection
  7.     Private Drc As SqlCeCommand
  8.     Private Dse As SqlCeEngine
  9.     Private LDrd As SqlDataReader
  10.  
  11.     Private Sub APC_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  12.         TextBox.PasswordChar = "*"
  13.         TextBox.Focus()
  14.         Button.BackColor = System.Drawing.Color.Green
  15.     End Sub
  16.  
  17.     Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
  18.         Dim Dcn As New SqlCeConnection
  19.         Button.BackColor = System.Drawing.Color.Yellow
  20.         Button.Refresh()
  21.         Cursor.Current = Cursors.WaitCursor
  22.         If TextBox.PasswordChar = "*" Then
  23.             Dim booNotFound As Boolean
  24.             Dcn.ConnectionString = "DataSource=\My Documents\Business\APC.sdf; Password=" & TextBox.Text
  25.             Try
  26.                 Dcn.Open()
  27.                 TextBox.PasswordChar = ""
  28.             Catch ex As SqlCeException
  29.                 If ex.NativeError = 25046 Then
  30.                     booNotFound = True
  31.                 Else
  32.                     MsgBox(ex.Message)
  33.                 End If
  34.             Catch ex As Exception
  35.                 MsgBox(ex.Message)
  36.             End Try
  37.             If booNotFound Then
  38.                 Dim dse As New SqlCeEngine("DataSource=\My Documents\Business\APC.sdf; Password=" _
  39.                                                 & TextBox.Text & ";encrypt database=TRUE")
  40.                 Try
  41.                     lblStatus.Text = "Creating database"
  42.                     lblStatus.Refresh()
  43.                     dse.CreateDatabase()
  44.                     Dcn.Open()
  45.                     lblStatus.Text = ""
  46.                     lblStatus.Refresh()
  47.                     If LoadDatabase(Dcn) = False Then
  48.                         Dcn.Close()
  49.                         Dcn.Dispose()
  50.                         System.IO.File.Delete("\My Documents\Business\APC.sdf")
  51.                         Cursor.Current = Cursors.Default
  52.                         MsgBox("Database did not sync!")
  53.                         Me.Close()
  54.                         Exit Sub
  55.                     End If
  56.                     TextBox.PasswordChar = ""
  57.                 Catch ex As Exception
  58.                     MsgBox(ex.Message)
  59.                 End Try
  60.             End If
  61.             If TextBox.PasswordChar = "*" Then
  62.                 TextBox.SelectAll()
  63.                 TextBox.Focus()
  64.             Else
  65.                 TextBox.Text = ""
  66.                 Label.Text = "Last Name"
  67.             End If
  68.         Else
  69.             lstList.Items.Clear()
  70.             lstList.Columns.Clear()
  71.             lstList.Columns.Add("Student",lstlist.Width,HorizontalAlignment.Left    )
  72.             Select Case Label.Text
  73.                 Case "Last Name"
  74.                     Dim Drc As New SqlCeCommand
  75.                     Dim Drd As SqlCeDataReader
  76.                     Drc.Connection = Dcn
  77.                     Drc.CommandType = Data.CommandType.Text
  78.                     Drc.CommandText = "Select * From Student Where LastName Like '" & TextBox.Text & "%'"
  79.                     Drd = Drc.ExecuteReader()
  80.                     While Drd.Read
  81.                         Dim lwi As New ListViewItem
  82.                         lwi.Text = Drd(1) & ", " & Drd(2) & " (" & Drd(5) & ")"
  83.                         lstList.Items.Add(lwi)
  84.                     End While
  85.             End Select
  86.         End If
  87.         Cursor.Current = Cursors.Default
  88.         Button.BackColor = System.Drawing.Color.Green
  89.     End Sub