0

I created a WPF app using C#, and for my database I use a .mdf files. When I deploy the app on my computer, it works perfectly. But when I try to use it on a different computer, sometimes it says cannot open database.

After a lot of hard work to figure it out, I fell into another error when I saw my database in that PC. It says object reference not set to an instance of an object.

What could possibly be the problem?

After I finished, deployed it and tested it on my PC, works great.

I tried on another PC, installing sqllocaldb only, didn't work.

I tried to install SQL Server Management Studio, and after a lot of grinding, I did manage to create a .mdf database file there with my tables and data. But I got a window error saying that object reference not set to an instance of an object.

My connection string :

String connString = "Data Source=(localdb)\\MSSQLLocalDB;Integrated Security=True;database='Autoecole.mdf';Initial Catalog=Autoecole.mdf;Connect Timeout=30;";

And my database file Autoecole.mdf, build action is content, "copy to output directory" is set to "copy if newer".

I expect to see some data of serial keys etc... but I got nothing.

EDIT

Hello, i tried to backup database and restored on the distant PC, then i tried what you told me to do, first of all, seems working perfectly, app started with database connection, but when i try other functionality using sql in the app it says "object reference not set to an instance of an object". what to do

my app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
      <add name="Autoecole.mdf"
      connectionString="Data Source=(localdb)\MSSQLLocalDB; Integrated Security=True; database='Autoecole.mdf'; Initial Catalog=Autoecole.mdf; Connect Timeout=30"
      providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

and my connString is:

String connString = System.Configuration.ConfigurationManager.ConnectionStrings["Autoecole.mdf"].ConnectionString.ToString();

EDIT AGAIN

an example of code that uses sql

private void activation_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                textBox.Text = CalculateMD5Hash(GetMacAddress().ToString());
                String mac = textBox.Text.ToString();
                String serial = CalculateMD5Hash(textBox1.Text.ToString());
                String connString = System.Configuration.ConfigurationManager.ConnectionStrings["Autoecole.mdf"].ConnectionString.ToString();
                String query = "update activation set activated='" + 1 + "' where serial='" + serial + "'";
                String query1 = "insert into localadress values('" + mac + "')";
                SqlConnection conn = new SqlConnection(connString);
                SqlCommand command = new SqlCommand(query, conn);
                conn.Open();
                int x = command.ExecuteNonQuery(); 
                if (x >0)
                {
                    MessageBox.Show("Application activé");
                    conn = new SqlConnection(connString);
                    command = new SqlCommand(query1, conn);
                    conn.Open();
                    command.ExecuteNonQuery();
                    MessageBox.Show("Redemmarer l'application");
                }
                else
                {
                    MessageBox.Show("Numero de serie faux, l'application va se fermer");
                }
                System.Windows.Application.Current.Shutdown();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                SqlConnection conn = new SqlConnection();
                conn.Close();
            }
        }

1 Answer 1

1

First off, a simple solution to this problem could be making sure your paths are all spelled exactly correctly. If so, make sure you have Microsoft SQL Server 2017 or whatever version of Microsoft SQL Server you want installed on your other PC. Also make sure your connection string is declared the exact same way in your App.config file, or that it is at least declared there in the first place. For you, it would look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <connectionStrings>
      <add name="Autoecole.mdf"
      connectionString="Data Source=(localdb)\\MSSQLLocalDB; Integrated Security=True; database='Autoecole.mdf'; Initial Catalog=Autoecole.mdf; Connect Timeout=30"
      providerName="System.Data.SqlClient" />
   </connectionStrings>
</configuration>

If you already have that done as well, you can explicitly ask Visual Studio for the information of a connection string by using the following code:

string con = System.Configuration.ConfigurationManager.ConnectionStrings["Autoecole.mdf"].ConnectionString;

I've also read you can use the ToString() method instead of the accessing the ConnectionString property at the end of the code, so like this:

string con = System.Configuration.ConfigurationManager.ConnectionStrings["Autoecole.mdf"].ToString();

I would try both. Hope this works for you!

Sign up to request clarification or add additional context in comments.

8 Comments

Hello, i tried to backup database and restored on the distant PC, then i tried what you told me to do, first of all, seems working perfectly, app started with database connection, but when i try other functionality using sql in the app it says "object reference not set to an instance of an object". what to do
I see the code I told you to do, but it would be best if you showed me the "SQL code that gives you functionality in the app".
ill show you an example of a functionality that uses sql in a moment, ill re edit my post
Is there something else you need to know so you can help me?
Sorry Robinhood, there's nothing I need to know...I just haven't been on Stack Overflow lately. When I gave you the System.Configuration.ConfigurationManager.ConnectionStrings["Autoecole.mdf"].ConnectionString.ToString(); statement, I actually wanted you to use it to test what information you would get for your connection string, so you know what the correct information is. I would get rid of the extra ToString() when defining your connString variable - as that seems redundant and may be causing the issue - and place the above statement in a Console.WriteLine() to see what you get.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.