I am developing C#-based mobile application and the exception always thrown and catched by my try catch block
Exceptin thrown : The database file cannot be found.Check the path to the database [File name = .\MyMobileDB.sdf]

the snippet code is

namespace DeviceApplication4
{
    public partial class Form1 : Form
    {
        public string strCon = @"Data Source =.\MyMobilePhoneDB.sdf; Password =123@asu";
        public SqlCeConnection SqlConn;
        public SqlCeCommand CeCmd = new SqlCeCommand();
        public SqlCeDataReader dataReader;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                SqlConn = new SqlCeConnection(strCon);
                CeCmd.CommandText = strCon;
                SqlConn.ConnectionString = strCon;

                MessageBox.Show("After open");

                SqlConn.Open();


                MessageBox.Show("After open");
                

                SqlConn.Close();

                MessageBox.Show("After close");
            }
            catch (SqlCeException x)
            {
                MessageBox.Show( x.ToString());

            }
        }
    }
}

This exception exception thrown at line 27 and displayed after being caught at line 36

Dani AI

Generated

A few focused notes that address the actual causes in the snippet and the follow-up posts by and . The exception text mentions .\MyMobileDB.sdf while the code uses MyMobilePhoneDB.sdf — a filename mismatch is an easy-to-overlook cause. The snippet also sets SqlCeCommand.CommandText to the connection string; CommandText must be an SQL statement, and the command must reference the connection. ‘s absolute-path idea and ‘s solution (including the .sdf so it deploys with the app) point in the right direction. The checklist and example below show a minimal, reliable pattern.

string dbPath = "\\Program Files\\MyApp\\MyMobilePhoneDB.sdf";
if (!System.IO.File.Exists(dbPath))
{
    MessageBox.Show("DB not found at: " + dbPath);
}
string connStr = "Data Source=" + dbPath + ";Password=123@asu;";
using (SqlCeConnection conn = new SqlCeConnection(connStr))
{
    conn.Open();
    using (SqlCeCommand cmd = conn.CreateCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = "SELECT COUNT(*) FROM MyTable";
        int count = Convert.ToInt32(cmd.ExecuteScalar());
    }
}

Short troubleshooting checklist: confirm the exact filename used in the connection string matches the deployed file; prefer absolute paths so the current working directory won’t mislead; check deployment settings so Visual Studio copies the .sdf into the device/emulator image; use File.Exists(path) at runtime to verify the file and print the full path when the exception occurs; and ensure the SQL CE runtime/version on the device is compatible with the .sdf format. Finally, correct the command usage (assign the command’s Connection and use a real SQL statement) to avoid confusing runtime errors with file/path problems.

Recommended Answers

All 2 Replies

>DB file cannot be found chck the path to the DB

Use absolute path.

public string strCon = @"Data Source =x:\folder\MyMobilePhoneDB.sdf; Password =123@asu";

>DB file cannot be found chck the path to the DB

Use absolute path.

public string strCon = @"Data Source =x:\folder\MyMobilePhoneDB.sdf; Password =123@asu";

Thank you for all of you you are so gentl
I solved the problem to have a database on my device application
and this the way

step 1 : Create New database represents the database of my project
A- R-click on the project (not solution explorer)
B- Add new item
C-Choose Database file

Step 2 : from the Menue bar : Data -> Create new Data Source
following the wizard you can add the the file created in previous step as the new data Source

step 3 : from the Menue : Data -> ShowDataSource
a DataSet will appear on the left panel , R-click on it and choose
Edit DataSet with Designer

Step 4 : Add a dataTable for each column
Simply Right click on the DataSet Designer , and add required relations
Step 5 : Please see the following link and thank you in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.