cancel
Showing results for 
Search instead for 
Did you mean: 

Adding multiple Data to UserTable by function

Former Member
0 Kudos

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?

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

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();
	}