What's wrong with this code? - delphi

In this case I have some code which is working without problem in an existing program, but throws an excecption when I use it in a new program.
It may not be the best code, but it is working in every day use ...
Function DoSQlCommandWithResultSet(const command : String;
AdoConnection : TADOConnection;
resultSet : TStringList): Boolean;
var i : Integer;
AdoQuery : TADOQuery;
begin
Result := True;
resultSet.Clear();
AdoQuery := TADOQuery.Create(nil);
try
AdoQuery.Connection := AdoConnection;
AdoQuery.SQL.Add(command);
AdoQuery.Open();
i := 0;
while not AdoQuery.eof do
begin
resultSet.Add(ADOQuery.Fields[i].Value);
AdoQuery.Next;
Inc(i);
end;
finally
AdoQuery.Free();
end;
end;
Yes, it probably needs a try/catch and the boolean result isn't used, but it works ...
.... in the previous program, but in a new one it thows an exception when called ...
procedure TForm1.FormCreate(Sender: TObject);
var my_stringlist : TStringList;
i : integer;
begin
AdoConnection := TADOConnection.Create(nil);
if ConnectToDefaultDatabase(AdoConnection) = False then
MessageDlg('Agh !', mtError, [mbOK], 0);
my_stringlist := TStringList.Create();
if DoSQlCommandWithResultSet('show databases', AdoConnection, my_stringlist) = False then
MessageDlg('Urk !', mtError, [mbOK], 0);
for i := 0 to Pred(my_stringlist.Count) do
memo1.Lines.Add(my_stringlist.Strings[i]);
end; // FormCreate()
Now, here's the interesting part ... it throws the exception on Inc(i) and, if I replace that while loop with a for loop ...
for i := 0 to Pred(ADOQuery.Fields.count) do
resultSet.Add(ADOQuery.Fields[i].Value);
it works just fine.
I suppose that I could just use the for loop & move on, but I would like to understand what is going wrong .... can someone explain to me? Thanks

The first thing that jumps out at me is that
i := 0;
while not AdoQuery.eof do
begin
resultSet.Add(ADOQuery.Fields[i].Value);
AdoQuery.Next;
Inc(i);
end;
and
for i := 0 to Pred(ADOQuery.Fields.count) do
resultSet.Add(ADOQuery.Fields[i].Value);
are not semantically equivalent! When you call Next, you're advancing the current record in the dataset. A loop until you hit EOF will run through each record in the dataset once. But the second loop never calls Next and doesn't check for EOF; it's grabbing all the fields from one record.
If I had to guess what's causing the exception in the first loop, I'd say that you've got more records (rows) than fields (columns) in your dataset, and so after enough iterations, i ends up at ADOQuery.Fields.Count and you get an index out of bounds error.
What exactly are you trying to do here?

In the while loop you are looping as often as there are are records
while not AdoQuery.eof do
but then you access the Field
ADOQuery.Fields[i]
while i represents the current "record number"...
This would crash in any program only depending on the relationship between record count of the query versus field count of the query... as soon as record count is higher than field count it crashes...
The for loop is part of the fix for this...
BUT since you don't provide the information whether you need all fields for one record or all fields of all records as resultset it is hard to provide fixed code...

Related

Get id from Values in a StringList

I have a StringList with 2 rows
aOldPriceTerms[0] = 'FuelAddition=336643160'
aOldPriceTerms[1] = 'Freight=336643155'
So it works fine to use
aOldPriceTerms.Values['Freight'] -> '336643155'
I want to have a list of ID's from the list.
So simply
'FuelAddition','Freight'
Currently I use this code where aOldPriceTerms is the actual StringList.
function GetList(aOldPriceTerms: TStringList): String;
var
vOldTerm : string;
vOldTermsList : TStringList;
begin
vOldTermsList := TStringList.Create;
try
for i := aOldPriceTerms.Count - 1 downto 0 do
begin
vOldTerm := Copy(aOldPriceTerms[i], 1, (Pos('=', aOldPriceTerms[i])-1));
vOldTermsList.Add(vOldTerm);
end;
Result := vOldTermsList.CommaText;
finally
FreeAndNil(vOldTermsList);
end;
end;
My question is there a cleaner way to get the ids ?
Example is from the Delphi Basics, but TStringList.Names is also described in the Delphi documentation
// Now display all name and age pair values
for i := 0 to names.Count-1 do
begin
ShowMessage(names.Names[i]+' is '+names.ValueFromIndex[i]);
end;
You can use the TALNVStringList (NV for nameValue) from alcinoe (https://github.com/Zeus64/alcinoe) to handle Name and Value without all the time splitting the string
TALNVStringList is exactly the same as TStringList (nothing to change in the code except replacing TstringList by TALNVStringList) except that it's more efficient in speed because it's store the name in one dedicated field and the value in another dedicated field (no need to do all the time pos('=') and copy() to retrieve the name and the value of the row)
for i := 0 to aOldPriceTerms.Count-1 do
begin
ShowMessage(aOldPriceTerms.Names[i]+' is '+aOldPriceTerms.ValueFromIndex[i]);
end;
Exe demo showing the speed penalty of classic TstringList: https://svn.code.sf.net/p/alcinoe/code/demos/ALSortedListBenchmark/win32/ALSortedListBenchmark.exe
To get all the id's
for name in names.Names do
begin
i := names.IndexOf[name];
end;
or
To get all the Values
for name in vOldTermsList.Names do
begin
Value := vOldTermsList.Value[name];
end;

dcc32 Hint H2077 Value assigned to 'objParam' never used

When building the application (code below) a hint is displayed:
H2077 Value assigned to objParam never used
How do I resolve this hint? Is it even applicable in my case?
function TESPGenerateParamList.RandomizationTimeConfiguration(SRandomizationTimeNode: string; eConfigType: string): Boolean;
var
objParam: Param;
sFirstNode : string;
nStartPos,nEndPos : word;
begin
try
try
objParam := ParamSchedulerRandomizationTime.Create;
if eConfigType = 'SETPARAM' then
begin
ParamSchedulerRandomizationTime(objParam).FrameType := Set_Param;
//TIMEOUT Node
sFirstNode := '';
if SearchNode(rsMinutes,SRandomizationTimeNode,sFirstNode,nStartPos,nEndPos,false) then
begin
ParamSchedulerRandomizationTime(objParam).SetParam(0, strtoint(trim(sFirstNode)));
end;
end
else if eConfigType = 'GETPARAM' then
begin
ParamSchedulerRandomizationTime(objParam).FrameType := Get_Param;
ParamSchedulerRandomizationTime(objParam).GetParam(0);
end;
slConfigurationList.AddObject(objParam.ClassName, objParam);
result := true;
except
on E: Exception do
begin
LogErrorMessage('uTESPGenerateParamList-->RandomizationTimeConfiguration' + E.Message);
result := false;
raise;
end;
end;
finally
objParam := nil; //(for here it give hint)
end;
end;
The compiler is absolutely correct. You don't refer to the variable after that assignment. The next thing that happens in all cases is that the function terminates.
To resolve the hint, delete the assignment statement entirely. Then you can remove the surrounding try-finally block, too, since nothing happens in the finally section.
But that's assuming the assignment statement was the proper way to dispose of the referenced object in the first place. It's probably not, if Param is a class type rather than an interface. In that case, keep the try-finally block, but replace the assignment with Param.Free, just like you've surely seen in dozens of other Delphi examples. Then, move the initial objParam assignment up two lines so it occurs before you enter the first try section.

Delphi ComboBox Access violation on combobox

I get Access violation error, but I know the code is correct, so where could be the problem? I'm trying to fill a ComboBox whit data from a local AccessDB.
var i : integer;
x : string;
begin
with DataModule3.ADOTable1 do begin
if RecordCount > 0 then
for i := 1 to RecordCount do begin
RecNo := i;
x := FieldByName('Teacher').AsString;
ComboBox1.Items.Add(x);
end;
end;
end;
I have tried lots of things and nothing works, I have tried lots of combobox typed but still doesn't work the only time a combobx showed value was when I selected a row in table then it showed in combobox the rows value by which I need to filter...
Access Violation is raised most probably because you have forgot to instantiate your datamodule DataModule3. Verify this by calling Assigned function.
begin
with DataModule3.ADOTable1 do
if Active then
while not Eof do
begin
ComboBox1.Items.Add(FieldByName('Teacher').AsString);
Next;
end;
end;

Error: Object Was Open - Delphi

I have the following issue, When ever I run this code in the procedure, getting a SQL from my SQL COMPACT DATABASE(sdf), it gives me an error "Object Already Open". How can I fix this. Below is my code for the procedure
Function GetSQL(sName: String; Var App: TApplication): String;
Var
Q: TADOQuery;
Begin
Q := TADOQuery.Create(App);
Q.ConnectionString := GetConnectionStringFromRegistry;
Q.Close;
Q.SQL.Text := 'SELECT * FROM SQLs WHERE Name = :sname';
Q.Parameters.ParamByName('sname').Value := sName;
Try
Q.Open;
If Q.RecordCount >= 1 Then
Begin
Q.First;
Result := Q['Query'];
Q.Close;
End;
Finally
Q.Free;
End;
End;
[This is what the error looks like]
[This is what the code looks like when I press Break]
The only thing I see that could be a problem is that your code leaves the query open if there are no rows returned:
Q.Open;
Try
If Q.RecordCount >= 1 Then
Begin
Q.First;
Result := Q['Query'];
Q.Close; // If Q.RecordCount = 0 above, this line never executes
End;
Finally
Q.Free;
End;
Move the Q.Close inside your finally instead, so it will always be called:
Q.Open;
Try
If Q.RecordCount >= 1 Then
Begin
Q.First;
Result := Q['Query'];
End;
Finally
Q.Close; // This will always run, even if no rows are returned
Q.Free; // or if an exception occurs.
End;
As an aside, you should use parameterized queries instead of concatenating the text, especially if you're running the same query multiple times with the only change being the value of sName. The server is smart enough to cache the compiled query and only replace the parameter value, which means your code executes faster and with less load on the server.
Function GetSQL(sName: String; Var App: TApplication): String;
Var
Q: TADOQuery;
Begin
Q := TADOQuery.Create(App);
Q.ConnectionString := GetConnectionStringFromRegistry;
// I've even tried closing it first
Q.Close;
Q.SQL.Text := 'SELECT Query FROM SQLs WHERE Name = :sname';
Q.ParamByName('sname').AsString := sName;
Try
// Error occurs here
Q.Open;
//Q.Active := True;
If Q.RecordCount >= 1 Then
Begin
Q.First;
Result := Q['Query'];
End;
Finally
Q.Close;
Q.Free;
End;
End;
Thanks to #user582118 for reminding this one...
This is actually a bug in the OleDB provider for SQL CE. If you have nvarchar fields greater than 127 characters in a table and you do a select query on that table, you will receive the DB_E_OBJECTOPEN error.
Original thread : https://stackoverflow.com/a/14222561/800214

Check If TEdit's Text Property Is Null On A Frame

So here is my situation. I have a Form (MainMenu) and a Frame (TestFrame). TestFrame is displayed on a TPanel located on MainMenu. Using this code:
frTestFrame := TfrTestFrame.Create(nil);
frTestFrame.Parent := plMain;
frTestFrame.Align := alClient;
frTestFrame.Visible := true;
TestFrame displays fine with no error. TestFrame has a few TEdit boxes on it. A TButton on MainMenu calls a procedure located in TestFrame to check if the TEdit boxes text property is null.
procedure TfmMainMenu.tbCheckClick(Sender: TObject);
begin
frTestFrame.Check;
end;
This function on TestFrame is supposed to go through all the "TEdit" components and use the function GetErrorData that returns a string if the TEdit's text property is null. That string is added to a TStringList and displayed if any TEdit boxes are null.
function TfrTestFrame.Check: Boolean;
var
ErrorList: TStringList;
ErrorString: string;
I: Integer;
begin
ErrorList := TStringList.Create;
for I := 0 to (frTestFrame.ComponentCount - 1) do
begin
if (frTestFrame.Components[I] is TEdit) then
begin
ErrorString := GetErrorData(frTestFrame.Components[I]);
if (ErrorString <> '') then
begin
ErrorList.Add(ErrorString);
end;
end;
end;
if (ErrorList.Count > 0) then
begin
ShowMessage('Please Add The Following Information: ' + #13#10 + ErrorList.Text);
result := false;
end;
result := true;
end;
function TfrTestFrame.GetErrorData(Sender: TObject): string;
var
Editbox: TEdit;
ErrorString: string;
begin
if (Sender is TEdit) then
begin
Editbox := TEdit(Sender);
if (Editbox.Text <> '') then
begin
Editbox.Color := clWindow;
result := '';
end
else
begin
Editbox.Color := clRed;
ErrorString := Editbox.Hint;
result := ErrorString;
end;
end;
end;
The problem is that when it hits the line "for I := 0 to (frTestFrame.ComponentCount - 1) do
" It blows up and I get the error "Access violation at 0x00458... Read of address 0x000..."
I do not know why this error is happening. I can only assume that maybe the Frame is not getting creating. Any help would be great. Thanks in advance.
According to your question, the line
for I := 0 to (frTestFrame.ComponentCount - 1) do
leads to an access violation at address 0x000..... Now, for a start, why won't you tell us the precise error message with the full details? Hiding the address makes it harder!
Anyway, it looks like the address is going to be a value very close to zero. In any case the only explanation for an access violation there is that frTestFrame is invalid. Most likely it is nil.
I note that the code in question is inside a TfrTestFrame method. So why do you use frTestFrame to refer to the object? You are already inside an instance of the object. Do you have multiple global variables named frTestFrame? Perhaps one in the main form unit and one in the frame unit?
You should stop using global variables for your GUI objects. I know that the IDE leads you that way. Resist the temptation to program that way. Abuse of global variables leads to pain and suffering.
Since the code is inside a TfrTestFrame method you can use Self. In all your TfrTestFrame methods remove all references to frTestFrame. Your loop should be like this:
for I := 0 to ComponentCount - 1 do
and the rest of the methods in that class need similar treatment. Note that you don't need to explicitly write Self and it is idiomatic not to.
Finally, I urge you to learn how to use the debugger. It's a wonderful tool and if you would use it, it would have told you what the problem was. Don't be helpless, let the tools help you.

Resources