Board index » delphi » Simple DBGrid Problem trying to Null out Foreign Keys

Simple DBGrid Problem trying to Null out Foreign Keys

Okay,

I have a real simple problem. I am hooking up to the local
interbase server.  I have a table displayed with all it's fields
displayed.

There's a few fields that are actually foreign keys nullable pointing
to another table's primary key.  This is all good until I try to delete
some of these columns.

I try to set certain foreign key records to null by highlighting it, and
hitting backspace.  Instead of having that field get set to null, and
all being good, Delphi decides to set it to "".  And interbase
complains about Foreign Key violation constraint errors.

How to I get the DBGrid to default save empty strings into NULL
fields?  Or any other table component for that matter?  In Access,
this is simply a property in the table def. But this is really kicking
my butt, and I imagine this should be a real simple thing...

If you've got a quick fix, please e-mail me at jimmy...@kuentos.guam.net.
I'm using Delphi 3, btw.

Jimmy

 

Re:Simple DBGrid Problem trying to Null out Foreign Keys


Okay,

I figured out the problem by reading a related article.  The solution
was to add some code into the event "BeforePost" In the TTable object.
I must have been barking up a tree, as I was looking for a property in the
DBGrid object that would produce this kind of behavior.

for i := 0 to Table1.FieldCount -1 do
   If (Table1.Fields[i].DataType = ftString) then
      If (Table1.Fields[i].AsString = '') then
         Table1.Fields[i].Value := Null;

This just goes through all the columns of the table and sees if they
are empty strings.  If they are, replace them for nulls.

I think this is also a really big problem with OOP.  You gotta have lot's
of good
documentation and even more examples.

Ah well, I ran into yet another problem, but it is unrelated to delphi.
Will post in a separate article.

Jimmy

Jimmy Huang <jimmy...@kuentos.guam.net> wrote in article
<01bf0cae$9d05a960$0100000a@jhs>...

Quote
> Okay,

> I have a real simple problem. I am hooking up to the local
> interbase server.  I have a table displayed with all it's fields
> displayed.

> There's a few fields that are actually foreign keys nullable pointing
> to another table's primary key.  This is all good until I try to delete
> some of these columns.

> I try to set certain foreign key records to null by highlighting it, and
> hitting backspace.  Instead of having that field get set to null, and
> all being good, Delphi decides to set it to "".  And interbase
> complains about Foreign Key violation constraint errors.

> How to I get the DBGrid to default save empty strings into NULL
> fields?  Or any other table component for that matter?  In Access,
> this is simply a property in the table def. But this is really kicking
> my butt, and I imagine this should be a real simple thing...

> If you've got a quick fix, please e-mail me at jimmy...@kuentos.guam.net.
> I'm using Delphi 3, btw.

> Jimmy

Other Threads