How do I export data from a datagridview/data set?

Hotpots

New member
Joined
Apr 19, 2016
Messages
1
Programming Experience
1-3
Hey,

So at the moment I can import an excel file into my c# application and view it in a datagridview, next I have rearranged the data into 17 different headings and displayed it in another datagridview and now I am trying to import that into an Access Database, although I am getting no errors the Access database is not being updated and I'm totally stumped:

My code below for updating the access database:

tblEquipmentTableAdapter.Update(sQTDBDataSet);



My whole project code:
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);
            //tblEquipmentTableAdapter.Update(SQTDBDataSet);
        }

        private void btnReadExcel_Click(object sender, EventArgs e)
        {
            // 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 [Equipment$]", 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];
        }

        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);

        }
    }
}


Any help is hugely appreciated.
 
That Update method that you're calling on your table adapter returns a number which, in your case, represents the number of records inserted into the database. When you make that call, there are only three possible outcomes:

1. The call fails and an exception is thrown.
2. The call succeeds and returns zero, which means that there were no changes to save.
3. The call succeeds and returns a non-zero value, which means that there were changes and they were saved.

Which is it in your case? I suspect that it's number 3, which would mean that everything is working as it should and you're just not looking in the right place for the data. That is a very common problem. In that case, I suggest that you follow the first link in my signature below to learn how local data files are managed.
 
Back
Top Bottom