1

I need help here with part of my code so here it is:

I have 6 methods as you can see below that parse incoming data and then returns it as a list, so my issue is to send that list data to my database table SerialNumber, each method of the lists is a separate field that will fill a database column.

So for example the parse material will fill the database materiallookupcode column and the same for the others.

Here is an image of the database table enter image description here

Here is the code of all 5 methods that reads the data and then returns it and I need this data send to my database

private List<string> ParseMaterial()
        {
            var materialList = new List<string>();

            foreach (var material in _connection.GetBarcodeList())
            {
                materialList.Add(material.Substring(10, 5));
            }

            return materialList;
        }

        private List<string> ParseLot()
        {
            var lotList = new List<string>();
            var establishmentList = GetEstablishmentCode();

            foreach (var lot in _connection.GetBarcodeList())
            {
                if (establishmentList.Contains("038"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.LoganSport038Property);
                }
                if (establishmentList.Contains("072"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.LouisaCounty072Property);
                }
                if (establishmentList.Contains("086"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.Madison086Property);
                }
                if (establishmentList.Contains("089"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.Perry089Property);
                }
                if (establishmentList.Contains("069"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.StormLake069Property);
                }
                if (establishmentList.Contains("088"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.Waterloo088Property);
                }
                if (establishmentList.Contains("265"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.GoodLetsVille265Property);
                }
                if (establishmentList.Contains("087"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.CouncilBluffs087Property);
                }
                if (establishmentList.Contains("064"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.Sherman064Property);
                }

            }

            return lotList;
        }

        private List<string> ParseSerialNumber()
        {
            var serialNumberList = new List<string>();

            foreach (var serialNumber in _connection.GetBarcodeList())
            {
                serialNumberList.Add(serialNumber.Substring(36, 10));
            }

            return serialNumberList;
        }

        public List<string> ParseNetWeight()
        {
            var netWeightList = new List<string>();


            foreach (var netWeight in _connection.GetBarcodeList())
            {            
                netWeightList.Add(netWeight.Substring(22, 4));
            }

            return netWeightList;
        }

        public List<string> ParseGrossWeight()
        {
            var grossWeightList = new List<string>();

            foreach (var grossWeight in _connection.GetBarcodeList())
            {
                grossWeightList.Add(grossWeight.Substring(22, 4));
            }

            return grossWeightList;
        }

        public List<string> FullBarcode()
        {
            var receiveFullBarcodeList = new List<string>();

            foreach (var fullBarcode in _connection.GetBarcodeList())
            {
                receiveFullBarcodeList.Add(fullBarcode);
            }

            return receiveFullBarcodeList;
        }

        public List<string> GetEstablishmentCode()
        {
            var establishmentList = new List<string>();

            foreach (var establishmentCode in _connection.GetBarcodeList())
            {
                establishmentList.Add(establishmentCode.Substring(36, 3));
            }

            return establishmentList;
        }

The issue is here the button when clicking will read all 5 methods and send it to the database, I am sure the part where I making the list of variables to a string and the separator part is wrong so I need how is the correct way to add those list to each column of the database

    private async void btn_SubmitData_Click(object sender, EventArgs e)
              {
                 // parse list methodss
            var materialList = ParseMaterial();
            var lotList = ParseLot();
            var netWeightList = ParseNetWeight();
            var grossWeightList = ParseGrossWeight();
            var serialNumberList = ParseSerialNumber();
            var fullSerialNumberList = FullBarcode();

            var material = "";
            var lot = "";
            var net = "";
            var gross = "";
            var serial = "";
            var fullSerial = "";

            var currentUser = _currentUser.GetCurrentUsernameOnApp();
            var licensePlateId = GetLicensePlateIds();

            for (var i = 0; i < _connection.GetBarcodeList().Count; i++)
            {
                 material = materialList[i];
                 lot = lotList[i];
                 net = netWeightList[i];
                 gross = grossWeightList[i];
                 serial = serialNumberList[i];
                 fullSerial = fullSerialNumberList[i];
            }

            // database table and columns
            var serialNumbersInsert = new List<SerialNumber>
            {
                new SerialNumber
                {
                    SerialNumberLookupCode = serial,
                    NetWeight = Convert.ToDecimal(net) / 100,
                    GrossWeight = Convert.ToDecimal(gross) / 100,
                    LotLookupCode = lot,
                    MaterialLookupCode = material,
                    FullSerialNumberLookupCode = fullSerial,
                    CreatedSysDateTime = DateTime.Now,
                    ModifiedSysDateTime = DateTime.Now,
                    CreatedSysUser = currentUser,
                    ModifiedSysUser = currentUser,
                    LicensePlateId = licensePlateId
                }
            };

            // insert to the database
            foreach (var list in serialNumbersInsert)
            {
                _unitOfWork.SerialNumbers.Add(list);
            }
            await _unitOfWork.Complete();
}

Here is the SerialNumber domain class that represents a database table using a code first migration

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BarcodeReceivingApp.Core.Domain
{
    // domain class, represents a database table in sql server using code 
    // first migration
    public class SerialNumber
    {
        public int Id { get; set; }
        public int LicensePlateId { get; set; }
        public string FullSerialNumberLookupCode { get; set; }
        public string SerialNumberLookupCode { get; set; }
        public decimal NetWeight { get; set; }
        public decimal GrossWeight { get; set; }
        public string LotLookupCode { get; set; }
        public string MaterialLookupCode { get; set; }
        public DateTime CreatedSysDateTime { get; set; }
        public DateTime ModifiedSysDateTime { get; set; }
        public string CreatedSysUser { get; set; }
        public string ModifiedSysUser { get; set; }

    }
}

I search other places but could not find a good solution so far, so any help is appreciate it.

16
  • anyone can see my issue and get some solution if possible? Commented Feb 27, 2019 at 17:19
  • can someone let me know a solution on my issue? Commented Feb 27, 2019 at 22:05
  • Your question is not clear. You have these lists of strings and a SerialNumber class that you have not shown where you are assigning lists to the properties. I expect you are trying to somehow combine the lists or use them as lookups? I can't tell. Commented Feb 28, 2019 at 17:43
  • 1
    There are may ways. Ideally, if all those fields are in a single table you would parse it to single record instead of several lists, do your validation and cleanup, and then insert the whole record at once. Commented Feb 28, 2019 at 18:44
  • 1
    here, here and here are a few for starters. Commented Feb 28, 2019 at 19:06

1 Answer 1

0

I was able to resolve my question, what I did is to assign all lists in a loop and then assign them to each column in the database.

But I am still searching for a better and more clean way to this solution

private async void btn_SubmitData_Click(object sender, EventArgs e)
    {
        // parse list methods - represents each field of the database column
        var materialList = ParseMaterial();
        var lotList = ParseLot();
        var netWeightList = ParseNetWeight();
        var grossWeightList = ParseGrossWeight();
        var serialNumberList = ParseSerialNumber();
        var fullSerialNumberList = FullBarcode();
        var currentUser = _currentUser.GetCurrentUsernameOnApp();
        var licensePlateId = GetLicensePlateIds();


        for (var i = 0; i < _connection.GetBarcodeList().Count; i++)
        {
             var serialNumbersInsert = new List<SerialNumber>
             {
                 new SerialNumber
                 {
                     SerialNumberLookupCode = materialList[i],
                     NetWeight = Convert.ToDecimal(netWeightList[i]) / 100,
                     GrossWeight = Convert.ToDecimal(grossWeightList[i]) / 100,
                     LotLookupCode = lotList[i],
                     MaterialLookupCode = materialList[i],
                     FullSerialNumberLookupCode = fullSerialNumberList[i],
                     CreatedSysDateTime = DateTime.Now,
                     ModifiedSysDateTime = DateTime.Now,
                     CreatedSysUser = currentUser,
                     ModifiedSysUser = currentUser,
                     LicensePlateId = licensePlateId
                 }
             };

             foreach (var list in serialNumbersInsert)
             {
                 _unitOfWork.SerialNumbers.Add(list);
             }
             await _unitOfWork.Complete();
        }

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.