0

I am trying to read data from local database - sdf file. I am using this code to get it:

Private Sub BookEdit_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Using conn As New SqlCeConnection("Data Source=|DataDirectory|Library.sdf;Persist Security Info=False")

        conn.Open()

        Dim comm = New SqlCeCommand("SELECT * FROM Book", conn)
        Dim reader As SqlCeDataReader = comm.ExecuteReader()

        While reader.Read()
            MessageBox.Show(reader.GetString(0))
        End While

    End Using

End Sub

When I open this form window, it is just freezing and then closing without showing any error.

Tools -> Options->TextEditor->C#->

Here I have tried to turn on Underline errors in the editor and Show live semantic errors. However my code is in VB.net so when I open same thing for VB.NET there is no this function.

Also tried to turn on Option Strict

Tools->Options->Projects and Solutions->VB defaults

But no effect. Visual Studio is not showing any error. So I do not know where to fix to connect to SDF file and show my windows form.

How to solve this problem?

Info: Visual Studio 2012 Ultimate Windows 8.1 Microsoft .NET framework 4.5.51641

Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109
  • 1
    move the code to a button click to test it - sometimes exceptions in FOrmLoad are hidden. Or run in AnyCPU mode (but that disables edit and run). `If reader.HasRows...` would not hurt either – Ňɏssa Pøngjǣrdenlarp Apr 23 '15 at 19:24
  • what about constructor? can i put my code to constructor? will it catch error? – Joe Rakhimov Apr 23 '15 at 19:25
  • 1
    No, the earliest is Form_shown, the button click is just to see catch the error; you could put it back once you fix it – Ňɏssa Pøngjǣrdenlarp Apr 23 '15 at 19:27
  • 1
    `FormLoad` swallowing exceptions is an `x64` issue. http://stackoverflow.com/questions/1583351/silent-failures-in-c-seemingly-unhandled-exceptions-that-does-not-crash-the-pr – Der Kommissar Apr 23 '15 at 20:19

1 Answers1

0

I am just trying to help. And i guess you might have to close conn and SqlCeDataReader object as below code. Or further you might want to put the code in Try-Catch block.

Private Sub BookEdit_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Using conn As New SqlCeConnection("Data Source=|DataDirectory|Library.sdf;Persist Security Info=False")

    conn.Open()

    Dim comm = New SqlCeCommand("SELECT * FROM Book", conn)
    Dim reader As SqlCeDataReader = comm.ExecuteReader()

    While reader.Read()
        MessageBox.Show(reader.GetString(0))
    End While

    reader.Close()
    conn.Close()
End Using
End Sub
Topman
  • 306
  • 1
  • 4
  • 14