I am using GridView in asp .net and editing data with edit command field property (as we know after updating the edited row, we automatically update the database), and I want to use transactions (with begin to commit statement - including rollback) to commit this update query in database, after clicking in some button (after some events for example), not automatically to insert or update the edited data from grid directly to the DB...so I want to save them somewhere temporary (even many edited rows - not just one row) and then to confirm the transaction - to update the real tables in database...
Any suggestions are welcomed...
I've used some good links, but very helpful, like:
http://www.asp.net/learn/data-access/tutorial-63-cs.aspx
http://www.asp.net/learn/data-access/tutorial-66-cs.aspx
etc...
Well,
you can store your edited data in a DataTable in session. and then pass this data table as a bulk insert in to the database. 2 options are available for this
if you are using SQL Server 2005 you can use OpenXML to achieve this, as i have stated here
if you are using SQL Server 2008 youc an use Table Variables like i did here.
i hope it helps
First way:
Create session variable that will contain your DB object (DataTable or mapped objects).
The GridView should work with this instance instead of sending the data to the database.
Once editing is finished you may take the object from the session and save it in the way you normally do.
Second way:
I would use javascript to collect all changes on the client side while he is editing as a array of objects (each object is separate row).
Once the editing done, you can create json string from the collection and pass it to the server.
If your json object configuration is same as server class then you can use JavaScriptSerializer to deserialize your string into collection of object.
After that, you can save your objects in the way you normally do.
Related
I recently made a monopoly game using C# Winforms and now I need to convert it to a web application. I'm using ASP.NET and I'm having some issues with variables being reset on button clicks.
In order to resolve this, I was planning on having all data stored within a SQL Server database whenever it is changed, and then retrieved when needed. The main issue is that I have 2 classes, Square and Player.
I can handle the data for player fine but each square has a Player associated with it called "Owner". I'm just wondering if there is any way that in the SQL Server database I can set the data type for "Owner" to the "Player" class so that I can save that along with all the other data. Thanks in advance.
SQL Server was designed for and works best for structured tabular data. For complex objects, this usually means it takes several records over several tables to represent a single object.
However, what you are trying to do is store a complex object in a single column in SQL Server. You can do this in SQL Server, by serializing your object to a string (usually XML or JSON) and storing the string representation of your object in an nvarchar(max) column in SQL. When you fetch the record, deserialize it back into an object. You lose quite a bit of power in SQL Server when doing this, but if you don't need to manipulate the data within your object server-side, then it works fine (you can do limited XML and JSON processing within SQL server, but from the sounds of what you are doing, you won't need to).
One of the easiest and most popular ways to serialize objects is to use Newtonsoft JSON. Add the NuGet package for Newtonsoft, and then serialize your objects by calling string myComplexObjectAsAString = JsonConvert.SerializeObject(myComplexObject);. You can send that string to SQL, and when you get it back, use MyComplexObjectType myComplexObject = JsonConvert.DeserializeObject<MyComplexObjectType>(theStringDataThatYouGotFromSqlServer);
Can i alter/intercept the event when updating an entity?
For example, for when i update the company entity?
The idea is to delete somehting in the BBDD after the entity is updated
You can use Table Scripts to run functions when a record is inserted, after a record is inserted, when a record is updated, or when a record is deleted.
Table Scripts use JavaScript, but have access to the server-side functions (such as FindRecord, SQL statements, etc).
So, you can add a Table Script to the Entity to delete a record via a SQL statement when a record is updated.
It's difficult to give you an example without knowing exactly what you're trying to do though.
Six Ticks Support
I have a large relational Access 2010 database. It is normalized, and includes some union queries that are very slow. I therefore thought I could speed things up by creating some cached fields. For example in tblOrder I would create a CustomerName field. To maintain this cached field I created a Before Change data macro that would dLookup the customer's company name from tblCustomer. It worked great. Then I created an After Update data macro in tblCustomer so when the user changes the Company Name all the child records would automatically be updated. It worked, but then the Before Change data macro fired and the dLookup returned the old Company Name. Any help would be very appreciated.
I made a sample of my problem using the Northwing Database. You can download a copy of it at http://www.thetechmentors.com/freestuff/exerciseFiles/msAccess/DlookupDatamacroProblem.zip
All you need to do is tweak the Before Change data macro on [tblOrder] to do the name lookup only when the [CustomerID] changes in that table. You can do that using the Updated() function like so:
That way when the macro fires as a result of the update performed from the After Update data macro on [tblCustomer], the [tblOrder].[CustomerID] value has not changed so the name lookup is bypassed.
I am using ASP.Net webform for my development and currently implement datatables.net. to perform some excel like data entry job. For more info, please go to http://datatables.net/release-datatables/extras/KeyTable/editing.html. I also added some add row and delete row functions at client side. Now I am stuck on how to push the entire table data to the server. It's look like excel and user can make the amendment. Whenever they plan to save the data, the user just need to click on save button and the entire table info shall be submitted to the server. For your information, no input textbox in this case because I am using keytable feature.
Please help!
Thanks.
I believe this datatables.net server side usage page has sServerMethod which may be fullfill your requirement. The data are passing in JSON format to server. In server side you can parse the JSON back to your DTO to update your data.
You can SqlBulkCopy for faster insertion of data in the database. You can search code for that on net.
You'll need to create an object collection of some kind in your handler to temporarily save the data into some kind of structure or object. What I did was append a row_id to the table row in the fnRowCallback of the Datatable, then grabbed it in the submitdata property of the Editable plugin, sending it to the server. Once there, I store it in an object, add it to the collection, and let editable update the table.
When you are ready to submit, fire it off, go to the handler, and send all of the data in the collection to the server as a mass update.
I recently began working on a project which has many gridviews on a single page. During creation of a new record, the user needs to be able to add/remove/edit these gridviews and then save to the database at the end. The problem with this obviously is that there is no datasource to bind the data too until after its written to the database.
This data represents a 1..* relationship, which is why the gridview data cannot be written to the database until the parent record has been created first.
The best way I have found so far to solve this is to use viewstate. This solution however does not seem ideal to me. I am also forced to manually create the gridview functionality with OnDeleting, OnUpdating, etc so that I can manage the binding of the viewstate with the gridview.
Does anyone have any suggestions on a better way to manage this situation, it seems like it would be a common thing?
UPDATE:
Keep in mind this data needs to be around throughout postbacks.
Use a DataSet as an intermediate connection to your data source. Fill the DataSet with your data and then bind your GridView to the DataSet setting the GridView DataMember to the name of the table it is supposed to bind to.
As the user updates tables it will add/modify records in the DataTables in the DataSet. When the user is done editing and clicks "Save" your code can then update the database from the datasets, either automatically using a DataAdapter, or manually looking at the RowState of the rows in the DataTables.
Use a DataAdapter and a Dataset. Invoke the fillschema method in the adapter to create de metadata (cols, constraints, relations, etc) in the dataset. bind the data tables created to the different grid views. update manually cheking each row rowstate on each table or call the adapter's update method to do it automatically. If you do it automatically you need to define commands for insert, delete, and update in the adapter.