4

So my title is pretty self explanatory, so thus far I have read Microsofts Documentation and watched some Youtube videos regarding my issue here.

Firstly my project code here:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

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

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (DataRow r in dsEquipment.Tables[0].Rows)
            {
                DataRow dr = sQTDBDataSet.tblEquipment.NewRow();
                dr[0] = r[0];
                dr[1] = r[1];
                dr[2] = r[2];
                dr[3] = r[3];
                dr[4] = r[4];
                dr[5] = r[5];
                dr[6] = r[6];
                dr[7] = r[7];
                dr[8] = r[8];
                dr[9] = r[9];
                dr[10] = r[10];
                dr[11] = r[11];
                dr[12] = r[12];
                dr[13] = r[13];
                dr[14] = r[14];
                dr[15] = r[15];
                dr[16] = r[16];

                sQTDBDataSet.tblEquipment.Rows.Add(dr);
            }
            //tblEquipmentTableAdapter.Update(sQTDBDataSet.tblEquipment);
            //tableAdapterManager.UpdateAll(sQTDBDataSet);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                this.tblEquipmentBindingSource.EndEdit();
                this.tblEquipmentTableAdapter.Update(this.sQTDBDataSet.tblEquipment);
                MessageBox.Show("Update successful");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Update failed");
            }
        }

        private void btnReadExcel_Click(object sender, EventArgs e)
        {
            OpenFileDialog OFD = new OpenFileDialog();

            if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string strfilename = OFD.FileName;
                txtFileName.Text = strfilename;
            }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void tblEquipmentBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.tblEquipmentBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.sQTDBDataSet);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'sQTDBDataSet.tblEquipment' table. You can move, or remove it, as needed.
            this.tblEquipmentTableAdapter.Fill(this.sQTDBDataSet.tblEquipment);

        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            try
            {
                // Establish connection between the c# application and the excel file.
                OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + txtFileName.Text + ";Extended Properties=Excel 12.0");
                // Reading the data from the excel file.
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Equipments$]", con);
                // All data from the file will be loaded into the dataset.
                da.Fill(dsEquipment);
                // Show in a message box how many rows of data there is. 
                //MessageBox.Show(dsEquipment.Tables[0].Rows.Count.ToString());
                // Show the data in the data grid view.
                dgEquipment.DataSource = dsEquipment.Tables[0];
            }
            catch
            {
                MessageBox.Show("Please select the SQT2 Excel sheet.");
            }
        }
    }
}

So my first attempt at tackling the problem was this:

            //tblEquipmentTableAdapter.Update(sQTDBDataSet.tblEquipment);
            //tableAdapterManager.UpdateAll(sQTDBDataSet);

I get no errors but for some reason my Access Database is not showing the updates.

My second attempt was the following:

private void btnOpenFile_Click(object sender, EventArgs e)
{
    try
    {
        // Establish connection between the c# application and the excel file.
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + txtFileName.Text + ";Extended Properties=Excel 12.0");
        // Reading the data from the excel file.
        OleDbDataAdapter da = new OleDbDataAdapter("select * from [Equipments$]", con);
        // All data from the file will be loaded into the dataset.
        da.Fill(dsEquipment);
        // Show in a message box how many rows of data there is. 
        //MessageBox.Show(dsEquipment.Tables[0].Rows.Count.ToString());
        // Show the data in the data grid view.
        dgEquipment.DataSource = dsEquipment.Tables[0];
    }
    catch
    {
        MessageBox.Show("Please select the SQT2 Excel sheet.");
    }
}

Which was a seperate button and I obtained this code from Microsofts documentation and changed the variables of the dataset and the database table, no errors... yet STILL my access database is not updating!

Completely stuck right now and any help is appreciated! :)

Thank you!

  • You might want to check your group policy permissions for file access. I recall having similar issues when using MS Office / Access connection providers. – FrugalTPH Jan 05 '20 at 18:05
  • I can't see anything in your code which is connecting to an access file (.accdb or .mdb) and attempting to write your data to it. Your C# code appears to allow you to select an excel file, which is duly loaded into memory in your C# app, and there it sits (I presume you can see it in the datagridview you mention?). You haven't included the definition of tblEquipmentTableAdapter, so its hard to say what's wrong. That is presumably where the bulk of the data export to Access functionality sits. – FrugalTPH Jan 05 '20 at 18:19
  • Also FYI, it is probably easier to use Access to transfer data from Excel, rather than going through a C# app as an intermediary. There are a good set of data import tools & cleanup tools built into Access these days (no VBA required). – FrugalTPH Jan 05 '20 at 18:19

0 Answers0