Alan Haden
Alan Haden

Reputation: 147

WPF .net Framework with MDF file not running when published and installed on another computer

I have created a very simple WPF application which simply reads and displays the contents of a local MDF database. It runs fine on my computer but fails when I publish and install it on another.

I have added a Try/Catch statement around the cm.Open() statement and discovered that it is unable to connect to the database.

I'm using Visual Studio 2022. The application is in C#. The local database is an MDF file.

This is my code...

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True");
        cn = new SqlConnection(@"Data Source=server;Initial Catalog=Database1.mdf;Integrated Security=True");
        try
        {
            cn.Open();
            GetAllEmployeeRecocrd();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        
    }

    private void GetAllEmployeeRecocrd()
    {
        cmd = new SqlCommand("SELECT * FROM tblEmployee", cn);

        da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGrid1.ItemsSource = dt.DefaultView;
    }

The only prerequisite which is automatically included is the .Net Framework 4.7.2.

Can anyone offer any ideas please?

Upvotes: 0

Views: 60

Answers (1)

Alan Haden
Alan Haden

Reputation: 147

I had forgotten to add SQL Server. Thank you to Hans Passant

Upvotes: 0

Related Questions