DataTable NewRow instance problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I've used xsd to generate a dataset from an xml file. In my program I
populate the dataset with data from a database. Well my problem is as follows:

The xsd has added an AddRow method for my schema. The method is as follows:

public BankaRow AddBankaRow(string BankaAdi, int AlacakCekAdeti,
int AlacakIadeliCekAdeti, int VerecekCekAdeti, int VerecekIadeliCekAdeti,
System.Decimal ToplamBorc, System.Decimal ToplamAlacak, System.Decimal
KalanBorc, System.Decimal KalanAlacak, string BankaId, BankalarRow
parentBankalarRowByBankalar_Banka) {
BankaRow rowBankaRow = ((BankaRow)(this.NewRow()));

rowBankaRow.ItemArray = new object[] {
BankaAdi,
AlacakCekAdeti,
AlacakIadeliCekAdeti,
VerecekCekAdeti,
VerecekIadeliCekAdeti,
ToplamBorc,
ToplamAlacak,
KalanBorc,
KalanAlacak,
BankaId,
parentBankalarRowByBankalar_Banka[0]};
this.Rows.Add(rowBankaRow);
return rowBankaRow;
}

In my code I add the rows as follows:
ornekES1.Banka.AddBankaRow(dsBankaSube1.BETSBankalar.BankaAdi,(int)alacaks[0],(int)alacaksIadeli[0],(int)verens[0],(int)verensIadeli[0],tborc,talacak,kborc,kalacak,dsBankaSube1.BETSBankalar.BETSBankaId,ornekES1.Bankalar[0]);

From the db:
dsBankaSube1.BETSBankalar[0].BankaAdi is 1. Banka and
dsBankaSube1.BETSBankalar[1].BankaAdi is 2. Banka

however the dataset output for BankaRow[0].BankaAdi is 2. Banka
and BankaRow[1].BankaAdi is also 2. Banka

to capture the problem in depth I have changed the code to this:

BankaRow temp=null;
for(int i=0;i<dsBankaSube1.BETSBankalar.Count;i++){
if(i==0)
{
//temp is an instace for
BankaRow temp=ornekES1.Banka.AddBankaRow(dsBankaSube1.BETSBankalar.BankaAdi,(int)alacaks[0],(int)alacaksIadeli[0],(int)verens[0],(int)verensIadeli[0],tborc,talacak,kborc,kalacak,dsBankaSube1.BETSBankalar.BETSBankaId,ornekES1.Bankalar[0]);
}
else
{ ornekES1.Banka.AddBankaRow(dsBankaSube1.BETSBankalar.BankaAdi,(int)alacaks[0],(int)alacaksIadeli[0],(int)verens[0],(int)verensIadeli[0],tborc,talacak,kborc,kalacak,dsBankaSube1.BETSBankalar.BETSBankaId,ornekES1.Bankalar[0]);
}
Console.WriteLine(temp.BankaAdi);
}

Well the output is:
1. Banka
2. Banka
2. Banka
2. Banka
....
And goes on with 2. Banka.

I believe the call to dataTable's NewRow method doesn't return a newly
generated row instace. Thus the previous datarow gets overwritten. How
come? Is this a bug in .Net framework (I'm using 1.1). Or if not what I'm
doing wrong?
 
"GatesOfIshtar"
Thus the previous datarow gets overwritten. How
come? Is this a bug in .Net framework (I'm using 1.1). Or if not what I'm
doing wrong?

At least that you are not telling what you want to achieve however give us a
puzzle to find that out.

Cor
 
Well I believe I have written:
"In my program I
populate the dataset with data from a database. Well my problem is as follows:

The xsd has added an AddRow method for my schema. The method is as follows:
....
In my code I add the rows as follows:
....
"
So I guess it is clear that I'm trying to fill an xsd generate dataset
programmatically row by row.? Isn't it?

Though the problem is AddRow method doesn't return an instance if a datarow
so the first added row gets replaces by the second.

As an example:
fBankaRow
temp=ornekES1.Banka.AddBankaRow("Hello",(int)alacaks[0],(int)alacaksIadeli[0],(int)verens[0],(int)verensIadeli[0],tborc,talacak,kborc,kalacak,dsBankaSube1.BETSBankalar.BETSBankaId,ornekES1.Bankalar[0])
ornekES1.Banka.AddBankaRow("Welcome",(int)alacaks[0],(int)alacaksIadeli[0],(int)verens[0],(int)verensIadeli[0],tborc,talacak,kborc,kalacak,dsBankaSube1.BETSBankalar.BETSBankaId,ornekES1.Bankalar[0]);
Console.WriteLine(temp.BankaAdi);

Prints welcome rather than Hello

In my program, replace hello with 1. Banka and welcome with 2. Banka.

So what may be the problem here?

"Cor Ligthert [MVP]":
 
Gates,
Well I believe I have written:
"In my program I
populate the dataset with data from a database. Well my problem is as
follows:

The xsd has added an AddRow method for my schema. The method is as
follows:
...
In my code I add the rows as follows:
...
"
So I guess it is clear that I'm trying to fill an xsd generate dataset
programmatically row by row.? Isn't it?

Yes it is, however you are in my opinion hardly telling what is your problem
with that

The normal procedure to create a row and and add that to a table in a
dataset is

ds.mytable.Rows.Add(ds.mytable.Newrow);

I hope this helps,

Cor
 
Ok, let me explain again.

I've an xml schema that I converted to a sub-class of dataset using the xsd
tool. Now xsd has generated a class called BankaDataTable which is an
subclass of datatable. To this class xsd has added the method:
public BankaRow AddBankaRow(string BankaAdi, int AlacakCekAdeti, int
AlacakIadeliCekAdeti, int VerecekCekAdeti, int VerecekIadeliCekAdeti,
System.Decimal ToplamBorc, System.Decimal ToplamAlacak, System.Decimal
KalanBorc, System.Decimal KalanAlacak, string BankaId, BankalarRow
parentBankalarRowByBankalar_Banka)


which addes a row to the table according to the schema in this way:

{
BankaRow rowBankaRow = ((BankaRow)(this.NewRow()));

rowBankaRow.ItemArray = new object[] {
BankaAdi,
AlacakCekAdeti,
AlacakIadeliCekAdeti,
VerecekCekAdeti,
VerecekIadeliCekAdeti,
ToplamBorc,
ToplamAlacak,
KalanBorc,
KalanAlacak,
BankaId,
parentBankalarRowByBankalar_Banka[0]};
this.Rows.Add(rowBankaRow);
return rowBankaRow;
}


Now the main program adds three rows to the dataset calling the AddBankaRow
method described above. And the program is like this:

for(int i=0;i<dsBankaSube1.BETSBankalar.Count;i++)
{
if(i==0)
{
temp=ornekES1.Banka.AddBankaRow("Merkez
Bankasi",(int)alacaks[0],(int)alacaksIadeli[0],(int)verens[0],(int)verensIadeli[0],tborc0,talacak,kborc,kalacak,dsBankaSube1.BETSBankalar.BETSBankaId,ornekES1.Bankalar[0]);
}
else
ornekES1.Banka.AddBankaRow(dsBankaSube1.BETSBankalar.BankaAdi,(int)alacaks[0],(int)alacaksIadeli[0],(int)verens[0],(int)verensIadeli[0],tborc,talacak,kborc,kalacak,dsBankaSube1.BETSBankalar.BETSBankaId,ornekES1.Bankalar[0]);

Console.WriteLine(temp.BankaAdi);
}

In this program you expect that the WriteLine method shoud print the
BankaAdi collumn of the first row. However, it doesn't. When the second row
is added to the table the first row gets replaced by the second one. Here is
an example
Assume the row data are "1. Banka" 0 0 0 0 ....
"2. Banka" 0 4 5 6 ....
"3. Banka" 0 1 2 3
The expected out should be:
"1. Banka" 0 0 0 0 ....
"1. Banka" 0 0 0 0 ....
"1. Banka" 0 0 0 0 ....
Since temp is referencing to the first row of the table. However the output
is:
"1. Banka" 0 0 0 0 ....
"2. Banka" 0 4 5 6 ....
"2. Banka" 0 4 5 6 ....
So when the second row gets inserted the first row gets replaced. Also when
I attach a datagrid to the dataset the row are:
"2. Banka" 0 4 5 6 ....
"2. Banka" 0 4 5 6 ....
"3. Banka" 0 1 2 3

I've traced the program and everything works corrently till the call to
addrow method use in addBankaRow method. A quickwatch of datatable.Rows[0]
before the call to addRow shows the correct first row (for example it is "1.
Banka" 0 0 0 0 ....) but after the call datatable.Rows[0] and
datatable.Rows[1] shows the newly added row (which in our case is "2. Banka"
0 4 5 6 ....). So the first row gets replaced. My guess is somehow
datatable.NewRow() called from addBankaRow() returns the memory address of
the previous call so that the previous row gets replaced. If thats the case
then this is a bug? If not then what am I doing wrong?

THANKS.
 
Gates,

{
BankaRow rowBankaRow = ((BankaRow)(this.NewRow()));

rowBankaRow.ItemArray = new object[] {
BankaAdi,
AlacakCekAdeti,
AlacakIadeliCekAdeti,
VerecekCekAdeti,
VerecekIadeliCekAdeti,
ToplamBorc,
ToplamAlacak,
KalanBorc,
KalanAlacak,
BankaId,
parentBankalarRowByBankalar_Banka[0]};
this.Rows.Add(rowBankaRow);
return rowBankaRow;
}

if you support the datatable, than all can be done as I showed you while you
cannot add a datarow with a not correct columnrage.
for(int i=0;i<dsBankaSube1.BETSBankalar.Count;i++)
{
if(i==0)
{
temp=ornekES1.Banka.AddBankaRow("Merkez
Bankasi",(int)alacaks[0],(int)alacaksIadeli[0],(int)verens[0],(int)verensIadeli[0],tborc0,talacak,kborc,kalacak,dsBankaSube1.BETSBankalar.BETSBankaId,ornekES1.Bankalar[0]);
}
else
ornekES1.Banka.AddBankaRow(dsBankaSube1.BETSBankalar.BankaAdi,(int)alacaks[0],(int)alacaksIadeli[0],(int)verens[0],(int)verensIadeli[0],tborc,talacak,kborc,kalacak,dsBankaSube1.BETSBankalar.BETSBankaId,ornekES1.Bankalar[0]);

Console.WriteLine(temp.BankaAdi);
}

What do you think that you are adding?

You can only add an existing datarow to a datatable, when it is not already
added to another datatable.

See for this the states of a datarow. "Detached"

http://msdn.microsoft.com/library/d...tml/frlrfsystemdatadatarowstateclasstopic.asp

I hope this helps,

Cor
 
Another explanation; starts the as previous one:

I've an xml schema that I converted to a sub-class of dataset using the xsd
tool. Now xsd has generated a class called BankaDataTable which is an
subclass of datatable. To this class xsd has added the method:
public BankaRow AddBankaRow(string BankaAdi, int AlacakCekAdeti, int
AlacakIadeliCekAdeti, int VerecekCekAdeti, int VerecekIadeliCekAdeti,
System.Decimal ToplamBorc, System.Decimal ToplamAlacak, System.Decimal
KalanBorc, System.Decimal KalanAlacak, string BankaId, BankalarRow
parentBankalarRowByBankalar_Banka)


which addes a row to the table according to the schema in this way:

{
BankaRow rowBankaRow = ((BankaRow)(this.NewRow()));

rowBankaRow.ItemArray = new object[] {
BankaAdi,
AlacakCekAdeti,
AlacakIadeliCekAdeti,
VerecekCekAdeti,
VerecekIadeliCekAdeti,
ToplamBorc,
ToplamAlacak,
KalanBorc,
KalanAlacak,
BankaId,
parentBankalarRowByBankalar_Banka[0]};
this.Rows.Add(rowBankaRow);
return rowBankaRow;
}

The code above is not written by. Its generated by xsd to add a row the
"Banka" table.
The below code is written by me:

for(int i=0;i<dsBankaSube1.BETSBankalar.Count;i++)

ornekES1.Banka.AddBankaRow(dsBankaSube1.BETSBankalar.BankaAdi,0,0,0,0,0,0,0,0,dsBankaSube1.BETSBankalar.BETSBankaId,ornekES1.Bankalar[0]);
}

basically I'm adding data to the table from an array. Notice that there is
only one dataset! So assume the array is:
dsBankaSube1.BETSBankalar[0].BankaAdi="1. Banka";
dsBankaSube1.BETSBankalar[0].BETSBankaId="0001";
dsBankaSube1.BETSBankalar[1].BankaAdi="2. Banka";
dsBankaSube1.BETSBankalar[1].BETSBankaId="0002";
dsBankaSube1.BETSBankalar[2].BankaAdi="3. Banka";
dsBankaSube1.BETSBankalar[2].BETSBankaId="0003";

ornekES1.Bankalar[0] -> this paramter is the column of the row that
specificies the parent table. Again xsd generated.

So when the row are added to the table with the above example the rows
should list (in a datagrid lets say)
"1. Banka" 0....0 "0001"
"2. Banka" 0....0 "0002"
"3. Banka" 0....0 "0003"

However, the rows added to the table are
"2. Banka" 0....0 "0002"
"2. Banka" 0....0 "0002"
"3. Banka" 0....0 "0003"

Strange? Perhaps a bug in .Net? Ok lets dig in more.
When the for counter variable i=0 zero the row with "1. Banka" is added. Thus
ornekES1.Banka[0].BankaAdi = "1. Banka" -> added a watch
Now when the counter variable i=1 row with "2. Banka" is added. Thus
ornekES1.Banka[1].BankaAdi = "2. Banka". However this at the same time causes
ornekES1.Banka[0] to change thus
ornekES1.Banka[0].BankaAdi turns to "2. Banka"

A strange problem?
 
Back
Top