I have a problem with the Delphi database components. I tried to add some functionality to a program, but was getting strange problems. I broke it down to a simple sample project and the behaviour is still there.
I created a new Forms Application on Delphi 2007, added a TSQLConnection, TSQLTable, TDataSetProvider and TClientDataSet. I inserted the information for a local MS SQL database and the table that is inside, which is a simple test table:
CREATE TABLE dbo.Test1(
[Name] varchar(32) not null primary key,
[Type] varchar(16) not null,
[Selected] BIT not null)
I then added a TEdit, TListBox and 2 Buttons. The function should be: Press Add and a record with the name of Edit1 is entered into the database. Press Update and the listview gets filled with the entries already in the database.
The code:
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
ClientDataSet1.Refresh;
ClientDataSet1.First;
while not ClientDataSet1.Eof do
begin
ListBox1.AddItem(ClientDataSet1Name.AsString,nil);
ClientDataSet1.Next;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ClientDataSet1.Append;
ClientDataSet1Name.AsString := Edit1.Text;
ClientDataSet1Type.AsString := 'A1';
ClientDataSet1Selected.AsBoolean := false;
ClientDataSet1.Post;
ClientDataSet1.ApplyUpdates(-1);
end;
Now the strange thing for me is, that when start the program, add 2 records (checked in management studio that they are really there) and now i click on update, the list stays empty, as the RecordCount of the ClientDataSet is 0 as soon as the Refresh on begin of Button1Click is executed. The entries however are and stay in the database.
The other strange thing is, that as soon as i quit the program, and start it again and try to add another record, i get the error "Cannot create new connection because in manual or distributed transaction mode". As soon as i delete the records from the table and restart the program, I can add again.
Can someone tell me how this strange behaviour happens and how i can fix it?
Thanks in advance.