Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > DB-Aware

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 05-20-2008, 12:34 AM
pkakkar pkakkar is offline
Member
 
Join Date: Apr 2008
Posts: 44
Default problem in displaying entered record in DBGrid

Hello,

I have a form with name, email and group name edit boxes and add contact button. It has one more button 'see database' which take you to another form with DBGrid. This DBGrid is connected to the required table where user is entering records.

The problem is when i click this button it doesnt show me the record i have just entered. I have to close my program and when i execute again then it shows the record in DBGrid.

Can someone advise how to see the record which user has just entered..how to update DBGrid instantly.

Regards
Pooja
Reply With Quote
  #2  
Old 05-20-2008, 12:41 AM
BitFarmer BitFarmer is offline
Senior Member
 
Join Date: Feb 2008
Posts: 160
Default RE: problem in displaying entered record in DBGrid

It looks like a typical transaction problem (or feature!)

Your from is displaying data, and adding record to the table, in one transaction, once the transaction is commited, the record can be viewed from other transactions, like the one in you grid.

If you are using transactional database (FireBird, MS SQL, Oracle...) then you HAVE to understand transactions and use it properly in order to have the vehaviour you want in your app.

You don't say with type of database are you using, so may be it is not the cause... please give more details.
Reply With Quote
  #3  
Old 05-20-2008, 12:52 AM
pkakkar pkakkar is offline
Member
 
Join Date: Apr 2008
Posts: 44
Default RE: problem in displaying entered record in DBGrid

This is the code when i click on my 'add'button. it adds the records in the database. But recently I have added one button also on the form that opens another form displaying the records in the DBGrid but it doesnt show the record i have entered just now. It shows when i execute my program again.

procedure TForm1.BitBtn1Click(Sender: TObject);
var

i:integer;
b: boolean;
begin
//verifying if the contact form is not empty and error message is shown
if (edit1.Text='') or (edit2.Text='') then begin
showMessage('Please enter a name and e-mail address');
exit;
end;

//verifying if email address doesnt have '@'
if pos('@', edit2.Text) =0 then begin
showMessage('Invalid email address');
exit;
end;



if RadioButton1.Checked then begin
if ComboBox1.Text <> '' then begin
//insert data into database
Form2.MSQuery1.Close;
Form2.MSQuery1.SQL.Text:='INSERT INTO contacts values(:name,:mail,:gname)';
Form2.MSQuery1.ParamByName('name').Value:=Edit1.Te xt;
Form2.MSQuery1.ParamByName('mail').Value:=Edit2.Te xt;
Form2.MSQuery1.ParamByName('gname').Value:=ComboBo x1.Text;
Form2.MSQuery1.Execute;
showMessage('New contact is added.');
Edit1.Clear;
Edit2.Clear;
ComboBox1.Clear;
exit;
end
else
begin
//user didnt select a name
showMessage('Please select a group name to add to contact.');
exit;
end;
end;


if RadioButton2.Checked then
begin
//check if name is entered
if Edit3.Text='' then begin
showMessage('Please enter a group name');
end
else
begin
//check if there is no duplicate entry of groupname
b := false;
for i:=0 to Form2.MSQuery2.RecordCount-1 do
begin
if AnsiUpperCase(Edit3.Text) = AnsiUpperCase(Form2.MSQuery2.FieldByName('GroupNam e').AsString) then
begin
b := true;
end;
Form2.MSQuery2.Next;
end;

if b then begin
showMessage('Group already exists.');
end

else
begin
//add the new groupname to groups table
//Form2.MSQuery2.Close;
Form2.MSQuery2.SQL.Text:= 'Insert into groups values(:gname)';
Form2.MSQuery2.ParamByName('gname').Value:=Edit3.T ext;
Form2.MSQuery2.Execute;
// also add the details to contacts table
//Form2.MSQuery1.Close;
Form2.MSQuery1.SQL.Text:='Insert into contacts values(:name, :mail, :gname)';
Form2.MSQuery1.ParamByName('name').Value:=Edit1.Te xt;
Form2.MSQuery1.ParamByName('mail').Value:=Edit2.Te xt;
Form2.MSQuery1.ParamByName('gname').Value:=Edit3.T ext;
Form2.MSQuery1.Execute;
showMessage('the new group: '+edit3.Text+ ' and contact details are added.');
Edit1.Clear;
Edit2.Clear;
Edit3.Clear;
exit;
end;
end;

end;
if not (RadioButton1.Checked) and not (RadioButton2.Checked) then
showMessage('Please select the options through RadioButtons to add contact');
end;
Reply With Quote
  #4  
Old 05-20-2008, 01:52 AM
BitFarmer BitFarmer is offline
Senior Member
 
Join Date: Feb 2008
Posts: 160
Default RE: problem in displaying entered record in DBGrid

I repeat you the same thing: You are NOT taking into account the transations,and you are using ms sql server, it is a transactional database, so only after you "commit" the changes, they are visible to other transactions.

You MUST understand what kind of transactions you have and how to use it, or a lot of similar problems will arise! Have a google on this and read about it.

In your case, after:

Form2.MSQuery1.Execute;

Yo must force a commit. I don't use ms sql server, but it can't be very different.

I would add after that line this code:

Form2.MSQuery1.Transaction.Commit;

The idea behind is that every query has a transaction attached, and this transaction keep the changes you post until a commit -or a roll back if you decide to uindo the changes- is issued. You can force it by code, or let the app. do it for you when you CLOSE your app.

This default vehaviuor is VERY dangerous: If you work 4 hours in your app, and the app hangs (or the lan fails, or the server is stoped, etc) all changes are NOT commited to the database and get lost forever! Funny, isn't it?

When we moved from dbf files to FireBird, first thing was to understand this point, as this is what makes sql databases good or bad: The use you make of transactions!
Reply With Quote
  #5  
Old 05-20-2008, 01:54 AM
BitFarmer BitFarmer is offline
Senior Member
 
Join Date: Feb 2008
Posts: 160
Default RE: problem in displaying entered record in DBGrid

You can start reading here: http://www.databasejournal.com/features/mssql/article.php/3524891
Reply With Quote
  #6  
Old 05-20-2008, 04:34 AM
davidj davidj is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,900
Default RE: problem in displaying entered record in DBGrid

What database engine are you using?

Have you tried YourTable.Refresh?
Are you commiting the changes?
Have you tried YourTable.Close; then YourTable.Open;

It depends largely on what database engine you are using.
Like above you may need to commit the changes.
Reply With Quote
  #7  
Old 05-20-2008, 12:22 PM
nikolayj nikolayj is offline
Senior Member
 
Join Date: May 2002
Posts: 335
Default RE: problem in displaying entered record in DBGrid

Hi there

As far I understood you use 2 datasets. One to update and another one to diplay records in the DBGrid. So after you add or update date in the first dataset you have to refresh data in the second dataset
Reply With Quote
  #8  
Old 05-20-2008, 11:33 PM
BitFarmer BitFarmer is offline
Senior Member
 
Join Date: Feb 2008
Posts: 160
Default RE: problem in displaying entered record in DBGrid

2 dataset means you need to refresh the second after the first add a record, that's true, but also, as it is using sql server (a transactional client/server database engine), each dataset is attached to it's transaction, so the first dataset, after adding the record, has to commit the changes, then, the second one can close/open in order to see the new record.

This is the full proccess when not using local databases.
Reply With Quote
  #9  
Old 05-29-2008, 09:29 PM
pkakkar pkakkar is offline
Member
 
Join Date: Apr 2008
Posts: 44
Default RE: problem in displaying entered record in DBGrid

Thanks for all this.

Regards
Pooja
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:28 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.