(SQL Server 2012, Delphi XE2, DataSet, DBGrid and adoquery)
I have a simple table with these columns:
id entering, exiting, description
entering
and exiting
the field to get the default value of 0
These defaults when creating the new record does not appear in Delphi. Let's say we get only 5 description field to field entering the summer. When recording is not a problem. No shortage does not appear in normal şartlarda. You can continue to operate on something other records.
But you enter the value you add just entering the field is nothing new, and the error occurs in the process of trying to unseen on record. 0 seems to have opened in the area off connections and is no longer an error.
Very rarely these default values appear and not an error.
I wonder how I solve this problem?
If your question is how to get default column values from the server, the code below shows two methods of doing this where the server is an MS Sql Server, and how to supply them to a new row in the table via its
OnNewRecord
event.OnNewRecord
is the dataset event you should use for setting field values for records as they are being inserted.To minimize the possibility of misunderstandings, the code is intended to be self-contained: It creates a new table which has an Identity column and three others which have default values, and then uses two different methods of adding data rows to it.
Note: Delphi's TField and its descendants have an
AutoGenerateValue
property, and setting this toarDefault
is supposed to retrieve the default column/field value from the server. In the Delphi7 era, the server-side functionality to do this was AFAIK only ever implemented in the DBTables unit for the long-obsolete BDE. In particular it was not supported by Delphi's ADO dataset types. I have not checked whetherAutoGenerateValue
works with other, more recent, Delphi versions and dataset types.Also, note that the Sql statement to create the table calls DROP TABLE before, and to avoid an error the first time it executes, you'll need to temporarily comment-out the DROP TABLE part.
Code:
The above code uses a second method of getting the column default values, by retrieving them from the server's database schema. To use this code, change the OnNewRecord code to refer to
GetColumnDefault2
instead ofGetColumnDefault
. This method is possibly preferable, because it only involves getting the default values once, and thereafter the AdoDataSet1 caches them.The above code was written for Delphi7 but should work with later versions, too.
If this answers your question, I'll try to edit your question to make it a bit clearer.
Finally, no Delphi answer that involves changing Sql in the client app seems complete these days without warning about the risk of Sql Injection - see https://en.wikipedia.org/wiki/Sql_injection.