Skip to Content
author's profile photo Former Member
Former Member

Adding multiple Data to UserTable by function

I created a Usertable with the fields Descript, Locked, and TEST.(Code and Name are default)

I created a function to add values to the usertable:

public string addData(string code, string name, System.Collections.Hashtable hTable)

{

oCompany.StartTransaction();

System.Console.WriteLine("Add Record to Table("tableName")");

SAPbobsCOM.UserTable userTable = oCompany.UserTables.Item(tableName);

if(userTable.GetByKey(code) == false)

{

userTable.Code = code;

userTable.Name = name;

foreach(System.Collections.DictionaryEntry dEntry in hTable)

{

try

{

userTable.UserFields.Fields.Item("U_"+dEntry.Key).Value = dEntry.Value;

}

catch(System.Exception ex)

{

System.Console.WriteLine("Exception: "+ex.ToString());

throw ex;

}

}

/*foreach(string key in hTable.Keys)

{

userTable.UserFields.Fields.Item("U_"+key).Value = hTable[key];

}*/

if(userTable.Add() != 0) throw new COR_DBException("Data in Table "tableName" not added: "+oCompany.GetLastErrorDescription(), oCompany.GetLastErrorCode());

System.Console.WriteLine("Record added to Table("tableName")");

int w = userTable.Update();

System.Console.WriteLine("update: "+w);

}

else

{

throw new COR_DBException("Record already exists!");

}

oCompany.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_Commit);

userTable = null;

System.GC.Collect();

return oCompany.GetNewObjectKey();

}

My function call looks like that:

try

{

COR_ITable utStatus= new COR_UserTable("COR_PRJS");

Hashtable hTable = new Hashtable();

hTable.Add("Descript", "The Project ...");

hTable.Add("Locked", "Y");

hTable.Add("TEST", 12635);

utStatus.addData("10", "name0", hTable);

hTable.Clear();

hTable.Add("Descript", "The Project ...");

hTable.Add("Locked", "Y");

hTable.Add("TEST", 12235);

utStatus.addData("11", "name1", hTable);

}

catch(COR_Exception ex)

{

MessageBox.Show(ex.ToString());

System.Console.WriteLine(ex.ToString());

}

}

The Problem now is follow:

After the function call i find follow datas in database:

Code Name Description Locked TEST

10 name0 The Project... Y 12635

11 name1 The Project... Y

The second value by de Test-Field is missing(???)

When I add a new userField TEST3 at the end of the table and try the same function call it looks like this:

Code Name Description Locked TEST TEST3

10 name0 The Project... Y 12635

11 name1 The Project... Y 12235

I see the second value in the column TEST now. That can not be a solution?

But, this is not all. When i do now a second call with other values, i have the follow result:

Code Name Description Locked TEST TEST3

10 name0 The Project... Y 12635

11 name1 The Project... Y 12235

12 name2

13 name3

Now only Code and Name are filled in the database.

The Hashtable in the function addData is ok. In Debug-Mode I see all right values, but they are not filled in the database by the SAP function.

Can anybody help me?

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • author's profile photo Former Member
    Former Member
    Posted on Dec 08, 2005 at 02:38 PM

    I "solved" the problem! The problem is the function userTable.GetByKey(code). I tested with this function if the record already exists. When I remove this call all works fine.

    public string addData(string code, string name, System.Collections.Hashtable hTable)
    	{	
    		System.Console.WriteLine("Add Record to Table("+tableName+")");
    		SAPbobsCOM.UserTable userTable = oCompany.UserTables.Item(tableName);
    		//if(userTable.GetByKey(code) == false)
    		//{
    			userTable.Code = code;
    			userTable.Name = name;
    			foreach(System.Collections.DictionaryEntry dEntry in hTable) 
    			{
    					userTable.UserFields.Fields.Item("U_"+dEntry.Key).Value = dEntry.Value;
    			}
    
    			if(userTable.Add() != 0) throw new COR_DBException("Data in Table "+tableName+" not added: "+oCompany.GetLastErrorDescription(), oCompany.GetLastErrorCode());
    			System.Console.WriteLine("Record added to Table("+tableName+")");
    		//}
    		//else throw new COR_DBException("Record already exists!");
    		return oCompany.GetNewObjectKey();
    	}
    

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.