i have form view i wrote this code in event sqlds_inserted
text1.text=e.Command.ExecuteScalar().ToString();
to have the id for the last inserted record
it works but every time i add record it inserts tow records with the same data
when i delete this code
it works fine
The formview is inserting a record via the default sqldatasource (or objectdatasource, etc...) and then you are calling another insert via the ExecuteScalar method (e in your code represents the datasource you are using).
Related
How do I get notified if a record has been inserted into mysql table by another database client, is this possible?
void QSqlTableModel::beforeInsert(QSqlRecord &record)
This signal is emitted by insertRowIntoTable() before a new row is inserted into the currently active database table. The values that are about to be inserted are stored in record and can be modified before they will be inserted.
QSqlDriver supports notifications , however not for Mysql
I have a table tblEmp with a After Insert data macro defined. However, the event is not working. what I am missing here?
Simply move your logic to the BeforeChange trigger which is the state before any record (new or existing) is saved. Your logic attempts to change field values in AfterInsert which is after the save mode. Also, be sure to not include the table identifier, tblEmp, in referencing column name in macro:
In fact, had you clicked the Application Errors on status bar in lower right after your current attempt, the outputted system table indicates the issue since you are in read-only mode after inserting:
EditRecord failed because the default alias represents a record which
is read only.
Hypothetically, I am going to develop a trigger that inserts a record to Table A when an insertion made to an Table A.
Therefore, I want to know how the system handles that kind of loophole or it is going to continue as a loop until the system hangs which requires restart and possibly remove the DB.
I'm trying to gather information on almost every DBMS on this issue or loophole.
I can only speak to Oracle, I know nothing of MySQL.
In Oracle, this situation is known as mutation. Oracle will not spiral into an endless loop. It will detect the condition, and raise an ORA-04091 error.
That is:
ORA-04091: table XXXX is mutating, trigger/function may not see it
The standard solution is to define a package with three functions and a package level array. The three functions are as follows:
initialize - this will only zero out the array.
save_row - this will save the id of the current row (uk or pk) into the arrray.
process_rows - this will go through the array, and actually do the trigger action for each row.
Now, define some trigger actions:
statement level BEFORE: call initialize
row level BEFORE or AFTER: call save_row
statement level AFTER: call process_rows
In this way, Oracle can avoid mutation, and your trigger will work.
More details and some sample code can be found here:
https://asktom.oracle.com/pls/asktom/ASKTOM.download_file?p_file=6551198119097816936
You can only insert a record in same table if you are using instead of trigger. In all other cases you can only modify the record being inserted.
I hope this answers your quest.
you can create trigger in mysql DBMS.
check below link for create insert trigger syntex
http://www.techonthenet.com/oracle/triggers/after_insert.php
The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship.
This is the error message I receive when I am halfway through keying in data into my subform. My subform's source is a Query that joins two Tables on a one-to-one parent-child relationship. I assume the following happens when I key in data:
I have an After Insert data macro that creates a row in the child table.
The data macro fires and a row is created in the child table with the corresponding foreign key.
Microsoft Access is not aware that I have created that row in the child table.
When I key in data that belongs to the child table in the subform, it automatically tries to add in a new row into the child table. This fails because a row already exists with the same foreign key.
Is there any way I can bypass this behaviour? Can I bridge the link between the parent and child table the moment the data macro is fired? I can't forgo the After Insert data macro because it contains some biz logic that helps identify which child table to insert into, and it's important that the row is created in the child table as soon as data is filled in the subform and not only when the child table's fields are touched in the subform.
Many thanks!
EDIT: I tried working around this by running Me.Requery in the After Insert macro of the subform. Seems to work fine for a single record, but with a batch copy-paste the error Operation not supported in transactions. occurs.
EDIT 2: I even found this forum thread that says that it's kinda impossible to Requery after a transaction. I'm kinda sure there should be a way around this because it seems kinda user-unfriendly...
I generally recommend against using timers but this is a case where you could use one. On the AfterInsert set the form's TimerInterval at something like 500. Then put your Me.Requery code on the Timer event and also set the TimerInterval back to 0.
After AfterInsert event should set the TimerInterval back to 500 which will prevent the timer from firing until all records have been pasted in.
I see you are using Macros. I don't know if you can use the timer in Macros or not. I don't use macros at all and I recommend learning VBA instead of using them.
Private Sub Form_AfterInsert()
Me.TimerInterval = 500
End Sub
Private Sub Form_Timer()
Me.Requery
Me.TimerInterval = 0
End Sub
I am currently working on a big web project using ASP and MySQL.
When inserting into multiple tables I've been using last_update_ID(), but after some research I've found that that SQL statement isn't safe.
So. the problem:
I use two different computers, with different internet connections.
Both computers are logged onto the system I am currently building. I have made a page that prints the connection_id(), and last_update_id.
If I update any table with one of the computers the other one also gets that last_update_ID.
Both computers have the same connection_ID.
What can I do to get around this?
I don't want to (if it's not necessary) do a select statement after the first INSERT; to search for the row that I inserted, to get the correct ID of that row.
It's not my server I am using so I can't make any large changes of the database.
I guess that this problem occurs because the webpages use the same loginName & password to connect to the database, is that true?
Is there any other alternative to get the last update ID? that is totally safe..
I close every connection at the end of the asp page. but that doesn't change the connection_ID.
The connection ID is the for a few minutes even thou I open up different web pages on the server.
I believe the LAST_INSERT_ID() is correct for the current session. So each session receives it's own correct value. Either I don't understand your question or you think you have a problem but you don't.
I am not aware of any LAST_UPDATE_ID() function, on an update you can easily retrieve the updated rows by SELECTing them with the same WHERE clause (before the update)?
reference: http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
For LAST_INSERT_ID(), the most
recently generated ID is maintained in
the server on a per-connection basis.
It is not changed by another client.
It is not even changed if you update
another AUTO_INCREMENT column with a
nonmagic value (that is, a value that
is not NULL and not 0). Using
LAST_INSERT_ID() and AUTO_INCREMENT
columns simultaneously from multiple
clients is perfectly valid. Each
client will receive the last inserted
ID for the last statement that
client executed.
If you want to retrieve the LAST_INSERT_ID from an INSERT query with an ON DUPLICATE KEY UPDATE clause, you can also use the LAST_INSERT_ID() function to retrieve the value of the AUTO_INCREMENT column that was updated:
reference: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
If a table contains an AUTO_INCREMENT
column and INSERT ... UPDATE inserts a
row, the LAST_INSERT_ID() function
returns the AUTO_INCREMENT value. If
the statement updates a row instead,
LAST_INSERT_ID() is not meaningful.
However, you can work around this by
using LAST_INSERT_ID(expr). Suppose
that id is the AUTO_INCREMENT column.
To make LAST_INSERT_ID() meaningful
for updates, insert rows as follows:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
Your server appears to have connection pooling turned on. What this means is that the database connection is held open after a script finishes, and the next script that comes along uses it, and thus can see any variables that were set on that connection, including LAST_INSERT_ID().
What can't happen is two script instances sharing a connection at the same time. Thus, if your server is busy enough to need to run two script instances at exactly the same time, it will simply create a second database connection, with its own separate LAST_INSERT_ID() variable, and won't interfere with the first.
In short, as long as the INSERT and the LAST_INSER_ID() request happen within the same script (and you don't somehow close the database connection between them), they're completely safe, as your script has exclusive use of that connection.