Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update XML file using a StringGrid and ADODataSet

Status
Not open for further replies.

Bud1960

Programmer
Jul 8, 2004
8
US
I have a project I am working on that requires me to periodically update an XML file. My code follows:

Code:
// asOrigDir is set on program startup to current dir
// Load the data set
ADODataSet1->LoadFromFile(asOrigDir + "\\TEMP.XML");

// Set the headings
StringGrid1->Cells[0][0] = "SSN";
StringGrid1->Cells[1][0] = "Name";
StringGrid1->Cells[2][0] = "Weeks";
StringGrid1->RowCount = ADODataSet1->RecordCount + 1;

// Load the StringGrid
for(int i = 1; i <= ADODataSet1->RecordCount; i++)
{   
       StringGrid1->Cells[0][i] = (WideString)ADODataSet1->Fields->FieldByName("SSN")->Text;
       StringGrid1->Cells[1][i] = (WideString)ADODataSet1->Fields->FieldByName("Name")->Text;
       StringGrid1->Cells[2][i] = (WideString)ADODataSet1->Fields->FieldByName("Weeks Worked")->Text;
       ADODataSet1->Next();                                                  
}                                                  

// Life is good to here... 
// Now, user edits the Weeks Worked field in the StringGrid

// ...

// ...

// user clicks the Save button...
for(int i = 1; i <= ADODataSet1->RecordCount; i++)
{
       AnsiString tmp = StringGrid1->Cells[2][i];
       ADODataSet1->Edit();
       ADODataSet1->Fields->FieldByName("Weeks Worked")->AsString = tmp;
       ADODataSet1->UpdateRecord();
       ADODataSet1->Post();
}

// Save back to XML file... no changes made!
ADODataSet1->SaveToFile(asOrigDir + "\\TEMP.XML", pfXML);

Have a great day!
Bud
 
This is just a quess so take it for what it's worth. According to my notes, the file TEMP.XML is opened and then remains open as long as the dataset is open. This means that TEMP.XML can be read but not written to until ADODataSet1 is closed.

Try changing the write statement to use another file name and see what happens. If you can write to another file then you have to close the dataset and reopen it to write to the file.





James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
No go, it still writes out the original amounts.

Have a great day!
Bud
 
Silly question but are you sure the field Weeks Worked is being updated?



James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
I have asked sillier... lol

I took the XML file down to just 1 data record and just before the SaveToFile call, it shows the value as the value that I entered in the StringGrid, but doesn't get written to the file... I am about down to raw meat on top of my head on this one...

Code:
	ShowMessage(ADODataSet1->Fields->FieldByName("Weeks Worked")->AsString);
	ADODataSet1->SaveToFile(asOrigDir + "\\TEMP.tmp.XML", pfXML);

Have a great day!
Bud
 
A head scratcher for certain. I'll look around this weekend and see what I can see.



James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
One thing I did notice is that I had to remove the call to Post() for the last code that I posted to work. For some reason the value for Weeks Worked went back to the original value after the Post.

Have a great day!
Bud
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top