Mistake

System.NullReferenceException: The object reference does not indicate an object instance. in FastFoodDemo.Program.GetConnection () in C: \ Users \ User \ Desktop \ FastFoodDemo \ FastFoodDemo \ Program.cs: line 36

Program.cs

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using System.Reflection; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Configuration; using MySql.Data.MySqlClient; namespace FastFoodDemo { public class Program { /// <summary> /// The main entry point for the application. /// </summary> /// [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form2()); } public static int admin = 0; public static string server = "null"; public static MySqlConnection GetConnection() { MySqlConnection conn = new MySqlConnection(); conn.ConnectionString = ConfigurationManager.ConnectionStrings["connect_admin"].ConnectionString; return conn; } } } 

app.config

 <connectionStrings> <add name="connect_admin" connectionString="Data Source=192.168.0.1;Initial Catalog=database;Persist Security Info=True;User ID=admin;Password=123" providerName="MySql.Data.MySqlClient" /> </connectionStrings> 

How to be gentlemen, tell me please) Given that when you start the program from VS 2017 - it works. But as soon as they launch the finished * .exe, this crashes :(

  • one
    And in the same folder where the "ready" exe lies, is the file * .exe.config? And it says the same as in your example? - Artyom Okonechnikov
  • Assembly goes through ILMerge. Near only pdb file. - Xhonor pm
  • Well, here is the answer, put the config next to the executable file, calling it in the format exename.exe.config - Artyom Okonechnikov
  • one
    Understood nothing. You have an application on ConnectionStrings["connect_admin"] because this section is not in the configuration file, because there is no configuration file itself. Compile the application and open the bin folder there will be your executable file and a number of the configuration file, so everything works for you "from the studio". You should do the same with your output file after ILMerge, put this configuration file next to it with the name in the format that I wrote in the comment above. - Artyom Okonechnikov
  • one
    Do not store the password in the clear, of course. If it will be easier for you, then ILMerge will hardly help you either. Review the application architecture in terms of database authentication. - Artyom Okonechnikov

1 answer 1

Your code crashes with a NullReferenceException , since ConnectionStrings["connect_admin"] returns null due to the lack of a configuration file in the search directory.


The name of the configuration file in the output directory must always be in the format имя_исполняемого_файла.расширение.config and is located in the same directory as this executable file. For example, the executable file program.exe must match the configuration file program.exe.config