Results 1 to 5 of 5

Thread: Problem with closing connection and showing data

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    Problem with closing connection and showing data

    This is my first attempt at SQLCE and I am having an issue that I can't figure out. If I close the connection in the finally statement as shown below I only get the first record from the database. When I uncomment it I get both records. What am I doing wrong?

    This is my code:
    VB Code:
    1. '
    2.         Dim con As SqlCeConnection = Nothing
    3.         Try
    4.             Dim dir As String = Path.GetDirectoryName( _
    5.                 Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
    6.  
    7.             con = New SqlCeConnection("Data Source = '" & dir & "\RouteDB.sdf'")
    8.             con.Open()
    9.  
    10.             Dim cmd As SqlCeCommand = New SqlCeCommand( _
    11.                 "SELECT RouteID " & _
    12.                 ", Name FROM Routes " & _
    13.                 "Where IsDeleted = 0", con)
    14.             cmd.CommandType = CommandType.Text
    15.  
    16.             Dim resultSet As SqlCeResultSet = _
    17.                 cmd.ExecuteResultSet(ResultSetOptions.Scrollable)
    18.  
    19.             dgRoutes.DataSource = resultSet
    20.  
    21.         Catch ex As Exception
    22.             MessageBox.Show(ex.Message.ToString)
    23.         Finally
    24.             'con.Close()
    25.             ' If I uncomment the close I only get the first record
    26.             ' in the database. If I comment it out as is here
    27.             ' both the records in the database show up.
    28.         End Try

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Problem with closing connection and showing data

    Hey,

    Really? That seems very strange?!?!

    Are you sure that is the only change that you are making?

    Gary

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    Re: Problem with closing connection and showing data

    Yes I tried it over and over. In fact the only reason I found that it made a difference is that I had used the same block of code on another datagrid and had accidently commented the close out when I commented out the TRY block. When I ran the program the second datagrid worked so when I was comparing the two blocks the only difference was the close. I removed it and it worked.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Problem with closing connection and showing data

    Ok, one thing to try...

    Rather then calling .Close in the finally of the try, wrap the SqlCeConnection in a Using statement:

    http://www.pluralsight.com/community...4/28/7834.aspx

    Does that work?

    Gary

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

    Re: Problem with closing connection and showing data

    Hi,
    debug it and count the rows in the resultset - I would guess it is the grid binding that is failing, not the get.
    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