Results 1 to 4 of 4

Thread: [RESOLVED] Problem with CONNECTION object

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Resolved [RESOLVED] Problem with CONNECTION object

    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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Problem with CONNECTION object

    Hi,
    but you are doing
    Dim Dcn As New SqlCeConnection in your click which will override the private dcn, and create a new one every time.

    My advice would be to open at the start, and only close at the end for performance reasons

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Problem with CONNECTION object

    I was trying to only open once - but having a problem - I think - with the scope of the variable. I'm probably not following good VS2005 OO concepts - but I finally got it to work with these changes.

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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Problem with CONNECTION object

    Hi,
    as I said, the
    vb Code:
    1. Dim Dcn As New SqlCeConnection
    was overriding your
    vb Code:
    1. Private DCN as SqlCeConnection
    which was the root of the problems.

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width