Skip to content
Advertisement

SQL connection string is not recognised and throws and exception in C#

CONTEXT: I am writing a WPF app. It works with a SQL Server database in which I put some data, concretely the strings Titulo and Descripcion, the int XP and the string fotoMision. When I click a button the program is supposed to save this data in the database.

PROBLEM: when I click the button it throws me an exception in string connection’s line showing that the object is not instanced. If I put these first lines right below the InitializeComponent(); line the second one doesn’t recognise the miConexion string. Why does that happen and how can I fix it?

CODE:

Advertisement

Answer

Connections are meant to be short-lived, i.e. you should create one when the button is clicked and then dispose it right after you have executed the query. Using a using statement implicitly disposes the IDisposable:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement