title: Saving Data to a Database (Single Table) C#
body: <p>I have been following the documentation here:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/0f92s97z%28v=vs.100%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/0f92s97z%28v=vs.100%29.aspx</a></p>
  
  <p>However, I cannot get my application to work!</p>
  
  <p><strong>please see my short video:</strong> <a href="https://www.youtube.com/watch?v=zpOCaT6NaGs" rel="nofollow">https://www.youtube.com/watch?v=zpOCaT6NaGs</a></p>
  
  <p>My Application is not working, I am having nightmares trying to get it working.</p>
  
  <p>I want to make a 'Stock management System',</p>
  
  <p><strong>Requirements:</strong></p>
  
  <ol>
  <li>Browse through products (Name, Price)</li>
  <li>Add Products to the Table</li>
  <li>Edit Existing Products</li>
  <li>Remove Existing Products</li>
  <li>Save changes to the Table</li>
  <li>Create an Order form that will allow for listing selected products and providing a quantity selected and cost displayed</li>
  <li>Print the results.</li>
  </ol>
  
  <p>I cannot seem to get the data to save to the database table without problems.</p>
  
  <p><strong>My System:</strong></p>
  
  <p>Windows 7 - SP1
  Visual Studio 2010 Professional
  Using a Microsoft Access Database (DataSet)
  1 Table to connect
  3 fields: "ID, proName, proPrice"</p>
  
  <p>Any Help is very welcome, Thanks.</p>
  
  <p><strong>My Code:</strong></p>
  
  <pre><code>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.OleDb;
  
  namespace KanadaStockControl
  {
      public partial class frmProducts : Form
      {
          stockDataSet dsoc;
          OleDbConnection conn;
          OleDbDataAdapter da;
  
          public frmProducts()
          {
              InitializeComponent();
          }
  
          private void btnAdd_Click(object sender, EventArgs e)
          {
              ////Adding the items to the database
              //dboKanadaDataSetProducts.ProductsRow newProductsRow;
              //newProductsRow = dboKanadaDataSetProducts.Products.NewProductsRow();
              ////Setting the Product Name
              //newProductsRow.ProductName = txtItem.Text;
              ////Setting the Product Price
              //newProductsRow.ListPrice = txtPrice.Text;
              ////Adding the new row to the table
              //this.dboKanadaDataSetProducts.Products.Rows.Add(newProductsRow);
              ////Saving the new row to the database
              //this.productsTableAdapter.Update(this.dboKanadaDataSetProducts.Products);            
              //Old NON WORKING DATABASE ADD CODE:
              //dboKanadaDataSetProductsTableAdapters.ProductsTableAdapter productsTableAdapter = new dboKanadaDataSetProductsTableAdapters.ProductsTableAdapter();
              //productsTableAdapter.Insert("9", "NA", "0", txtPrice.Text, "Software");
  
              //lstPrice.Items.Add(txtPrice.Text);
              //lstItem.Items.Add(txtItem.Text);
              stockDataSetTableAdapters.stockTableAdapter stockTableAdaptor = new stockDataSetTableAdapters.stockTableAdapter();
              stockTableAdapter.Insert(txtItem.Text, txtPrice.Text);
          }
  
          //private void lstItem_SelectedIndexChanged(object sender, EventArgs e)
          //{
          //    int x = lstItem.SelectedIndex;
          //    lstPrice.SelectedIndices.Add(x);
          //}
  
          //private void lstPrice_SelectedIndexChanged(object sender, EventArgs e)
          //{
          //    int x = lstPrice.SelectedIndex;
          //    lstItem.SelectedIndices.Add(x);
          //}
  
          private void btnRemove_Click(object sender, EventArgs e)
          {
              if(1==1/*lstItem.SelectedItem == null*/)
              {
                  MessageBox.Show("You must select an Item first, Before you are able to delete it!");
              }
              else
              {
  
  
                  //Finding the row to delete in the database
                  //dboKanadaDataSetProducts.ProductsRow delProductsRow;
                  //delProductsRow = dboKanadaDataSetProducts.Products.FindByProductsID(string ProductName);
  
                  //START -------
                  // Locate the row to delete.
                  //NorthwindDataSet.RegionRow oldRegionRow;
                  //oldRegionRow = northwindDataSet.Region.FindByRegionID(5);
  
                  // Delete the row from the dataset
                  //oldRegionRow.Delete();
  
                  // Delete the row from the database 
                  //this.regionTableAdapter.Update(this.northwindDataSet.Region);
                  //END -------
  
                  //lstPrice.Items.Remove(lstPrice.SelectedItem);
                  //lstItem.Items.Remove(lstItem.SelectedItem);
              }
          }
  //I cannot be bothered to add spaces to this code, hence the misaligned code.
      private void btnClear_Click(object sender, EventArgs e)
      {
          //lstItem.Items.Clear();
          //lstPrice.Items.Clear();
      }
  
      private void txtPrice_TextChanged(object sender, EventArgs e)
      {
          //string context = this.txtPrice.Text;
          //for (int i = 0; i &lt; context.Length; i++)
          //{
          //    if (!char.IsNumber(context[i]))
          //    {
          //        MessageBox.Show("Only numbers are allowed in this field!");
          //        txtPrice.Text = txtPrice.Text.Remove(txtPrice.TextLength - 1);
          //    }
          //}
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          /*
          if (MessageBox.Show("Are you sure that you want to DELETE ALL LISTED ITEMS?", "WARNING: Delete All Records",
       MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
       == DialogResult.Yes)
          {
              int j = lstPrice.Items.Count;
              for (int i = lstItem.Items.Count; i &gt; 0; )
              {
  
                  lstItem.Items.Remove(lstItem.Items[--i]);
                  lstPrice.Items.Remove(lstPrice.Items[--j]);
              }
          }*/
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          this.Hide();
          frmOrder makeOrder = new frmOrder();
          makeOrder.Show();
      }
  
      //private void frmProducts_Load(object sender, EventArgs e)
      //{
      //    // TODO: This line of code loads data into the 'dboKanadaDataSetProducts.Products' table. You can move, or remove it, as needed.
      //    //this.productsTableAdapter.Fill(this.dboKanadaDataSetProducts.Products);
  
      //}
  
      private void frmProducts_Load_1(object sender, EventArgs e)
      {
          // TODO: This line of code loads data into the 'stockDataSet.stock' table. You can move, or remove it, as needed.
          //this.stockTableAdapter.Fill(this.stockDataSet.stock);
          dsoc = new stockDataSet();
          conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\stock.accdb");
          da = new OleDbDataAdapter("Select * from stock", conn);
          OleDbCommandBuilder cmdBl = new OleDbCommandBuilder(da);
          da.Fill(dsoc, "stock");
          dataGridView2.DataSource = dsoc;
          dataGridView2.DataMember = "stock";
      }
  
  
      private void btnSave_Click(object sender, EventArgs e)
      {
          try
          {
              //this.Validate();
              //this.stockBindingSource.EndEdit();
              //this.stockTableAdapter.Update(this.stockDataSet.stock);
              //MessageBox.Show("Update successful");
              //da.Update(stockDataSet.Tables["stock"]);
  
              System.Data.OleDb.OleDbConnection oledb1 = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\stock.accdb");
              System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
              cmd.CommandType = System.Data.CommandType.Text;
              cmd.CommandText = "INSERT stock (proName, proPrice) VALUES ("+ txtItem.Text +", " + txtPrice.Text + ")";
              cmd.Connection = oledb1;
  
              oledb1.Open();
              cmd.ExecuteNonQuery();
              oledb1.Close();
  
          }
          catch (System.Exception ex)
          {
              MessageBox.Show("Update failed");
          }
  
  
  
          //this.dataGridView1.EndEdit();
      }
  
      private void btnMenu_Click(object sender, EventArgs e)
      {
          this.Hide();
          frmMenu showMenu = new frmMenu();
          showMenu.Show();
      }
  }
  }
  </code></pre>