How do I export data from a datagridview/data set into an Access Database? [C#] -
so title pretty self explanatory, far have read microsofts documentation , watched youtube videos regarding issue here.
firstly 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: line of code loads data 'sqtdbdataset.tblequipment' table. can move, or remove it, needed. this.tblequipmenttableadapter.fill(this.sqtdbdataset.tblequipment); } private void btnopenfile_click(object sender, eventargs e) { try { // establish connection between c# application , excel file. oledbconnection con = new oledbconnection(@"provider=microsoft.ace.oledb.12.0;data source=" + txtfilename.text + ";extended properties=excel 12.0"); // reading data excel file. oledbdataadapter da = new oledbdataadapter("select * [equipments$]", con); // data file loaded dataset. da.fill(dsequipment); // show in message box how many rows of data there is. //messagebox.show(dsequipment.tables[0].rows.count.tostring()); // show data in data grid view. dgequipment.datasource = dsequipment.tables[0]; } catch { messagebox.show("please select sqt2 excel sheet."); } } } }
so first attempt @ tackling problem this:
//tblequipmenttableadapter.update(sqtdbdataset.tblequipment); //tableadaptermanager.updateall(sqtdbdataset);
i no errors reason access database not showing updates.
my second attempt following:
private void btnopenfile_click(object sender, eventargs e) { try { // establish connection between c# application , excel file. oledbconnection con = new oledbconnection(@"provider=microsoft.ace.oledb.12.0;data source=" + txtfilename.text + ";extended properties=excel 12.0"); // reading data excel file. oledbdataadapter da = new oledbdataadapter("select * [equipments$]", con); // data file loaded dataset. da.fill(dsequipment); // show in message box how many rows of data there is. //messagebox.show(dsequipment.tables[0].rows.count.tostring()); // show data in data grid view. dgequipment.datasource = dsequipment.tables[0]; } catch { messagebox.show("please select sqt2 excel sheet."); } }
which seperate button , obtained code microsofts documentation , changed variables of dataset , database table, no errors... yet still access database not updating!
completely stuck right , appreciated! :)
thank you!
Comments
Post a Comment