Delphi 7 object undefinedat

351 views Asked by At

I have a web service that I have created using Delphi and I want to connect to sql server with it so I have added to the project an ADO Connection and ADOQuery had both of them configured and ready to use, there was only a small problem, there are two units on my project and those objects were added to Unit1 and I am working with my ImplUnit whitch is another unit, and can`t find a way to reference or include one unit inside the other unit.

unit1

  { SOAP WebModule} 
unit Unit1;

interface

uses
  SysUtils, Classes, HTTPApp, InvokeRegistry, WSDLIntf, TypInfo,
  WebServExp, WSDLBind, XMLSchema, WSDLPub, SOAPPasInv, SOAPHTTPPasInv,
  SOAPHTTPDisp, WebBrokerSOAP, DB, ADODB;

type
  TWebModule1 = class(TWebModule)
    HTTPSoapDispatcher1: THTTPSoapDispatcher;
    HTTPSoapPascalInvoker1: THTTPSoapPascalInvoker;
    WSDLHTMLPublish1: TWSDLHTMLPublish;
    ADOConnection1: TADOConnection;
    ADODataSet1: TADODataSet;
    ADOQuery1: TADOQuery;
    procedure WebModule1DefaultHandlerAction(Sender: TObject;
      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  WebModule1: TWebModule1;

implementation

{$R *.dfm}

procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  WSDLHTMLPublish1.ServiceInfo(Sender, Request, Response, Handled);
end;

end.

My unit

   unit UTImplementacao;

interface

uses
  InvokeRegistry,DB, ADODB;


type
    IInterface = interface(IInvokable)
    ['{EFF30FFA-DA0C-433A-832A-0BA057B55103}']
    function ReceiveUser(username : String; password : String) :
    Boolean; stdcall;
  end;



  TImplementacao = class(TInvokableClass, IInterface)
    public
    function ReceiveUser(username : String; password : String) :
    Boolean; stdcall;
   end;

implementation

{ TImplementacao }

function TImplementacao.ReceiveUser(username, password: String): Boolean;
var
 ADOConnection1: TADOConnection;
 ADOQuery1: TADOQuery;
begin
    try
      ADOConnection1 := TADOConnection.Create(nil);
      ADOConnection1.LoginPrompt := False;
      ADOConnection1.ConnectionString:= 'Provider=SQLOLEDB.1;Integrated Security=SSPI;' +
                                        'Persist Security Info=False;' +
                                        'User ID=Diego;'+
                                        'Catalog=OnlineShopping;' +
                                        'Data Source=DIEGO-PC\SQLEXPRESS'+
                                        ';Use Procedure for Prepare=1;' +
                                        'Auto Translate=True;Packet Size=4096;'+
                                        'Workstation ID=DIEGO-PC;'+
                                        'Use Encryption for Data=False;'+
                                        'Tag with column collation when possible=False;';
      ADOConnection1.Connected := True;
      ADOQuery1.Connection := ADOConnection1;
      ADOQuery1.SQL.Add('select username,upassword from Users '+
                      'where  username = :usernamep and upassword = '+
                      ':upasswordp');
      ADOQuery1.Parameters.ParamByName('upasswordp').Value := password;
      ADOQuery1.Parameters.ParamByName('usernamep').Value  := username;
      ADOQuery1.ExecSQL;

      Result := True;

finally
  ADOQuery1.Free;
  if ADOConnection1.Connected then
    ADOConnection1.Close;
    ADOConnection1.Free;
  end;


 Result := False;

end;

initialization
  InvRegistry.RegisterInvokableClass(TImplementacao);
  InvRegistry.RegisterInterface(TypeInfo(IInterface));

end.

please disregard the ADOConnection and ADOQuery that I have added to my unit i got a little desperate ad duplicade the code... Yeah, I know yachs!!!!

@SilverWarrior

If declare Unit1 inside the uses of UTImplementacao will I have access to the componemts below:

type
    ADOConnection1: TADOConnection;
        ADODataSet1: TADODataSet;
        ADOQuery1: TADOQuery;

or should I declare for each one of the types variable inside var clause ?

1

There are 1 answers

1
SilverWarior On

If you want to access objects declared in Unit1 from other units in your project you need to add Unit1 into interface uses section (the one at top) of those units.

unit ImplUnit;

interface

uses
  SysUtils, Classes, ... , Unit1;

...

That is the same way as Delphi automatically adds other units like Sysutils, Classes, etc.

Also I would strongly recomend you change the name of your unit to somethng more meaningfull so that when you will be looking at your code after some time you will quickly know what code does that unit contains and what it is used for.

EDIT: Based on your edit of the question I suspect you want to acces the components from your Unit1 directly by calling:

Unit1.AdoConnection1

That won't work. Why? Becouse the components are declared within the scope of the TWebModule1 class.

So you need to access them like this:

Unit1.WebModule1.AdoConnection1;

NOTE: If Unit1 is added into interface uses section of your UTImplementacao unit you can also directly call:

WebModule1.AdoConnection1

You don't have to prefix every command with Unit1. I have written this in such way to be hopefully more understandable which unit mebers are you accessing. Especially for other people which might be reading this thread and not knowing the structure of your program.