Forum > Databases

[resolved]Debugging SQLQuery.EOF loop with a strange SQLQuery.Next code redirect

<< < (3/4) > >>

indydev:
I removed the inner exception block.

The problem is line 11 (or at least one problem).

SessionID := PtrInt(Subjects.Items.Objects[SelectedIndex]);

If I change it to a value in the table directly

SessionID := 4;

Then the errors go away.

I tried changing PtrInt to PtrUInt but that did not make a difference.

I changed SessionID to PtrInt type, and that didn't make a difference

Unfortunately I am traveling soon, so I might be able to read a response, but I won't be able to implement anything for approximately 24 hours.

Thank you so much for helping.

rvk:

--- Quote from: indydev on June 13, 2024, 05:39:37 am ---The problem is line 11 (or at least one problem).

SessionID := PtrInt(Subjects.Items.Objects[SelectedIndex]);
--- End quote ---
Ha, yes, that makes more sense.

First (again) a small comment about ItemIndex ( -> SelectedIndex). It can be -1. So you might want to check if there really is something selected.
https://lazarus-ccr.sourceforge.io/docs/lcl/stdctrls/tcustomcombobox.itemindex.html

Then... You cast the Object from Items with PtrInt. PtrInt is a Int64 (I assume you are on 64 bit). But you declared SessionID in BtnLoadClick as an Integer !!

I assume SessionId is something that's always an integer in the database (because you do SessionID := SQLQuery.FieldByName('id').AsInteger ).
Then you shouldn't use PtrInt at all. It's for pointers, not for integers.

Change the PtrInt in your calling LoadSubjects to Integer.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.LoadSubjects;var  SessionID: Integer;Change the line in BtnLoadClick with a cast to Integer and add the check for SelectedIndex.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if SelectedIndex < 0 then exit;SessionID := Integer(Subjects.Items.Objects[SelectedIndex]);
Always save and restore items from .Objects with the same type (which wasn't done here) ;)

Reference for types: https://saco-evaluator.org.za/docs/fpc/ref/refsu4.html

(If sessionid is something bigger than an Integer, i.e. signed 32 bit, then you might need to change the database type and assignments too)

cdbc:
Hi
PtrInt & PtrUInt are aliases to the 'pointer-sized-integer' for the platform, signed or unsigned, they are 2=16bit, 4=32bit & 8=64bit bytes in size.
'SizeInt' is another alias... they fit in a register, I think...
Regards Benny

rvk:

--- Quote from: cdbc on June 13, 2024, 11:24:44 am ---PtrInt & PtrUInt are aliases to the 'pointer-sized-integer' for the platform, signed or unsigned, they are 2=16bit, 4=32bit & 8=64bit bytes in size.
'SizeInt' is another alias... they fit in a register, I think...

--- End quote ---
Yes, and seeing that SessionID from the database is taken as .AsInteger, you shouldn't stuff that into a PtrInt  :D
(and later take it out as cast with PtrInt into a Integer type, as is done here)

If you are on 32 bit platform, you might get away with it because then PtrInt is a LongInt (which you can interchange with that Integer).
But an Integer is still 4 bytes on 64 bit, while PtrInt points to a Int64.
Mixing those while CASTING, you'll get in a whole heap of trouble ;)

cdbc:
Hi
You are absolutely right.
I wasn't arguing that, but there are still people out there, who are not sure about this ptrint business...
Regards Benny

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version