I have application mvc4 c# and I need add
Multiple record to database but wen I use db.savechanges() in foreach I get error
And Wen use it outside of loop it can save last recode only
did you try a thing like AddRange?
instead of foreach try to do AddRange(toAddCollection) and then SaveChanges()
Related
Good day,
In my Ms Access 2010 Form, I have 4 textboxes, namely GrossAmt, WTax1, WTax2, and NetAmt.
GrossAmt, WTax1, and WTax2 are bounded. And so, their values are stored in the table.
NetAmt is unbounded as I have a function running. In its Control Source I have this:
=[GrossAmount]-([WTax1]+[WTax2]).
which is running properly. My problem is, how can I save the value(answer) in the table?
I have tried adding another textbox (named NetAmount) which is bounded to the table and was supposed to copy the values from the unbounded textbox. I have tried putting Me.NetAmount = Me.NetAmt in the "After Update" Event in Property but its not saving.
What should I do, I don't have any clue on what to do anymore. Thank you for your help.
with the equals sign, the value is not stored anywhere, it is calculated on the fly.
If GrosssAmount, WTax1 & WTax2 are stored in the database you never need to store that data, instead you can run the calculation based on the other three. In fact, I would recommend that you do it this way, unless there is a particular need to retrieve a historic net amount.
If you wanted to save the data to the database, you're going to have to use code on Update.
the easiest would be to write a function, something like
Private Function saveNetAmount()
Me.[NetAmount] = [GrossAmount]-([WTax1]+[WTax2])
End Function
and then add that into the AfterUpdate or OnChange event for the Gross Amount, WTax1 and WTax2 text boxes.
Something like:
Private Sub GrossAmt_AfterUpdate()
Call saveNetAmount
End Sub
I am referencing the 2 step newsletter example at http://agiletoolkit.org/codepad/newsletter. I modified the example into a 4 step process. The following page class is step 1, and it works to insert a new record and get the new record id. The problem is I don't want to insert this record into the database until the final step. I am not sure how to retrieve this id without using the save() function. Any ideas would be helpful.
class page_Ssp_Step1 extends Page {
function init(){
parent::init();
$p=$this;
$m=$p->add(Model_Publishers);
$form=$p->add('Form');
$form->setModel($m);
$form->addSubmit();
if($form->isSubmitted()){
$m->save();//inserts new record into db.
$new_id=$m->get('id');//gets id of new record
$this->api->memorize('new_id',$new_id);//carries id across pages
$this->js()->atk4_load($this->api->url('./Step2'))->execute();
}
}
}
There are several ways you could do this, either using atk4 functionality, mysql transactions or as a part of the design of your application.
1) Manage the id column yourself
I assume you are using an auto increment column in MySQL so one option would be to not make this auto increment but use a sequence and select the next value and save this in your memorize statement and add it in the model as a defaultValue using ->defaultValue($this->api->recall('new_id')
2) Turn off autocommit and create a transaction around the inserts
I'm from an oracle background rather than MySQL but MySQL also allows you to wrap several statements in a transaction which either saves everything or rollsback so this would also be an option if you can create a transaction, then you might still be able to save but only a complete transaction populating several tables would be committed if all steps complete.
In atk 4.1, the DBlite/mysql.php class contains some functions for transaction support but the documentation on agiletoolkit.org is incomplete and it's unclear how you change the dbConnect being used as currently you connect to a database in lib/Frontend.php using $this->dbConnect() but there is no option to pass a parameter.
It looks like you may be able to do the needed transaction commands using this at the start of the first page
$this->api->db->query('SET AUTOCOMMIT=0');
$this->api->db->query('START TRANSACTION');
then do inserts in various pages as needed. Note that everything done will be contained in a transaccion so if the user doesnt complete the process, nothing will be saved.
On the last insert,
$this->api->db->query('COMMIT');
Then if you want to, turn back on autocommit so each SQL statement is committed
$this->api->db->query('SET AUTOCOMMIT=1');
I havent tried this but hopefully that helps.
3) use beforeInsert or afterInsert
You can also look at overriding the beforeInsert function on your model which has an array of the data but I think if your id is an auto increment column, it won't have a value until the afterInsert function which has a parameter of the Id inserted.
4) use a status to indicate complete record
Finally you could use a status column on your record to indicate it is only at the first stage and this only gets updated to a complete status when the final stage is completed. Then you can have a housekeeping job that runs at intervals to remove records that didn't complete all stages. Any grid or crud where you display these records would be limited with AddCondition('status','C') in the model or added in the page so that incomplete ones never get shown.
5) Manage the transaction as non sql
As suggested by Romans, you could store the result of the form processing in session variables instead of directly into the database and then use a SQL to insert it once the last step is completed.
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
I just started learning entity and have done some playing with it in Winforms and now with Webforms. It was working fine for a few days and now I get the error when I try to update a field in the DataView. 'AutoGenerateEditButton="True" in the DV. The DV is wired to the EntityDataSource. A friend accessed the webform and edited one from a diff location and was the last time the edit/update worked.
This is the updating event in case that helps:
Sub dgv_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles dgv.RowUpdating
Try
Dim row As GridViewRow = dgv.Rows(dgv.EditIndex)
Dim list As DropDownList = CType(row.FindControl("ddlDoseType"), DropDownList)
e.NewValues("DoseType") = list.SelectedValue
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Any ideas?
You can reproduce this error if you do the following
Client A reads a row with ID = 1
Client B deletes the row with ID = 1
Client A changes the contents of the row
Clinet A tries to save the changes
Since the row with ID = 1 does not exist you get the error that was in your question.
It does not look like the code you posted is causing the error, since that is the updating of the datagrid, not the saving of data to the database.
I want a user to be able to do a search in the layout/table "Cages" and then click a button to run a script that will create a new record in a table called "CagesProtocolLineHistory" for each record in the found set.
Below is what I have so far which almost works but on go to original layout line it doesn't go to the next record, it goes to a record near the end. I.e., it's skipping some records.
Yes, Go to Record[ first ] before the loop will ensure that all records are copied. Otherwise, if the script starts at some record other than first, it will skip all records before this.
A couple of notes: FileMaker string comparison is case-insensitive by default, so you don't need to use Upper() here. Also, in most cases it's simpler not to copy all data through variables, but pass a single key and copy other data via lookups.
You could do this in a three step: search for the "yes"s in a new window; export the record ids (passed params) to a local temp file, and reimport the ids into the child table.
Can any one help me why this error occurs when i update using sqlDataadapter with join query
Dynamic SQL generation is not supported against multiple base tables.
You have a "join" in your main query for your dataset (The first one in the TableAdapter with a check by it). You can't automatically generate insert/update/delete logic for a TableAdapter when the main query has multiple tables referenced in the query via a join. The designer isn't smart enough to figure out which table you want to send updates to in that case, that is why you get the error message.
Solution. Ensure that your main query only references the table you want the designer to write insert/update/delete code for. Your secondary queries may reference as many tables as you want.
It was in the case that i was trying to set value for identity column in my datarow. Simply i deleted the code to set value for identity column and it will work.
My Scenario:
Database:
uin [primary, identity]
name
address
Whenever i tried to set the datarow("uin") the error occurs. But works fine with datarow("name") and datarow("address").
Hope it works for you too