Board index » delphi » Fields[0] not always Fields[0]??

Fields[0] not always Fields[0]??

Hi, i have a TQuery descendent component that works over queries
assigning them a consecutive value in the DoOnNewRecord procedure. (Then
i call the DoOnNewRecord of the ancestor). The field of the consecutive
always is created in the first position of the fields.
(Query.Fields[0].AsInteger := MyConsec).
It works pretty well most of the time, but from time to time an error
raises when trying to post the record: "The field 'IdOfMyTable' can't be
null" but if you close the application and do the process again, all
works fine again. It's like if for some reason i don't understand,
referring to Fields[0] not always assign the value to the proper
database field.
Any ideas?
Thanks in advance
--
Guillermo Casta?o Acevedo
Gerente de Sistemas - Grupo Millennium Ltda
Guiller...@GrupoMillennium.com
www.GrupoMillennium.com
 

Re:Fields[0] not always Fields[0]??


Yes, order of field component in fields array may change at any time.
You can change it in dataset designer. If you connect it to grid, leave
default columns, reordering fields in grid will reorder fields in fields
array of dataset.
Etc.

--
Robert

Quote
Guillermo Casta?o A wrote in message

<3B7AE1D3.65A7C...@GrupoMillennium.com>...
Quote
>Hi, i have a TQuery descendent component that works over queries
>assigning them a consecutive value in the DoOnNewRecord procedure. (Then
>i call the DoOnNewRecord of the ancestor). The field of the consecutive
>always is created in the first position of the fields.
>(Query.Fields[0].AsInteger := MyConsec).
>It works pretty well most of the time, but from time to time an error
>raises when trying to post the record: "The field 'IdOfMyTable' can't be
>null" but if you close the application and do the process again, all
>works fine again. It's like if for some reason i don't understand,
>referring to Fields[0] not always assign the value to the proper
>database field.
>Any ideas?
>Thanks in advance
>--
>Guillermo Castano Acevedo
>Gerente de Sistemas - Grupo Millennium Ltda
>Guiller...@GrupoMillennium.com
>www.GrupoMillennium.com

Re:Fields[0] not always Fields[0]??


Even at run time? When the program is compiled? The problem i have is with
the .exe generated from my project!

Quote
Robert Cerny wrote:
> Yes, order of field component in fields array may change at any time.
> You can change it in dataset designer. If you connect it to grid, leave
> default columns, reordering fields in grid will reorder fields in fields
> array of dataset.
> Etc.

> --
> Robert

> Guillermo Casta?o A wrote in message
> <3B7AE1D3.65A7C...@GrupoMillennium.com>...
> >Hi, i have a TQuery descendent component that works over queries
> >assigning them a consecutive value in the DoOnNewRecord procedure. (Then
> >i call the DoOnNewRecord of the ancestor). The field of the consecutive
> >always is created in the first position of the fields.
> >(Query.Fields[0].AsInteger := MyConsec).
> >It works pretty well most of the time, but from time to time an error
> >raises when trying to post the record: "The field 'IdOfMyTable' can't be
> >null" but if you close the application and do the process again, all
> >works fine again. It's like if for some reason i don't understand,
> >referring to Fields[0] not always assign the value to the proper
> >database field.
> >Any ideas?
> >Thanks in advance
> >--
> >Guillermo Castano Acevedo
> >Gerente de Sistemas - Grupo Millennium Ltda
> >Guiller...@GrupoMillennium.com
> >www.GrupoMillennium.com

--
Guillermo Casta?o Acevedo
Gerente de Sistemas - Grupo Millennium Ltda
Guiller...@GrupoMillennium.com
www.GrupoMillennium.com

Re:Fields[0] not always Fields[0]??


"Guillermo Casta?o A" <Guiller...@GrupoMillennium.com> wrote in message
news:3B7DE637.20720BCF@GrupoMillennium.com...

Quote
> Even at run time? When the program is compiled? The problem i have is with
> the .exe generated from my project!

They do not change arbitrarily or randomly, but as per Robert's example, if
you change the order of columns in a grid at runtime (by either dragging or
in code) then the TField order in the dataset will be changed accordingly.

The easy solution in this case is to use FieldByName instead of
Fields[index].

--
Wayne Niddery (Logic Fundamentals, Inc.)
RADBooks: http://www.logicfundamentals.com/RADBooks/delphibooks.html
"Some see private enterprise as a predatory target to be shot, others as a
cow to be milked, but few are those who see it as a sturdy horse pulling the
wagon." - Winston Churchill

Re:Fields[0] not always Fields[0]??


The problem is that i'm building a generic TQuery descendant, so i don't know
the name of the first field....

Quote
"Wayne Niddery [TeamB]" wrote:
> "Guillermo Casta?o A" <Guiller...@GrupoMillennium.com> wrote in message
> news:3B7DE637.20720BCF@GrupoMillennium.com...
> > Even at run time? When the program is compiled? The problem i have is with
> > the .exe generated from my project!

> They do not change arbitrarily or randomly, but as per Robert's example, if
> you change the order of columns in a grid at runtime (by either dragging or
> in code) then the TField order in the dataset will be changed accordingly.

> The easy solution in this case is to use FieldByName instead of
> Fields[index].

> --
> Wayne Niddery (Logic Fundamentals, Inc.)
> RADBooks: http://www.logicfundamentals.com/RADBooks/delphibooks.html
> "Some see private enterprise as a predatory target to be shot, others as a
> cow to be milked, but few are those who see it as a sturdy horse pulling the
> wagon." - Winston Churchill

--
Guillermo Casta?o Acevedo
Gerente de Sistemas - Grupo Millennium Ltda
Guiller...@GrupoMillennium.com
www.GrupoMillennium.com

Re:Fields[0] not always Fields[0]??


You have at least two options:
1. In DBGrid create columns, so if user reorders columns, it does not affect
order of fields in dataset
2. use FieldDefs. FieldDefs have constant order reflecting order of fields
in select statement, so instead of:
Dataset.Fields[0]
you use
Dataset.FieldByname(Dataset.FieldDefs[0].Name);

--
Robert

Quote
Guillermo Casta?o A wrote in message

<3B7E7D24.6E9CD...@GrupoMillennium.com>...
Quote
>The problem is that i'm building a generic TQuery descendant, so i don't
know
>the name of the first field....

Other Threads