Visual Studio SqlCeConnection for Smart Device: SDF opening problem

Posted on 2011-04-20
Last Modified: 2013-12-27
I am a student trying to integrate a database into a smart device project using Visual Studio 2008. I want to run some SQL queries when a form loads to populate a combo box, and I think I have the code to open a database as a SqlCeConnection, but must be missing something. When i open the form, it tells me that "The database file cannot be found".

I'm pretty sure I have the SDF file embedded in the project (its new to me, so i might be looking at it wrong). Can't understand why it's unable to find the database. My ultimate goal is to populate the combo box as a first step; What am i doing wrong here?
Private Sub frmItemSelect_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strSql As String = "SELECT [BAR_CODE] FROM INVENTORY WHERE [USER_ID] = 12345"
        Dim strPath As String = "Data Source=AppDatabase1.sdf;Persist Security Info=False;"
        Dim sconInventory As New SqlCeConnection(strPath)
        Dim scmdInventory As New SqlCeCommand(strSql, sconInventory)
        Dim val1 As Integer()

        sconInventory.Open() 'This is the line telling me it can't find the database. Everything below is code I'll fix later 
        Dim sqlReader As SqlCeDataReader = scmdInventory.ExecuteReader()

            Dim i As Integer = 0
            While sqlReader.Read()
                val1(i) = sqlReader.GetInt32(0)
                i += 1
                MsgBox(val1(i) & " " & i)
            End While


        End Try

        For i = 0 To val1.length - 1


    End Sub

Open in new window

Screenshot of the project
Question by:75THRGR
    LVL 16

    Accepted Solution


    windows ce based devices do not support a current directory. So it is best to ALWAYS provide full path information for every file you want to use.

    Change your connect string and include the path to the sdf database file. For example
    "Data Source = \Program Files\InventoryProject\AppDatabase1.sdf;"
    instead of "Data Source = AppDatabase1.sdf;" only.

    See also here:

    Author Closing Comment

    That was it, wasn't sure what directory it was being ran from on the emulator, so you answered two questions in one. Thanks.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now