Table.rows.find() returns null in a table where the row exists. RRS feed

  • Question

  • Hi...

    I am working in a Project in Visual Studio 2013 where I have to read an Excel report related with a list of business opportunities. Some of the Fields that are involved in it are:  Opportunity ID, BU, Account, close date, Account Manager, Total Value and so on. As soon as I get the conection with the Excel Report, I fill a DataTable and declare the columns [Opportunity ID] and [BU] as my Primary Keys.  In that way, an Opportunity value may have one o more rows with different BU code each of them.  In consequence, you can define one row in the table if you specify the [Opportunity ID] and the [BU] values.

    My problem starts when I try to get information of those rows that have an specific value of [Opportunity ID]. For that situation I make a "foreach" structure where I check the BU codes that are involved with that Opportunity. So I use the Find() Method to get the information of the row specified by the Opportunity ID and BU.  The first time it goes to find the information it gets the results successfully but in the next cycle changing just the [BU] using the same [Opportunity ID] value, the find() method returns a "null" and I do not know why because I am sure that the row exists in the table.  I have verified the "Unique" property for the  [Opportunity ID] and [BU] columns (which are my primary keys)  and they are in false which is OK.

    I share the code that I am using...

    Hope someone can help.



    void ObtenTotalesxBUs(string Opportunity, string[] BUs, Object[] TotalBUs, Object[] ExpTotalBUs) { ArrayList TotalesBUs = new ArrayList(); //Required information from the row. ArrayList ExpTotalesBUs = new ArrayList(); //Required information from the row. foreach(string businessUnit in BUs) { Object[] LlaveBusqueda = { Opportunity, businessUnit }; //Provide values to the Primary Keys. DataRow RenglonInfo = null; RenglonInfo = TablaFunnel.Tabla.Rows.Find(LlaveBusqueda); //Find method receives the Primary Keys values. Here is where I have a "null" in return the second time changing just the [BU] field. TotalesBUs.Add(RenglonInfo["Total"]); //Get the required information from the row. ExpTotalesBUs.Add(RenglonInfo["Expected Total Value"]); //Get the required information from the row. } TotalBUs = TotalesBUs.ToArray(); ExpTotalBUs = ExpTotalesBUs.ToArray();


    • Moved by Fred Bao Wednesday, February 18, 2015 7:06 AM Ado.NET DataSet Related
    Monday, February 9, 2015 4:44 PM

All replies

  • Are you absolutely sure that the second item of BUs is valid and exists in the table together with Opportunity in the same row? How did you checked this? 

    Also note that TotalBUs and ExpTotalBUs will not be returned from the function since the parameters do not contain “out”.

    Monday, February 9, 2015 7:25 PM
  • Hi Viorel..

    Thanks a lot for you help.

    I am absolutely sure that the second ítem of BU is valid and exist in the table because I tried to make a "Select" statement in the table providing the values of [Opportunity ID] and [BU] and I get the expected record. So, why is not working using Find() with the defined Primary Keys??? 

    I tried this in order to test that the record exists...

    if(RenglonInfo == null)
    	string strSelect = "[Opportunity ID] = '0000218256' AND [BU] = 'SFW'";
    	RenglonInfo2 = TablaFunnel.Tabla.Select(strSelect);

    Tuesday, February 10, 2015 4:35 AM
  • So even if you write explicitly

    Object [] LlaveBusqueda = { "0000218256", "SFW" };
    Debug.Assert( TablaFunnel.Tabla.PrimaryKey[0].ColumnName == "Opportunity ID" );
    Debug.Assert( TablaFunnel.Tabla.PrimaryKey[1].ColumnName == "BU" );
    DataRow RenglonInfo = TablaFunnel.Tabla.Rows.Find(LlaveBusqueda);

    then assertions pass, but it returns null?

    • Edited by Viorel_MVP Tuesday, February 10, 2015 6:45 AM
    Tuesday, February 10, 2015 6:42 AM