473,657 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to update data from DataSet to Excel

1. I can fill data from Excel to DataSet like this:

OleDbConnection connection = new
OleDbConnection (@"Provider=Mic rosoft.Jet.OLED B.4.0;Data
Source=../../Book1.xls;Exten ded Properties=Exce l 8.0;");

connection.Open ();

OleDbCommand command =new OleDbCommand("S ELECT * FROM myRange1",
connection);

connection.Clos e();

OleDbDataAdapte r dataAdapter = new OleDbDataAdapte r();
dataAdapter.Sel ectCommand = command;
DataSet dataSet = new DataSet();

dataAdapter.Fil l(dataSet);
Then OK!!!

2. But I can't update it from dataset to Excel by:
connection.Open ();
dataAdapter.Upd ateCommand = new OleDbCommand("U PDATE myRange1 SET FirstName
= @FirstName, LastName = @LastName", connection);
dataAdapter.Upd ate(this.dataSe t);
connection.Clos e();
How can I do it???? :-(
Nov 16 '05 #1
3 19721
JacksonYin,

OLEDB doesn't support named parameters, only positional ones. I believe
you have to do something like this:

connection.Open ();
dataAdapter.Upd ateCommand = new OleDbCommand("U PDATE myRange1 SET FirstName
= ?, LastName = ?", connection);

// At this point, add the parameters, making sure that the firstname
parameter is
// added first, and the last name parameter is added second.

dataAdapter.Upd ate(this.dataSe t);
connection.Clos e();

That should work.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"JacksonYin " <Ja************ @hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
1. I can fill data from Excel to DataSet like this:

OleDbConnection connection = new
OleDbConnection (@"Provider=Mic rosoft.Jet.OLED B.4.0;Data
Source=../../Book1.xls;Exten ded Properties=Exce l 8.0;");

connection.Open ();

OleDbCommand command =new OleDbCommand("S ELECT * FROM myRange1",
connection);

connection.Clos e();

OleDbDataAdapte r dataAdapter = new OleDbDataAdapte r();
dataAdapter.Sel ectCommand = command;
DataSet dataSet = new DataSet();

dataAdapter.Fil l(dataSet);
Then OK!!!

2. But I can't update it from dataset to Excel by:
connection.Open ();
dataAdapter.Upd ateCommand = new OleDbCommand("U PDATE myRange1 SET
FirstName = @FirstName, LastName = @LastName", connection);
dataAdapter.Upd ate(this.dataSe t);
connection.Clos e();
How can I do it???? :-(

Nov 16 '05 #2
JacksonYin,

As an alternate method you can create an OleDbCommand object and use
ExecuteNonQuery against the spreadsheet to insert/update/delete. I have
posted some sample code below.

I hope this helps.
---------------------

OleDbConnection conn = new OleDbConnection (strConn);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "Insert into [Sheet1$] (FirstName, LastName)" +
" values ('Fred', 'Flintstone')";
cmd.ExecuteNonQ uery();
cmd.CommandText = "UPDATE [Sheet1$] SET FirstName = 'Homer', LastName =
'Simpson' " +
" WHERE FirstName = 'Joe'";
cmd.ExecuteNonQ uery();
conn.Close();
Nov 16 '05 #3
Thank you very much! ;-)

"JacksonYin " <Ja************ @hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
1. I can fill data from Excel to DataSet like this:

OleDbConnection connection = new
OleDbConnection (@"Provider=Mic rosoft.Jet.OLED B.4.0;Data
Source=../../Book1.xls;Exten ded Properties=Exce l 8.0;");

connection.Open ();

OleDbCommand command =new OleDbCommand("S ELECT * FROM myRange1",
connection);

connection.Clos e();

OleDbDataAdapte r dataAdapter = new OleDbDataAdapte r();
dataAdapter.Sel ectCommand = command;
DataSet dataSet = new DataSet();

dataAdapter.Fil l(dataSet);
Then OK!!!

2. But I can't update it from dataset to Excel by:
connection.Open ();
dataAdapter.Upd ateCommand = new OleDbCommand("U PDATE myRange1 SET
FirstName = @FirstName, LastName = @LastName", connection);
dataAdapter.Upd ate(this.dataSe t);
connection.Clos e();
How can I do it???? :-(

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7550
by: vinay | last post by:
I have read the excel file into the datagrid and modified some data. Now i want to update the excel file with the data. How do I do it. Please help me.
2
1944
by: Chris Fink | last post by:
Any idea why the database table is not being updated with the contents of the excel file loaded into the dataset? Any help is appreciated! // load database table, shell only string cnString = "DSN=XXX D5200"; OdbcConnection myConnection = new OdbcConnection(cnString); OdbcDataAdapter da = new OdbcDataAdapter("select * from DB where 1=2;", myConnection); DataSet ds = new DataSet(); da.Fill(ds, "ccginput"); // load excel file into...
6
4186
by: certolnut | last post by:
HI all New to C# I'm merging datasets. The source dataset is an excel file and the target dataset is in SqlServer. salesTracingCommisionNDCdataset1.Merge(ndcExcel,false,MissingSchemaAction.Error)
9
3382
by: Brian Hanson | last post by:
Hi, I have an unusual problem that just showed its ugly head at a pretty bad time. I have an asp.net (VB) app that takes data from an Excel sheet and puts it into SQL Server. I get the data out of Excel using OleDB, and suddenly, some of the data was not being extracted from Excel. I use OleDb for the extract into a DataTable and from there an SqlClient.SqlCommand to put it into SQL Server.
1
6785
by: Luis Esteban Valencia | last post by:
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0117: 'System.Data.DataTable' does not contain a definition for 'WriteXml' Source Error:
4
2299
by: washoetech | last post by:
Hello, I am working on a project where I need to be able to grab the data from an Excel spreadsheet and create a new table in my database based on the columns in the spreadsheet. After the table is created then I need to fill the table with the data from that spreadsheet. I am using ASP.NET 2.0 and SQL 2000 for my database. What would be the best way to do this? Should I go Excel to XML or Excel to a Dataset?
1
2635
by: Ramakrishnan Nagarajan | last post by:
Hi, I am converting Excel data into a Dataset in C#. There are around 24 columns in the Excel Sheet. First I tried to insert one row with correct values in the Excel sheet. i.e. for text columns I entered text values and for numeric columns I entered numeric values. It works fine and pass through all the validation checks and gets inserted into the database successfully. But when I gave some junk values in the excel sheet and tried to...
1
6119
by: Anonieko | last post by:
Here are some of the approaches. 1. Transform DataGrid http://www.dotnetjohn.com/articles.aspx?articleid=36 3. Use the Export approach http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asp
5
4740
by: mark | last post by:
I've been looking at working with Excel data. I understand the process of getting the data into a dataset and modifying it. It's one of simple beauty that is well documented. Now, I want to send the updated data set back. I suspect this is also simple but it eludes me. I have: Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim Conn As System.Data.OleDb.OleDbConnection
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.