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
|