Moving Data From One Table To Another, Dynamically, In Accounting Module

I am going to share an issue that I faced while developing a module for my accounting software. I thought of sharing my problem with you, along with the solution of how I solved it. I have also attached the screenshots as well as the hard code, so that you can download it and understand how it is working.
 
I was given a task wherein the user can create the Barcode for a product using the product fields and it should be dynamic, so that  the user can choose the fields he needs to be included in Barcode.
 
Now, I will start explaining how the code works, to accomplish this task. I created three static tables.
 
The First Table
  1. public DataTable Items()  
  2.         {  
  3.             DataTable tblItm = new DataTable();  
  4.             tblItm.Columns.Add("StockNo"typeof(int));  
  5.             tblItm.Columns.Add("Price"typeof(decimal));  
  6.             tblItm.Columns.Add("qty"typeof(int));  
  7.             tblItm.Rows.Add(9012, 100, 1);  
  8.             tblItm.Rows.Add(9023, 200, 2);  
  9.             tblItm.Rows.Add(9034, 300, 3);  
  10.             tblItm.Rows.Add(9045, 400, 4);  
  11.             tblItm.Rows.Add(9056, 500, 5);  
  12.             return tblItm;  
  13.         } 
In this table, we hold the data which the customer can include in the barcode, as per the setting table which is the next one. In this sample code, a customer will choose products as per his needs. 
 
Settings Table
  1. public DataTable setting()  
  2.         {  
  3.             DataTable sett = new DataTable();  
  4.             sett.Columns.Add("FieldName"typeof(string));  
  5.             sett.Columns.Add("Fieldvalue"typeof(int));  
  6.             sett.Rows.Add("StockNo", 1);  
  7.             sett.Rows.Add("Price", 0);  
  8.             sett.Rows.Add("qty", 1);  
  9.             return sett;  
  10.         } 
I have used this table to put the filter. Only those fields will be copied or inserted, which have "FieldValue"=1;
 
 
The Third Table 
 
In this table, I don't have data and I have inserted data in this table by applying the filter in First table, using setting table. You will see the data in this table of StockNo & Qty.
  1. public DataTable InsertSecondTblItems()  
  2.         {  
  3.             DataTable tbl = new DataTable();  
  4.             tbl.Columns.Add("StockNo"typeof(int));  
  5.             tbl.Columns.Add("Price"typeof(decimal));  
  6.             tbl.Columns.Add("qty"typeof(double));  
  7.             return tbl;  
  8.         } 
Here is the output table. We are only getting StockNo & Qty because we have applied the filter i.e if Filedvalue=1 on it. Else, it would have been blank. Price is blank. The code to filter data from settings table and insert data first table to third table is,
  1. public DataTable Startjob(DataTable tblItm, DataTable Inserttbl, DataTable Settings)  
  2.         {  
  3.             string columnsarr = "";  
  4.             foreach (DataRow dRow in Settings.Rows)  
  5.             {  
  6.                 if (Convert.ToInt32(dRow["Fieldvalue"]) == 1)  
  7.                 {  
  8.                     columnsarr = columnsarr + "," + dRow["FieldName"];  
  9.                 }  
  10.             }  
  11.             DataRow r;  
  12.             columnsarr = columnsarr.Remove(0, 1);  
  13.             string[] columnarray = columnsarr.Split(',');  
  14.   
  15.             foreach (DataRow dr in tblItm.Rows)  
  16.             {  
  17.                 r = Inserttbl.NewRow();  
  18.                 for (int i = 0; i <= columnarray.Length - 1; i++)  
  19.                 {  
  20.                     r[columnarray[i]] = dr[columnarray[i]];  
  21.                 }  
  22.                 Inserttbl.Rows.Add(r);  
  23.             }  
  24.             return Inserttbl;  
  25.         } 
 
It's my first article on C# Corner. I am very excited to write on this forum for the first time. I have also uploaded the source code so that you can get a better idea of what I am trying to achieve.
 
Hope this will help some of our developer friends. If you have any doubts regarding the code or article, please feel free to write to me.

Up Next
    Ebook Download
    View all
    Learn
    View all