Imports System.Data.SqlServerCe
Imports System.Data.SqlClient
Public Class APC
Public Dcn As New SqlCeConnection <-*********************
Private Drc As SqlCeCommand
Private Dse As SqlCeEngine
Private LDrd As SqlDataReader
Private Sub APC_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox.PasswordChar = "*"
TextBox.Focus()
Button.BackColor = System.Drawing.Color.Green
End Sub
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
Button.BackColor = System.Drawing.Color.Yellow
Button.Refresh()
Cursor.Current = Cursors.WaitCursor
If TextBox.PasswordChar = "*" Then
Call OpenDatabase() <-******************
Else
lstList.Items.Clear()
lstList.Columns.Clear()
lstList.Columns.Add("Student", lstList.Width, HorizontalAlignment.Left)
Select Case Label.Text
Case "Last Name"
Dim Drc As New SqlCeCommand
Dim Drd As SqlCeDataReader
Drc.Connection = Dcn
Drc.CommandType = Data.CommandType.Text
Drc.CommandText = "Select * From Student Where LastName Like '" & TextBox.Text & "%'"
Drd = Drc.ExecuteReader()
While Drd.Read
Dim lwi As New ListViewItem
lwi.Text = Drd(1) & ", " & Drd(2) & " (" & Drd(5) & ")"
lstList.Items.Add(lwi)
End While
End Select
End If
Cursor.Current = Cursors.Default
Button.BackColor = System.Drawing.Color.Green
End Sub
Private Function OpenDatabase() As Boolean <--*** made this a function
OpenDatabase = False
'Dim Dcn As New SqlCeConnection <--***************
Dim booNotFound As Boolean
Dcn.ConnectionString = "DataSource=\My Documents\Business\APC.sdf; Password=" & TextBox.Text
Try
Dcn.Open()
TextBox.PasswordChar = ""
Catch ex As SqlCeException
If ex.NativeError = 25046 Then
booNotFound = True
Else
MsgBox(ex.Message)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
If booNotFound Then
Dim dse As New SqlCeEngine("DataSource=\My Documents\Business\APC.sdf; Password=" _
& TextBox.Text & ";encrypt database=TRUE")
Try
lblStatus.Text = "Creating database"
lblStatus.Refresh()
dse.CreateDatabase()
Dcn.Open()
lblStatus.Text = ""
lblStatus.Refresh()
If LoadDatabase(Dcn) = False Then
Dcn.Close()
Dcn.Dispose()
System.IO.File.Delete("\My Documents\Business\APC.sdf")
Cursor.Current = Cursors.Default
MsgBox("Database did not sync!")
Me.Close()
Exit Function
End If
TextBox.PasswordChar = ""
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
If TextBox.PasswordChar = "*" Then
TextBox.SelectAll()
TextBox.Focus()
Else
TextBox.Text = ""
Label.Text = "Last Name"
End If
End Function