title: C# - Connecting to an Access MDB file using WPF (The ConnectionString Property has not been initialized)
body: <p>Let me start by saying I've only been coding C# for about 3-4 month. </p>
  
  <p>I am trying to get a basic understanding of how databases are accessed within Visual studio so that I can both insert, update and delete data. I have tried various different tests and I think I've come to the conclusion that there's something about WPF's that OleDb doesn't quite like. I have created 2 identical projects, one using Windows Forms and another using WPF. The Windows Forms version of the project works absolutely fine however the WPF project does not, giving me the error </p>
  
  <p>"The ConnectionString Property has not been initialized"</p>
  
  <p>I have no idea how to fix this. I've been scouring the internet for the best part of 12 hours and still had absolutely no success.</p>
  
  <p>Here is the original code for the WPF file. It is the same as the Windows Form code</p>
  
  <pre><code>public partial class MainWindow : Window
  {
  
      OleDbCommand cmd = new OleDbCommand();
      OleDbConnection cn = new OleDbConnection();
      OleDbDataReader dr;
  
      public MainWindow()
      {
          InitializeComponent();
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
  
          cn.ConnectionString = @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\MarkB\Documents\testing.mdb";
          cmd.Connection = cn;
          loaddata();
      }
      private void loaddata()
      {
          listBox1.Items.Clear();
          listBox2.Items.Clear();
          listBox3.Items.Clear();
          listBox4.Items.Clear();
          try
          {
              string q = "select * from info";
              cmd.CommandText = q;
              cn.Open();
              dr = cmd.ExecuteReader();
              if(dr.HasRows)
              {
                  while(dr.Read())
                  {
                      listBox1.Items.Add(dr[0].ToString());
                      listBox2.Items.Add(dr[1].ToString());
                      listBox3.Items.Add(dr[2].ToString());
                      listBox4.Items.Add(dr[3].ToString());
                  }
              }
              dr.Close();
              cn.Close();
          }
          catch(Exception e)
          {
              cn.Close();
              MessageBox.Show(e.Message.ToString());
          }
      }
  
      private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
      {
          ListBox l = sender as ListBox;
          if(l.SelectedIndex != -1)
          {
              listBox1.SelectedIndex = l.SelectedIndex;
              listBox2.SelectedIndex = l.SelectedIndex;
              listBox3.SelectedIndex = l.SelectedIndex;
              listBox4.SelectedIndex = l.SelectedIndex;
          }
  
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
  
          if ((textBox1.Text!="") &amp;&amp; (textBox2.Text!=""))
          {
              string q ="insert into info (firstname,surname,address) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text +"')";
              dosomething(q);
              textBox1.Text = null;
              textBox2.Text = null;
              textBox3.Text = null;
          }
      }
  
      private void dosomething(String q)
      {
          try
          {
              cn.Open();
              cmd.CommandText = q;
              cmd.ExecuteNonQuery();
              MessageBox.Show("Data Saved");
              cn.Close();
              loaddata();
          }
          catch (Exception e)
          {
              cn.Close();
              MessageBox.Show(e.Message.ToString());
          }
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          if(listBox1.SelectedIndex !=-1)
              {
              string q = "delete from info where id =" + listBox1.SelectedItem.ToString();
              dosomething(q);
          }
      }
  }
  </code></pre>
  
  <p>If anyone can help it would be greatly appreciated. I know the problem must stem from the connection aspect of the code</p>