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();
}
}