Click here to Skip to main content
11,581,353 members (60,013 online)
Rate this: bad
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 12-Nov-11 23:09pm
Edited 12-Nov-11 23:21pm
Amir Mahfoozi at 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 Smile | :)
Mayank Topiwala at 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 at 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
0 OriginalGriff 260
1 Sergey Alexandrovich Kryukov 155
2 virusstorm 114
3 Frankie-C 105
4 RyanDev 84
0 OriginalGriff 1,183
1 Sergey Alexandrovich Kryukov 825
2 Abhinav S 597
3 F-ES Sitecore 500
4 Suvendu Shekhar Giri 433

Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 13 Nov 2011
Copyright © CodeProject, 1999-2015
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