Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 1st, 2004, 01:54 AM
Authorized User
 
Join Date: Aug 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to update the datasource from using dataset

hi all

I have this Xml file to load in Dataset and below is the code to update the existing records in the datasource table customer
when i run the below code it displays this error
Error : - Update requires a valid InsertCommand when passed DataRow collection with new rows
i am just trying to update the Company Name column by passing the Customer ID .the records for below given customer Id already exist in table and it is a primary key column.
i don't known where iam wrong , can any body look at the below given code and give me your good ideas.

Very Urgent.......:)
Thank u very much
S Javid
KSA

<Customers>
 <Customers>
  <CustomerID>CHOPS</CustomerID>
  <CompanyName>Cereals</CompanyName>
 </Customers>
 <Customers>
  <CustomerID>BOLID</CustomerID>
  <CompanyName>Dairy Products</CompanyName>
 </Customers>
 <Customers>
  <CustomerID>FISSA</CustomerID>
  <CompanyName>Confections</CompanyName>
 </Customers>
 <Customers>
  <CustomerID>ANATR</CustomerID>
  <CompanyName>Condiments</CompanyName>
 </Customers>
 <Customers>
  <CustomerID>AROUT</CustomerID>
  <CompanyName>Beverages</CompanyName>
 </Customers>
</Customers>
************************************************** ************************************************** *********************************************
here is a code to update the datasource
************************************************** ************************************************** ***********************************************
   string strConnectionString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind;Data Source=c01186;";
   System.Data.SqlClient.SqlConnection ObjConnection = new System.Data.SqlClient.SqlConnection(strConnectionS tring);

   // Put user code to initialize the page here
   FileStream ObjFileStream = new FileStream(Server.MapPath("data1.xml"),FileMode.Op en,FileAccess.Read);
   StreamReader ObjReader = new StreamReader(ObjFileStream);
   DataSet ObjDataSet = new DataSet();

   ObjDataSet.ReadXml(ObjReader,XmlReadMode.Auto);

   ObjFileStream.Close();

   foreach(DataTable ObjTable in ObjDataSet.Tables)
    {
     string strTableName = ObjTable.TableName;

     SqlDataAdapter da = new SqlDataAdapter();
     SqlCommand cmd;
     SqlParameter parm;
     // Create the SelectCommand.
     cmd = new SqlCommand("SELECT * FROM Customers ", ObjConnection);
     da.SelectCommand = cmd;
     // Create the UpdateCommand.
     cmd = new SqlCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " + "WHERE CustomerID = @oldCustomerID", ObjConnection);
     cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
     cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
     parm = cmd.Parameters.Add("@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
     parm.SourceVersion = DataRowVersion.Original;
     da.UpdateCommand = cmd;
     da.Update(ObjDataSet,strTableName);
    }

syedjavid
__________________
syedjavid
 
Old August 1st, 2004, 03:13 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

There are some rows in your ObjDataSet(XML file)that don't exist in your dataSource
because of it,the error informs,you need an InsertCommand.
HTH.

--------------------------------------------
Mehdi.
 
Old August 1st, 2004, 09:37 AM
Authorized User
 
Join Date: Aug 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Actually i am trying to modify the existing records in datasource.
i do not have any new records , here CustomerID is primary column and which is same as in
datasource and company name will be updated in the datasource.



syedjavid
 
Old August 1st, 2004, 02:58 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

dear syedjavid
please make another command and assign it to your da.InsertCommand
test it again for probable errors.


--------------------------------------------
Mehdi.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Gridview for inser,update without using datasource karthikeyan_pa ASP.NET 2.0 Professional 0 September 4th, 2007 07:58 AM
update db from dataset nikosnyc VS.NET 2002/2003 1 July 2nd, 2006 06:27 PM
Problem with Dataset Update Aaron Edwards ADO.NET 1 April 29th, 2005 04:26 AM
dataset to databse update nashnash ASP.NET 1.0 and 1.1 Professional 1 December 10th, 2004 01:05 PM
Dataset Update with Null Value [V] reyboy ADO.NET 2 June 1st, 2004 12:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.