1

I am taking an Application Development course and I am not able to figure out what's wrong in my context. Can anyone please help me out? I am kind of new in communicating with databases so I am unable to come up with a solution for this. Thanks in advance!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                try
                {
                    if (sqlConnection.State == ConnectionState.Closed)
                        sqlConnection.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
     }
        }
    }
}

App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <clear />
    <add name="NorthwindConnectionString"
     providerName="System.Data.ProviderName" 
    connectionString="Server=localhost;Database=Northwind;Integrated Security=true;" />
  </connectionStrings>

</configuration>
  • 1
    do you have a reference to `System.Configuration.dll`? – Daniel A. White Oct 09 '14 at 19:42
  • No, I had a comment on the code: // Add a reference to System.Configuration in .NET Then I got lost –  Oct 09 '14 at 19:45
  • Okay I added the reference System.Configuration.dll It seems to work now. Silly mistake on my end. Thank you Daniel. –  Oct 09 '14 at 19:47

1 Answers1

7

TRY THIS:

Menu Bar -> Project -> Add Reference... -> .NET (Tab) -> System.Configuration -> OK

You Should Make sure that You are referencing a System.Configuration.dll

Dgan
  • 10,077
  • 1
  • 29
  • 51
  • Yeah I thought that it would automatically be referenced when I added "using System.Configuration;" –  Oct 09 '14 at 19:57
  • @SamerElSabeh Unless the DLL is in the GAC. For More Info http://stackoverflow.com/questions/25643746/visual-studio-reference-of-dlls-are-automatically-created-at-bin-folder – Dgan Oct 09 '14 at 20:00
  • how to do this in vscode? – alex Mar 31 '20 at 01:09
  • @alex it is only in Visual Studio , vscode is simple editor – Dgan Apr 01 '20 at 13:34