Click here to Skip to main content
12,760,621 members (32,099 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# SQL-Server
Here's the code :-
DataTable mDT_RptHead = new DataTable();
 mDT_RptHead = mDBHelper.GetTable("Select * from rptpo_head");
 mDBHelper.ExecuteSQLNonQuery("delete from rptpo_head");

 DataTable mDT_PoHead = new DataTable();
 mDT_PoHead = mDBHelper.GetTable("SELECT * FROM PO_HEAD WHERE PO_NO='D0003' AND             PO_DATE =CONVERT(DATETIME,'13/11/2011',103) and profcen_cd='1'");

if (mDT_PoHead.Rows.Count > 0)
      DataRow newRow = mDT_RptHead.NewRow();
      foreach (DataRow row in mDT_PoHead.Rows)
             foreach (DataColumn col in mDT_PoHead.Columns)
                   mDT_RptHead.Rows[0][col.ColumnName] = row[col];

After running the code, data properly inserted in mDT_RepHead.Now i want to update the table "rptpo_head" from which i fetch the corresponding data.

Need Suggestion.
Posted 13-Nov-11 0:09am
Updated 13-Nov-11 0:21am
Amir Mahfoozi 13-Nov-11 7:46am
what do you want to update in rptpo_head ?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It's a bit hard to tell since there is some magic going on in the mDBHelper.
Judging from the rest of your code I think it uses SqlConnection[^], SqlCommand[^], SqlDataAdapter[^] and DataSets[^] though.
In that case your best bet is the DataAdapter.Update Method[^].
Possibly use a SqlCommandBuilder Object[^] to auto-generate your update statements.
More on ADO.NET[^].
Hope it helps :)
Mayank Topiwala 13-Nov-11 6:21am
Dear Naerling

mDBHelper is the object of class DBHelper that has already created in my module and with the help of static methods (like GetTable, ExecuteSQLNonQuery etc.), I can easily fetch relevant data. But the thing is without using the insert query how i can insert the data in "rptpo_head".I have already collected these data in the data table mDT_RptHead using foreach loop as shown above.

Naerling 13-Nov-11 7:17am
I don't see why you could not use the insert query. However, if your mDBHelper Object can't help you here I think you are making things hard on yourself and you should check your design.
The alternative is looping through the datatable again and writing your own update query that you can execute using a SqlCommand Object. You can pass the values of your current DataRow to the SqlCommand Object as parameters. However, you should be wary of the fact that update and insert and delete should be handled seperatly. Seems like a lot extra work if the DataAdapter already provides an Update Method that does all this work for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170217.1 | Last Updated 13 Nov 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100