Page 1 of 1

How to prevent ''Access denied for user''

Posted: Thu 09 Oct 2008 11:29
by mierlp
hi,

i use a secure .ini file which contains the settings for connecting to
a database (user, password, database, port and server name (IP nr).

Within the OnCreate event from a datamodule i use this settings to make the connection. When some of the settings are wrong (wrong password)
the user gets the default (i think MYSQL) message "'Access denied
for user ....''.

Is it possible to disable this message and use something like :

if not MyConnection.Connected then ....
message.....
else
......

I did try this but can't get is working....within the datamodule OnCreate event i use the following code to connect :

Code: Select all

procedure TdmTables.DataModuleCreate(Sender: TObject);
var
  IniFile : TIniFile;
  i : integer;
begin
    cMapProg:= ExtractFilePath(Application.ExeName);
    if not fileExists(cMapProg+'database.dat') then begin
       Application.MessageBox('Settings file does not exist !'+#10+''+#10+'The applications will be terminated', 'Warning !', MB_OK+MB_ICONHAND+MB_DEFBUTTON1+MB_APPLMODAL);
       Application.Terminate;
    end;
    if fileExists(cMapProg+'database.dat') then begin
       // Call .ini file to read settings
       GetSetting;
       // variabelen toekennen
       dmTables.MyConnection.Server:=cRemoteserver;
       dmTables.MyConnection.Database:=cRemoteDB;
       dmTables.MyConnection.Username:=cRemoteUser;
       dmTables.MyConnection.Password:=cRemotePassword;
       dmTables.MyConnection.Port:=strTOint(cRemotePort);

       dmTables.MyConnection.Connected:=true;
       if not dmTables.MyConnection.Connected then begin
          MessageDlg('Warning' + #10 + '' + #10 + 'It's not possible to maken connection with database.' + #10 + 'Application will be terminated !', mtError, [mbOK], 0);
          Application.Terminate;
       end;
       if dmTables.MyConnection.Connected then begin
          for i := 0 to dmTables.ComponentCount -1 do begin
             if Components[i] is TMyQuery then begin
               (Components[i] as TMyQuery).Active := true;
             end;
          end;
       end;
   end;
end;

Posted: Thu 09 Oct 2008 12:00
by cybsistemas
in the event onError de myconnection
procedure MyconnectionError(Sender: TObject; E: EDAError; var Fail: Boolean);
begin


If e.IsFatalError then
Begin
ShowMessage ('xxxxxxxxxxxxxxxx' + IntToStr(e.ErrorCode) );
Application.Terminate;
End

Else If e.IsKeyViolation then
Begin
ShowMessage ('Duplicate key'+IntToStr(e.ErrorCode));
End

Else
ShowMessage (trim (e.Message));
Fail := False;
End;

Posted: Thu 09 Oct 2008 13:20
by mierlp
Hi

thanks for the reply...i did the following based on your reply.
The error code which exist if ...lets say a wrong password is used
is the following 28000 Access denied for

Internet manual says:
Error: 1045 SQLSTATE: 28000 (ER_ACCESS_DENIED_ERROR)
Message: Access denied for user '%s'@'%s' (using password: %s)

Now i use the following code below BUT this does not prevent
showing many times the mysql error message

What's a good solution for creating a connection using MyConnection
in combination with the BeforeConnect or DataModule OnCreateEVent

Code: Select all

procedure TdmTables.MyConnectionError(Sender: TObject; E: EDAError;
  var Fail: Boolean);
begin
  If (E is EMyError) then begin
   if (EMyError(E).ErrorCode=ER_ACCESS_DENIED_ERROR) then begin
      MessageDlg('Waarschuwing' + #10 + '' + #10 + 'Er kan GEEN verbinding worden gemaakt met de database.' + #10 + 'Controleer de instellingen van de applicatie middels SETDB.EXE' + #10 + 'en of de gedefinieerde gebruiker voldoende rechten heeft.' + #10 + '' + #10 + 'Applicatie wordt nu automatische beƫindigd  !', mtError, [mbOK], 0);
      Application.Terminate;
   end;
  end;
end;

Posted: Thu 09 Oct 2008 13:35
by cybsistemas
procedure TdmTables.MyConnectionError(Sender: TObject; E: EDAError;
var Fail: Boolean);
begin

If (E is EMyError) then begin
if (EMyError(E).ErrorCode=ER_ACCESS_DENIED_ERROR) then begin
MessageDlg('Waarschuwing' + #10 + '' + #10 + 'Er kan GEEN verbinding worden gemaakt met de database.' + #10 + 'Controleer de instellingen van de applicatie middels SETDB.EXE' + #10 + 'en of de gedefinieerde gebruiker voldoende rechten heeft.' + #10 + '' + #10 + 'Applicatie wordt nu automatische beƫindigd !', mtError, [mbOK], 0);
Fail := False;
Application.Terminate;
end;
end;
end;