Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have form A that I enter in a number in a search box and click a button. If the number is found in the load table, I open form B and populate with the data from the load table. This works fine. When the number in the search box is found in the history table, I want to open form B and populate with the data from the history table.

I have the code working to populate form B when the data is found in the load table. What I trying to figure out is how to use the same code on form B to populate the form when the data is found in the history table data.

What I have tried:

The below code will populate form B when data is found in the load table.
C#
public void DisplayRecord(string loadNumber)
{
    var loadresult = GlobalConfig.Load_MainTable.GetByLoadNumber(loadNumber);

    if (loadresult != null)
        {
            if (loadresult.InUseBy == "0") // Record not in use
            {
                // Hold the record for current user
                GlobalConfig.Load_MainTable.TakeOwnership(loadresult.LoadMainId, UserId);

                // Store the LoadMainId of the current record
                CurrentRecordId = loadresult.LoadMainId;

                // Populate form
                _firstPickUpAccountCode = loadresult.FirstPickUpAccountCode;
                _lastDeliveryAccountCode = loadresult.LastDeliveryAccountCode;
                loadNumberValue.Text = loadresult.LoadNumber;
Posted
Updated 1-Oct-20 6:10am
v2
Comments
BillWoodruff 30-Sep-20 22:44pm    
Describe the structure of the history and load tables. What controls are on FormB ?
Sandeep Mewara 1-Oct-20 2:55am    
What is the difference or why are you not able to apply the same logic to another table?
Oshtri Deka 1-Oct-20 4:40am    
Write entire method.
What is the structure of both tables?
How do you fetch data from db?
MIDCOPC 1-Oct-20 7:19am    
Tables are the same. One is current data other is history data.
I am trying not to duplicate the code if possible
Form A:
using SpeadIMLibrary;
using SpeadIMLibrary.DataAccess.Models;
using SpeadIMUI.FormTools;
using SpeadIMUI.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace SpeadIMUI.Forms
{
public partial class AccessorialsMenuForm : Form
{
// Stores the instance of the parent form (LoginForm)
private Form _parent;

// Stores the userID of the currently loggid in user
private readonly string _userId;

public AccessorialsMenuForm(Form parent, string userId)
{
InitializeComponent();

_parent = parent;
_userId = userId;
}

private void accessorialsMenuGOButton_Click(object sender, EventArgs e)
{
if (createModifyReviewAccessorialsRadioButton.Checked)
{
var mainresult = GlobalConfig.Load_MainTable.GetByLoadNumber(loadSearchBoxValue.Text);

//TODO fix locking form

if (mainresult != null)//load is in load_mail
{
if (createModifyReviewAccessorialsRadioButton.Checked && (!string.IsNullOrWhiteSpace(loadSearchBoxValue.Text)))
{
// Pass LoadNumber to LoadProcessingForm
LoadProcessingForm frm = new LoadProcessingForm(this, _userId, loadSearchBoxValue.Text);
if (frm.IsDisposed == false)
{
frm.Show();
}
loadSearchBoxValue.Text = "";
createModifyReviewAccessorialsRadioButton.Checked = true;
return;
}
}
else //test to see if in history
{
var historyresult = GlobalConfig.History_Load_MainTable.GetByLoadNumber(loadSearchBoxValue.Text);

if (historyresult != null) //load is in history_load_main
{
// Pass LoadNumber to LoadProcessingForm
LoadProcessingForm frm = new LoadProcessingForm(this, _userId, loadSearchBoxValue.Text);
if (frm.IsDisposed == false)
{
frm.Show();
}
loadSearchBoxValue.Text = "";
createModifyReviewAccessorialsRadioButton.Checked = true;
return;
}
}
//TODO: Use textbox to locate load then pass info to form
//LoadFinancialSummaryForm frm = new LoadFinancialSummaryForm(this, _userId, add additional parameters here );
//if (_parent is SpeadIMStartForm mainForm)
//{
// mainForm.openForms.Add(frm);
//}
//frm.Show();
createModifyReviewAccessorialsRadioButton.Checked = false;
return;
}
else if (accessorialsReportsRadioButton.Checked)
{
// TODO: Change ApplyPaymentForm to a reports menu form
//ApplyPaymentForm frm = new ApplyPaymentForm(this, _userId);
//openForms.Add(frm);
//frm.Show();
accessorialsReportsRadioButton.Checked = false;
return;
}
else
{
MessageBox.Show($"A Radio Button must be selected.");
return;
}
}
}
}

Form B: just code to populate form:
public void DisplayRecord(string loadNumber)
{
var loadresult = GlobalConfig.Load_MainTable.GetByLoadNumber(loadNumber);

if (loadresult != null)
MIDCOPC 1-Oct-20 7:20am    
Form B:
public void DisplayRecord(string loadNumber)
{
var loadresult = GlobalConfig.Load_MainTable.GetByLoadNumber(loadNumber);

if (loadresult != null)
{
if (loadresult.InUseBy == "0") // Record not in use
{
// Hold the record for current user
GlobalConfig.Load_MainTable.TakeOwnership(loadresult.LoadMainId, UserId);

// Store the LoadMainId of the current record
CurrentRecordId = loadresult.LoadMainId;

// Populate form
_firstPickUpAccountCode = loadresult.FirstPickUpAccountCode;
_lastDeliveryAccountCode = loadresult.LastDeliveryAccountCode;
loadNumberValue.Text = loadresult.LoadNumber;
shipmentNumberValue.Text = loadresult.ShipmentNumber;
loadStatusValue.Text = loadresult.LoadStatus;
shipperAccountCodeValue.Text = loadresult.ShipperAccountCode;
shipperName1Value.Text = loadresult.ShipperName1;
_shipperName2Value = loadresult.ShipperName2;
_shipperPhoneValue = loadresult.ShipperPhone;
_shipperAddressValue = loadresult.ShipperAddress;
_shipperCityValue = loadresult.ShipperCity;
_shipperStateValue = loadresult.ShipperState;
_shipperZipCodeValue = loadresult.ShipperZipCode;
shipperEmailsValue.Text = loadresult.ShipperEmails;
{
if (loadresult.Send214)
send214Checkbox.Checked = true;
}
bCOValue.Text = loadresult.BCO;
// Get lists based on BCO
LoadAssignmentLists();
LoadOtherSRCList();
// Continue populating
billToAccountCodeValue.Text = loadresult.BillToAccountCode;
billtoName1Value.Text = loadresult.BillToName1;
_billToName2Value = loadresult.BillToName2;
_billToPhoneValue = loadresult.BillToPhone;
_billToAddressValue = loadresult.BillToAddress;
_billToCityValue = loadresult.BillToCity;
_billToStateValue = loadresult.BillToState;
_billToZipCodeValue = loadresult.BillToZipCode;
{
if (loadresult.Send210)
_billToSend210 = 1;
}
totalWeightValue.Text = loadresult.TotalWeight;
totalVolumeValue.Text = loadresult.TotalVolume;
totalQuantityValue.Text = loadresult.TotalQuantity;
totalPickUpsValue.Text = loadresult.TotalPickUps;
totalDeliveriesValue.Text = loadresult.TotalDeliveries;
_stops = Int32.Parse(totalDeliveriesValue.Text) + Int32.Parse(totalPickUpsValue.Text);
totalMilesValue.Text = loadresult.TotalMiles;
trailerValue.Text = loadresult.Trailer;
trailerNumberValue.Text = loadresult.Trailer;
sealNumberValue.Text = loadresult.SealNumber;
// Change format of DTP based on null/MinDate or not
if (loadresult.TrailerDroppedDate.Equals(DateTime.MinValue))
{
trailerDroppedDateValue.CustomFormat = " ";
}
else
{
trailerDroppedDateValue.Cu

1 solution

Where you have "loadResult", you just have a "result"; representing either "load" or "history" (#3)

Then you have 2 public methods: one that takes a "load number" (#1) and another that takes a "history number" (#2).

You call #1 or #2 depending on the context.

In #1 you retrieve a "result set" for "load" as pass it to #3.

In #2, you do the same for history.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900