All my respect, you dont need to detect raised error.
- Because you always know there is no way to duplicate record, so you might be checked before like below :
Kod: Tümünü seç
function IsUserExist( strUser: String; SQLConnection:TSQLConnection ): boolean;
begin
With TSQLQuery.Create(nil) do
begin
SQLConnection := SQLConnection;
SQL.Clear;
SQL.Add('SELECT Count(*) as UserCount FROM ''users'' WHERE ''username'' = :uname');
ParamByName('uname').AsString := trim(strUser);
Active := True;
Result := FieldByName( 'UserCount' ).AsInteger > 0;
Active := False;
Free;
end;
end;
Usage :
Kod: Tümünü seç
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if IsUserExist( trim(txtUsername.Text), connectionmo.userslq.SQLConnection ) then
begin
MessageDlg( 'User allready Exists.. Please choose another name...', mtWarning, [mbOk], 0 );
Exit;
end;
// ...
// ...
// follows other operations..
- if user exists on table records (above function will return boolean result) then check password to know if the same user want to sign in back. Or new another user wants but not know is there a user name which he/she wants...
EDIT : Result must be greater than 0 for user exists.. I'm sorry to late.
Kod: Tümünü seç
Result := FieldByName( 'UserCount' ).AsInteger > 0;