ive recently learnt that you can store connection strings in a .config file so i created one (connectionstring) for to test this out however whenever i run this i get a object reference not set to an instance of an object ive referenced system.configuration.dll and im using system.configuration; but i cant figure out why this is throwing this error
this is the connectionstring.config fileCode:using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Windows; using System.Windows.Input; using System.Windows.Threading; namespace ExperimentingApplication { /// <summary> /// Interaction logic for AuthenticationScreen.xaml /// </summary> public partial class AuthenticationScreen : Window { public AuthenticationScreen() { InitializeComponent(); } private void UserInputKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) //only accepts enter key { string dbconnection = ConfigurationManager.ConnectionStrings["Database"].ConnectionString; //if there is anyway of condensing it please let me know SqlConnection connection = new SqlConnection(dbconnection); connection.Open(); if (connection.State == ConnectionState.Open) // if connection.Open was successful { MessageBox.Show("You have been successfully connected to the database!"); } else { MessageBox.Show("Connection failed."); }
Code:<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="Database" connectionString="Server=******-*******;Database=TestDatabse;Trusted_Connection=True;" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>




Reply With Quote
