iiAaronXiX
iiAaronXiX

Reputation: 93

Updating the value(s) in an MS Access database

I am trying to set up a system that allows the user to alter the values of stock in an MS Access database. It is supposed to update the stock value of the SKU that has been entered on a previous form. Instead I don't get any error message, the program just sits there and doesn't do anything. The record does not get updated, no exceptions are thrown and I'm 99% certain the connection string is valid.

I had set it up so that once the operation is complete, it notified the user with a popup but this doesn't display either. Any suggestions would be appreciated.

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 InventoryManager
{
    public partial class frmAdjustment : Form
    {
        frmAmendStock _main;

        public string enteredSKU { get; set; }

        public frmAdjustment(frmAmendStock main)
        {
            InitializeComponent();
            _main = main;
        }        

        private void frmAdjustment_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btnSubmit;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            using (OleDbConnection connect = new OleDbConnection())
            {

                connect.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Other\Documents\University Work\USB\Individual Project\Artefact\InventoryManager\InventoryManager\stock.mdb";
                connect.Open();

                OleDbCommand cmd = new OleDbCommand("UPDATE items SET Stock = @stock, Stock_Counted = @counted WHERE SKU ='" +enteredSKU+"'", connect);
                string units = txtAmount.Text;

                    if (connect.State == ConnectionState.Open)
                    {
                        if (string.IsNullOrEmpty(units))
                        {
                            MessageBox.Show("Please enter the correct amount of units.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            cmd.Parameters.Add("@stock", OleDbType.Integer, 5).Value = txtAmount.Text;
                            cmd.Parameters.Add("@counted", OleDbType.Integer, 5).Value = txtAmount.Text;

                            try
                            {
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Stock Adjusted", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);

                                txtAmount.Clear();

                                connect.Close();
                            }
                            catch (Exception expe)
                            {
                                MessageBox.Show(expe.Source);
                                connect.Close();
                            }
                      }
                }
                else
                {
                    MessageBox.Show("Connection Failed");
                }
            }
        }
    }
}

After updating the code I now get this error:

Microsoft JET Database Engine error

Upvotes: 0

Views: 87

Answers (2)

iiAaronXiX
iiAaronXiX

Reputation: 93

Managed to fix the issue and get it to save to the database. Thank you for everyone's help and suggestions.

Here's the working 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 InventoryManager
{
    public partial class frmAdjustment : Form
    {
        frmAmendStock _main;

        public string enteredSKU { get; set; }

        public frmAdjustment(frmAmendStock main)
        {
            InitializeComponent();
            _main = main;
        }        

        private void frmAdjustment_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btnSubmit;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            using (OleDbConnection connect = new OleDbConnection())
            {

                connect.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Other\Documents\University Work\USB\Individual Project\Artefact\InventoryManager\InventoryManager\stock.mdb";
                connect.Open();

                OleDbCommand cmd = new OleDbCommand("UPDATE items SET Stock = @stock, Stock_Counted = @counted WHERE SKU LIKE '" +enteredSKU+"'", connect);
                string units = txtAmount.Text;

                    if (connect.State == ConnectionState.Open)
                    {
                        if (string.IsNullOrEmpty(units))
                        {
                            MessageBox.Show("Please enter the correct amount of units.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            cmd.Parameters.Add("@stock", OleDbType.Integer, 5).Value = txtAmount.Text;
                            cmd.Parameters.Add("@counted", OleDbType.Integer, 5).Value = txtAmount.Text;

                            try
                            {
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Stock Adjusted", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);

                                txtAmount.Clear();

                                connect.Close();
                            }
                            catch (Exception expe)
                            {
                                MessageBox.Show(expe.ToString());
                                connect.Close();
                            }
                      }
                }
                else
                {
                    MessageBox.Show("Connection Failed");
                }
            }
        }
    }
}

Upvotes: 1

Gord Thompson
Gord Thompson

Reputation: 123399

Your code to perform the update is inside the if (string.IsNullOrEmpty(units)) block, so if units actually has a value then the code will never be executed. It looks like you need an else after your error message.

Upvotes: 1

Related Questions