Calling an Adapter with multiple keys?

On a form, is it possible to call an Adapter with multiple keys? I’m working in a form customization and trying to call the ShipTo table to pull the specific ShipTo information (Name/Addr/City/etc) to display on screen. Essentially, on the form the user selects the customer and shipto id, then I need to take that to pull the other fields.

I declared vars for the key fields: Company, ShipToCustNum, and ShipToNum but the method never connects. I have a testing message box in the try that never fires so it’s failing.

System.Data.DataSet dsShipToNum = adapterShipTo.GetData(Company, ShipToCustNum, ShipToNum);
DataRowCollection dsShipTo = dsShipToNum.Tables["ShipTo"].Rows;

What’s your code look like prior to your example

Here is the method I have, I first pull the needed data from the OrderHed table in another method and set it to Vars: SOCustID, SOCompany, and SOShipTo referenced below.

private void GetShipToInfo()  // Get ShipTo info 
{
    try
    {
			MessageBox.Show("Get ShipTo info method start");  //TEST

        // Declare and Initialize EpiDataView Variables
        EpiDataView edvRMAHead = ((EpiDataView)(this.oTrans.EpiDataViews["RMAHead"]));

        // Check if valid EpiDataView Row(s) are selected
        if ((edvRMAHead.Row < 0))
        {
            return;
        }
        // Declare and create an instance of the Adapter
        ShipToAdapter adapterShipTo = new ShipToAdapter(this.oTrans);
        adapterShipTo.BOConnect();

        // Declare and Initialize Variables
        int RMACust = SOCustID;  		// CustNum to link RMAHead to ShipTo 
        string RMACompany = SOCompany;   //CustNum to link RMAHead to ShipTo 
        string RMAShipToNum = SOShipTo;  // CustNum to link RMAHead to ShipTo 

        // Call Adapter method
        System.Data.DataSet dsShipToNum = adapterShipTo.GetByID(RMACompany, RMACustID, RMAShipToNum);
        DataRowCollection dsShipTo = dsShipToNum.Tables["ShipTo"].Rows;

        foreach (DataRow STrow in dsShipTo)
        {
			   EpiDataView RMAHead = (EpiDataView)oTrans.EpiDataViews["RMAHead"];
		   RMAHead.dataView[RMAHead.Row].BeginEdit();
 			   RMAHead.dataView[RMAHead.Row]["PkUpName_c"] = STrow["Name"].ToString();

 			   RMAHead.dataView[RMAHead.Row].EndEdit();  

//TEST
                MessageBox.Show("ShipTo info " );   
//TESTING

            }
            // Cleanup Adapter Reference
            adapterShipTo.Dispose();
        }
        catch (System.Exception ex)
        {
            ExceptionBox.Show(ex);
        }
    }

As a test, can you remove the try/Catch block and hard code in some values for the variables?
With a try/catch and your return out if the row is less than 0, i’ve seen it before execute the return prior to rendering the msg box. I want to see where it’s failing and the try/catch will hide this

Same situation, the first message box at the top of the method fired but not the one in the ForEach loop, so it’s not connecting to the ShipTo table correctly.

Are you passing the right info? I think that method accepts Company, CustNum, and ShipToCustNum

Also, I’m thinking maybe call the GetByID (which returns a bool) and then reference the data in a separate call.
Perhaps…

1 Like

The Company, ShipToNum, and CustNum is correct so I’m sure my issue is the code syntax. I’ll see if I can figure out GetByID call to ShipTo

something like

ShipToAdapter adapterShipTo = new ShipToAdapter(oTrans);
adapterShipTo.BOConnect();
bool result = adapterShipTo.GetByID(company, custNum, shipToCustNum);
if(result)
{
   foreach(var row in adapterShipTo.ShipToData.ShipTo.Rows)
   {
   }
}
1 Like

Still something with the syntax it seems. This brings me to a question: Where can I find information on the methods, ie: GetByID, and Adapters?

Object Explorer will show you all the methods and properties of an adapter, although sometimes using the Business Logic Testers is a little easier to visualize.

So it looks like we don’t need to pass company, just custNum and shipToNum

2 Likes

Awesome, lucky we don’t have multiple companies anyway. :smiley: I found it pretty easy in the Object Browser in VS, once I added in the ShipTo DLLs

1 Like

So close! It’s got to be with the EpidataView declaration. It passes the F5 code test but I get an error when I run the form.

private void GetShipToInfo()  // Get ShipTo info on OrderHed
{ 
		MessageBox.Show("START GetShipTo info method start SOCustID/ShipToNum: "+SOCustID+"/"+SOShipTo);  // This Displayed 

		ShipToAdapter adapterShipTo = new ShipToAdapter(oTrans);
		adapterShipTo.BOConnect();
		bool result = adapterShipTo.GetByID(SOCustID, SOShipTo); 	//Erp.Tablesets.ShipToTableset GetByID(int custNum, string shipToNum)
		if(result)
		{
		   foreach(var row in adapterShipTo.ShipToData.ShipTo.Rows)
		   {
			EpiDataView edvShipTo = (EpiDataView)oTrans.EpiDataViews[row];
	        System.Data.DataSet dsShipToNum = adapterShipTo.GetData(edvShipTo);
        DataRowCollection dsShipTo = dsShipToNum.Tables["ShipTo"].Rows;

	            foreach (DataRow strow in dsShipTo)
	            {
					string STName = strow["Name"].ToString();
					MessageBox.Show("ShipTo ForEach fired:"+STName); 
} 
   } 
  }
}

Your code inside your first foreach loop is giving me hives.
What are you trying to do?

I’m trying to get the ShipTo information; Name, Address, City, State, Zip to put it into UD fields on the RMAHead table. For example: ShipTo.Name to RMAHead.STName_c

		ShipToAdapter adapterShipTo = new ShipToAdapter(oTrans);
		adapterShipTo.BOConnect();
		bool result = adapterShipTo.GetByID(SOCustID, SOShipTo); 	//Erp.Tablesets.ShipToTableset GetByID(int custNum, string shipToNum)
		if(result)
		{
		   foreach(var row in adapterShipTo.ShipToData.ShipTo.Rows)
		   {

To here everything is golden, then I tried to pull data from ShipTo. For example when I tried, string STName = row[“Name”].ToString(); a test compile gives the error: “Cannot apply indexing with [] to an expression of type ‘object’” I’ve seen this before and had to make a EpiDataView. Which is the code giving you hives. :wink:

I’ll keep working on it.

1 Like

Randy,
Use the debug option in VS in your customization and step though. I’m sure that will help pickup your null reference issue.

Cheers
Simon

1 Like

@Randy - I don’t understand the purpose of the “foreach{ foreach{} }”

You’re calling a specific customer/shipto, what are you using the foreach to go through?

Wouldn’t this work?

  ShipToAdapter adapterShipTo = new ShipToAdapter(oTrans);
  adapterShipTo.BOConnect();
  adapterShipTo.GetByID(SOCustID, SOShipTo); 	
   	string STName = adapterShipTo.ShipToData.ShipTo[0].Name.ToString();
         adapterShipTo.Dispose();
3 Likes

@duckor and @Aaron_Moreng are amazing! I owe you both a drink if we ever meet. Once again you guys have rescued this know-just-enough-to-get-into-trouble self. Wish I could give you both a “solution” check.
You too @Hally :wink:

I tried something similar but didn’t have the [0] bracket and it was giving me indexing error inside the foreach. I’ll be sure to update my stash of code snips I use to self-help and a few other resources I try to not overuse you guys here.

parsing the code: adapterShipTo.ShipToData.ShipTo[0].Name.ToString();

adapterShipTo I understand as it’s been declared in the code when accessing the adapter

.ShipToData.ShipTo[0] This one must specify the data to access but what is the [0] for?

.Name.ToString() is the fieldname and converting it to string.

2 Likes

so to answer questions about the code:

the indexer “[]” is telling the code which row of the data to access. In this case, because we performed a GetByID on a specific ShipTo record, we should have 1 row of information in that index. In C#, indexes start at 0, so it says to look at position 0 of the index to identify the record you’re interested in.

5 Likes