Somad Somad - 26 days ago 13
C# Question

Entity Framework Post method

I have two tables(Testresult & SampleCollection) linked to each other in a database. I have created a Post controller in entity framework that is saving data to both tables at the same time. But there is a particular condition that should be applied in saving data to both tables. I have condition A, condition B, condition C and condition D:

Condition A is the following screenshot:

[
{
"TestResult": {
"iOutletStorageId": 50061,
"dLastDeliveryDate": "2016-9-14",
"szMarkingCertificate": "Tank 1: Mark Cert",
"iFirstResultId": "1",
"iSecondResultId": "2",
"szFirstResultRemark": "Tank 1: First Result Remarks",
"szSecondResultRemark": "Tank 1: Second Result Remarks 2",
"szSiteVisitNo": "SVN01",
"dTransdate": "2016-9-20 10:4:10.754",
"iOutletId": 756
},
"MySampleCollections": {
"iPPMSTestResultId": 0,
"szZonalLabelNumber": "Tank 1: Sample Lbl No Zonal",
"szZonalSampleQty": "275",
"szZonalSerialNumber": "Tank 1: Seal No. Zonal",
"szCLabLabelNumber": "Tank 1: Sample Lbl No C-Lab",
"szCLabSampleQty": "275",
"szCLabSerialNumber": "Tank 1: Seal No. C-Lab",
"szGSALabelNumber": "Tank 1: Sample Lbl No GSA",
"szGSASampleQty": "275",
"szGSASerialNumber": "Tank 1: Seal No. GSA",
"szOutletLabelNumber": "Tank 1: Sample Lbl No Outlet",
"szOutletSampleQty": "275",
"szOutletSerialNumber": "Tank 1: Seal No. Outlet",
"szOutletSampleRecievedBy": "Tank 1: Sample Received By Outlet",
"szRetentionLabelNumber": "Tank 1: Sample Lbl No Retention",
"szRetentionSampleQty": "275",
"szRetentionSerialNumber": "Tank 1: Seal No. Retention"
}
}
]


Condition B is the following screenshot:

[
{
"TestResult": {
"iOutletStorageId": 50062,
"dLastDeliveryDate": "2016-9-14",
"szMarkingCertificate": "Tank 2: Cert #",
"iFirstResultId": "1",
"iSecondResultId": "2",
"szFirstResultRemark": "Tank 2: First test Remarks",
"szSecondResultRemark": "Tank 2: Second Test Remarks",
"szSiteVisitNo": "SVN02",
"dTransdate": "2016-9-20 10:4:10.754",
"iOutletId": 756
},
"MySampleCollections": {
"iPPMSTestResultId": 0,
"szZonalLabelNumber": "Tank 1: Sample Lbl No Zonal",
"szZonalSampleQty": "275",
"szZonalSerialNumber": "Tank 1: Seal No. Zonal",
"szCLabLabelNumber": "Tank 1: Sample Lbl No C-Lab",
"szCLabSampleQty": "275",
"szCLabSerialNumber": "Tank 1: Seal No. C-Lab",
"szGSALabelNumber": "Tank 1: Sample Lbl No GSA",
"szGSASampleQty": "275",
"szGSASerialNumber": "Tank 1: Seal No. GSA",
"szOutletLabelNumber": "Tank 1: Sample Lbl No Outlet",
"szOutletSampleQty": "275",
"szOutletSerialNumber": "Tank 1: Seal No. Outlet",
"szOutletSampleRecievedBy": "Tank 1: Sample Received By Outlet",
"szRetentionLabelNumber": "Tank 1: Sample Lbl No Retention",
"szRetentionSampleQty": "275",
"szRetentionSerialNumber": "Tank 1: Seal No. Retention"
}
}
]


Condition C is the following screenshot:

[
{
"TestResult": {
"iOutletStorageId": 50063,
"dLastDeliveryDate": "2016-9-14",
"szMarkingCertificate": "Tank 2: Cert #",
"iFirstResultId": "1",
"iSecondResultId": "2",
"szFirstResultRemark": "Tank 2: First test Remarks",
"szSecondResultRemark": "Tank 2: Second Test Remarks",
"szSiteVisitNo": "SVN02",
"dTransdate": "2016-9-20 10:4:10.754",
"iOutletId": 756
},
"MySampleCollections": {
"iPPMSTestResultId": 0,
"szZonalLabelNumber": "",
"szZonalSampleQty": "",
"szZonalSerialNumber": "",
"szCLabLabelNumber": "",
"szCLabSampleQty": "",
"szCLabSerialNumber": "",
"szGSALabelNumber": "",
"szGSASampleQty": "",
"szGSASerialNumber": "",
"szOutletLabelNumber": "",
"szOutletSampleQty": "",
"szOutletSerialNumber": "",
"szOutletSampleRecievedBy": "",
"szRetentionLabelNumber": "",
"szRetentionSampleQty": "",
"szRetentionSerialNumber": ""
}
}
]


Condition D is the following screenshot:

[
{
"TestResult": {
"iOutletStorageId": 50063,
"dLastDeliveryDate": "2016-9-14",
"szMarkingCertificate": "Tank 2: Cert #",
"iFirstResultId": "1",
"iSecondResultId": "2",
"szFirstResultRemark": "Tank 2: First test Remarks",
"szSecondResultRemark": "Tank 2: Second Test Remarks",
"szSiteVisitNo": "SVN02",
"dTransdate": "2016-9-20 10:4:10.754",
"iOutletId": 756
}
}
]


Now i want to be saving two rows or more than a row at time.

When i pass condition A & B, It's saving right to the database
When i pass condition A & C, It's saving right to the database
When i pass condition A & D, I am getting an error.

The idea is that, the second table will get its ID from the first table.

And here is my code:

public int PostTestResultObj(List<TestAndSampleResult> testandsampleresult)
{
long testResultId = 0;
if (testandsampleresult != null)
foreach (TestAndSampleResult orderdet in testandsampleresult)
{
mdlTestResult NewTestResult = new mdlTestResult
{
iOutletStorageId = orderdet.TestResult.iOutletStorageId,
dLastDeliveryDate = orderdet.TestResult.dLastDeliveryDate,
szMarkingCertificate = orderdet.TestResult.szMarkingCertificate,
iFirstResultId = orderdet.TestResult.iFirstResultId,
iSecondResultId = orderdet.TestResult.iSecondResultId,
szFirstResultRemark = orderdet.TestResult.szFirstResultRemark,
szSecondResultRemark = orderdet.TestResult.szSecondResultRemark,
szSiteVisitNo = orderdet.TestResult.szSiteVisitNo,
dTransdate = orderdet.TestResult.dTransdate,
iOutletId = orderdet.TestResult.iOutletId
};
SampleCollections newsamplecollectionmodel = new SampleCollections
{
szZonalLabelNumber = orderdet.MySampleCollections.szZonalLabelNumber,
szZonalSampleQty = orderdet.MySampleCollections.szZonalSampleQty,
szZonalSerialNumber = orderdet.MySampleCollections.szZonalSerialNumber,
szCLabLabelNumber = orderdet.MySampleCollections.szCLabLabelNumber,
szCLabSampleQty = orderdet.MySampleCollections.szCLabSampleQty,
szCLabSerialNumber = orderdet.MySampleCollections.szCLabSerialNumber,
szGSALabelNumber = orderdet.MySampleCollections.szGSALabelNumber,
szGSASampleQty = orderdet.MySampleCollections.szGSASampleQty,
szGSASerialNumber = orderdet.MySampleCollections.szGSASerialNumber,
szOutletLabelNumber = orderdet.MySampleCollections.szOutletLabelNumber,
szOutletSampleQty = orderdet.MySampleCollections.szOutletSampleQty,
szOutletSerialNumber = orderdet.MySampleCollections.szOutletSerialNumber,
szOutletSampleRecievedBy = orderdet.MySampleCollections.szOutletSampleRecievedBy,
szRetentionLabelNumber = orderdet.MySampleCollections.szRetentionLabelNumber,
szRetentionSampleQty = orderdet.MySampleCollections.szRetentionSampleQty,
szRetentionSerialNumber = orderdet.MySampleCollections.szRetentionSerialNumber
};

DataTable mydtTransReslt = Uow.tresult.GetTestResultNo(NewTestResult.iOutletStorageId,
NewTestResult.dLastDeliveryDate, NewTestResult.szMarkingCertificate,
NewTestResult.iFirstResultId, NewTestResult.iSecondResultId,
NewTestResult.szFirstResultRemark, NewTestResult.szSecondResultRemark,
NewTestResult.szSiteVisitNo, NewTestResult.dTransdate, NewTestResult.iOutletId);

if (mydtTransReslt.Rows.Count > 0)
{
testResultId = Convert.ToInt32(mydtTransReslt.Rows[0]["NewPPMSTestResult"]);
}

if (testResultId > 0)
{
long samples = Uow.spleCollections.GetSampleCollectionsNo(testResultId,
newsamplecollectionmodel.szZonalLabelNumber,
newsamplecollectionmodel.szZonalSampleQty,
newsamplecollectionmodel.szZonalSerialNumber,
newsamplecollectionmodel.szCLabLabelNumber,
newsamplecollectionmodel.szCLabSampleQty,
newsamplecollectionmodel.szCLabSerialNumber,
newsamplecollectionmodel.szGSALabelNumber,
newsamplecollectionmodel.szGSASampleQty,
newsamplecollectionmodel.szGSASerialNumber,
newsamplecollectionmodel.szOutletLabelNumber,
newsamplecollectionmodel.szOutletSampleQty,
newsamplecollectionmodel.szOutletSerialNumber,
newsamplecollectionmodel.szOutletSampleRecievedBy,
DateTime.UtcNow,
newsamplecollectionmodel.szRetentionLabelNumber,
newsamplecollectionmodel.szRetentionSampleQty,
newsamplecollectionmodel.szRetentionSerialNumber);
}
else
{
mydtTransReslt.Columns.Add("Exception", typeof (string));
mydtTransReslt.Rows.Add("Sorry!!! Transaction Failed");
}
}
return 1;
}


Is it possible for me to pass both condition A & D without getting any error.
If possible, please how?
Thanks, Somad

Answer

I came to solve my question by putting the following code:

    public int PostTestResultObj(List<TestAndSampleResult> testandsampleresult)
    {
        long testResultId = 0;

        if (testandsampleresult != null)
            foreach (TestAndSampleResult orderdet in testandsampleresult)
            {
                if (orderdet.TestResult != null) //Making sure our results object is passing null
                {
                    mdlTestResult NewTestResult = new mdlTestResult
                    {
                        iOutletStorageId = orderdet.TestResult.iOutletStorageId,
                        dLastDeliveryDate = orderdet.TestResult.dLastDeliveryDate,
                        szMarkingCertificate = orderdet.TestResult.szMarkingCertificate,
                        iFirstResultId = orderdet.TestResult.iFirstResultId,
                        iSecondResultId = orderdet.TestResult.iSecondResultId,
                        szFirstResultRemark = orderdet.TestResult.szFirstResultRemark,
                        szSecondResultRemark = orderdet.TestResult.szSecondResultRemark,
                        szSiteVisitNo = orderdet.TestResult.szSiteVisitNo,
                        dTransdate = orderdet.TestResult.dTransdate,
                        iOutletId = orderdet.TestResult.iOutletId
                    };

                    //Persist the result
                    DataTable mydtTransReslt = Uow.tresult.GetTestResultNo(NewTestResult.iOutletStorageId,
                                                        NewTestResult.dLastDeliveryDate,
                                                        NewTestResult.szMarkingCertificate,
                                                        NewTestResult.iFirstResultId,
                                                        NewTestResult.iSecondResultId,
                                                        NewTestResult.szFirstResultRemark,
                                                        NewTestResult.szSecondResultRemark,
                                                        NewTestResult.szSiteVisitNo,
                                                        NewTestResult.dTransdate,
                                                        NewTestResult.iOutletId);

                    testResultId = 0;
                    if (mydtTransReslt.Rows.Count > 0) //Result inserted successfully
                    {
                        //Get the id of new result
                        testResultId = Convert.ToInt32(mydtTransReslt.Rows[0]["NewPPMSTestResult"]);

                        if (testResultId > 0)//Result insert was successful
                        {
                            if (orderdet.MySampleCollections != null && testResultId > 0) //Samples have been passed from client
                            {
                                foreach (SampleCollection Sample in orderdet.MySampleCollections)
                                {
                                    long samples = Uow.spleCollections.GetSampleCollectionsNo(testResultId,
                                                                        //newsamplecollectionmodel.iPPMSTestResultId,
                                                                        Sample.iSampleTypeId,
                                                                        Sample.szLabelNumber,
                                                                        Sample.iSampleQty,
                                                                        Sample.szSerialNumber,
                                                                        Sample.szRecievedBy);
                                }
                            }
                        }
                        else
                        {
                            mydtTransReslt.Columns.Add("Exception", typeof(string));
                            mydtTransReslt.Rows.Add("Sorry!!! Transaction Failed");
                        }
                    }
                }
            }
        return 1;
    }

It's saving exactly the way I wanted it to be... Thanks

Comments