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
- public DataTable Items()
- {
- DataTable tblItm = new DataTable();
- tblItm.Columns.Add("StockNo", typeof(int));
- tblItm.Columns.Add("Price", typeof(decimal));
- tblItm.Columns.Add("qty", typeof(int));
- tblItm.Rows.Add(9012, 100, 1);
- tblItm.Rows.Add(9023, 200, 2);
- tblItm.Rows.Add(9034, 300, 3);
- tblItm.Rows.Add(9045, 400, 4);
- tblItm.Rows.Add(9056, 500, 5);
- return tblItm;
- }
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
- public DataTable setting()
- {
- DataTable sett = new DataTable();
- sett.Columns.Add("FieldName", typeof(string));
- sett.Columns.Add("Fieldvalue", typeof(int));
- sett.Rows.Add("StockNo", 1);
- sett.Rows.Add("Price", 0);
- sett.Rows.Add("qty", 1);
- return sett;
- }
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.
- public DataTable InsertSecondTblItems()
- {
- DataTable tbl = new DataTable();
- tbl.Columns.Add("StockNo", typeof(int));
- tbl.Columns.Add("Price", typeof(decimal));
- tbl.Columns.Add("qty", typeof(double));
- return tbl;
- }
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,
- public DataTable Startjob(DataTable tblItm, DataTable Inserttbl, DataTable Settings)
- {
- string columnsarr = "";
- foreach (DataRow dRow in Settings.Rows)
- {
- if (Convert.ToInt32(dRow["Fieldvalue"]) == 1)
- {
- columnsarr = columnsarr + "," + dRow["FieldName"];
- }
- }
- DataRow r;
- columnsarr = columnsarr.Remove(0, 1);
- string[] columnarray = columnsarr.Split(',');
-
- foreach (DataRow dr in tblItm.Rows)
- {
- r = Inserttbl.NewRow();
- for (int i = 0; i <= columnarray.Length - 1; i++)
- {
- r[columnarray[i]] = dr[columnarray[i]];
- }
- Inserttbl.Rows.Add(r);
- }
- return Inserttbl;
- }
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.