Handling concurrency exceptions in DataGridView - c#

This article here describes handling concurrency exceptions. The steps to reproduce the problem are:
Create a new Windows Application project.
Create a new dataset based on the Northwind Customers table.
Create a form with a DataGridView to display the data.
Fill a dataset with data from the Customers table in the Northwind database.
After filling the dataset, use the Visual Database Tools in Visual Studio to directly access the Customers data table and change a record.
Then on the form, change the same record to a different value, update the dataset, and attempt to write the changes to the database, which results in a concurrency error being raised.
Catch the error, then display the different versions of the record, allowing the user to determine whether to continue and update the database, or to cancel the update.
My question is, why does this even happen? Why can't I just save and edit the record from the DataGridView without causing any errors? I'm creating an app with a DataGridView and I'm facing this problem. I need some way to avoid or resolve this error without notifying the user, so whatever they see in the DataGridView gets saved exactly the way thy see it. What's the cause of that error?

The solution turned out to be pretty simple.
All you need to do is reload the data into the DataGridView again after every save.
So the code for the BindingNavigator save button is now:
this.Validate();
this.maintableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.yourDataSet);
this.maintableTableAdapter.Fill(this.yourDataSet.yourtable);
I have no idea why this works, so I need an expert to confirm this. Working solution though.
Thanks to E-Bat for planting this idea in my head.

Related

System.Data.SqlClient.SqlException (Invalid Column Name '...') after modifying database when using Data Source on a listbox

I just started developing my first application using Visual Studio. I have created my own SQL Server database and connected it to a listbox element using a Data Source which is working fine.
However I noticed that after modifying the Data Source (let's say removing a column) I get the error mentioned in the title, which makes perfect sense as obviously the column doesn't exist anymore. But how can I tell Visual Studio that it does not have to look for the removed column anymore?
After receiving the error, I only get directed to this line of code:
this.tableNameTableAdapter.Fill(this.dbNameDataSet.dbtable);
I can't really do anything with this. I've tried to look up the internet for several answers but I couldn't find someone who has encountered the same problem (probably because I used the wrong keywords).
Any advice? Thanks for helping me out!
Additional question: Is this called DataBinding?
You need to reconfigure the TableAdapter after any change in the schema:
configure-a-tableadapters-fill-method
Even better, make the changes in the TableAdapter first and they'll be reflected to the database:
Any changes that you make to the TableAdapter's main Fill method are reflected in the schema of the associated data table. For example, removing a column from the query in the main Fill method also removes the column from the associated data table. Additionally, removing the column from the main Fill method removes the column from any additional queries for that TableAdapter.

Update SQL Server database via a datagridview in C# on Visual Studio

When the user double click on a row on the datagridview, the relevant details appear in the textboxes.
User can edit the details and click on Update button to save the updates on the SQL database.
Note : CustomerID is an identity column in the customer table on the database
Please help with the C# code.
Capture of Program
I presume that you've just started to learn WinForms & C#. Firstly, I would recommend that at this stage, you should keep the datagrid view as just a view.
Second, it would be better if you start off by making a simple search button to query your SQL database and populate the textboxes accordingly. Then you can keep on building up your application as you get more sophisticated with how everything works.
Step-by-step dude, nobody is just going to paste an entire code here specifically for you, and even if somebody does, you'll learn nothing.

Saving history from a SQL database using C#

I don't think I need to post any code but if I need to I will.
I have created a Windows Form in Visual Studio that creates a record and saves it into a database. I have also added an update function so that I can change the record and save the new changes.
What I need to know though is how to create some kind of version history. i.e. When I click update, I want it to save the previous version so that I can look back at it. Like a bug tracker has a kind of version control but a lot simpler (hopefully).
Would I need a separate database? Could I have another datagridview that when a record is clicked on (or a button) it will show the history of changes for that record?
I've done many searches online and just can't find how to do it or where to look exactly to find this?
Any help would be greatly appreciated.
Thanks
you are talking about audit trail , take look at this :Link
I know this example for MVC but it's the same idea
you only need one table to track changes of the whole database by using json objects that represent "before" and "after"

Update DataSet in Crystal Reports

I have little to no experience in using Datasets and Crystal Reports so please don't bash me if the answer is really obvious, I didn't manage to find the answer on-line.
I have a C# Winforms application, containing a Crystal Report that takes its data from a DataSet. I populate the DataSet dynamically from inside the application.
In the past, if I added another column to the DataSet, I could right click the DataSet in the Crystal Report and click "Verify Database" to make the new columns available in the Report.
Now, if I do this the following window pops up:
There is no item available in the Class Name combobox, and if I check the checkbox, another combobox appears which is also empty.
Please note that I populate the DataSet from inside the program, so there is no connection.
How can I update the DataSet in the Crystal Reports, so that the new columns become available?
Thank you.
P.S.: I have tried the following solution but it didn't work: Update DataSet Schema On Crystal Reports for VS2008 because I am presented with the same window as above.
Also, this is what I see, might be useful:
Edit1: The fields appear in the Data Sources but do not appear in the Field Explorer.
I already have some Formula Fields that have the same name as the newly added Columns but removing them didn't help either.
I finally managed to solve it by myself. There are three things I did and I'm not quite sure which one did the trick since I can't seem to manage to recreate the problem.
I renamed the Formula Fields that had the same name as the DataTable columns I added.
I clicked the " ... " box and reselected the same file.
I added the Internal Connection ID {294de39c-3e3c-4748-9138-53d4be2a74a6} in the Class Name textbox.
The first two probably didn't help since they didn't work before. It did work when I added the string so that's probably the solution, but this didn't work the first time either.
Hopefully my wasted hours would save someone's time.
This is my solution. Here is the steps:
Create New DataSet and drag the table from SQL Server ObjectExplorer
Right click on Database Fields
Choose Datasource Location
Click on the table on new DataSet created on step 1
Click Update
Hope it can help

C# Winforms DataTableAdapter databindinding question

I have an app written in c#.
Records from the DB are shown through the auto-generated visual studio code (DataTableAdapter).
this._______tTableAdapter.Fill(this._______SQLDataSet._______);
Now, when I make changes to the DB, not through the DataTableAdaper - but through code on another winform, close it and open another where I have my DataTable filled with adapter, the changes are not shown until I restart my app.
I don't get it, I tryed closing the window using this.Close() and this.Dispose() to release all the resources, so the next time I open it, the code should rebind the new data from the database, but that's not the case...
What am I doing wrong?
Thanks very much for any anwser...
Have you tried explicitly calling DataGridView.Rows.Clear() when you are loading the form?
If that doesn't work, load the form in debug mode, and look at the status of the data every step of the way.
My suggestion/comment may not meet your conditions, but referring to this problem, notice, that you can also modify data in DataSet (not passing the calculations on the server witch every change). If all changes are made - you can call Update method on data adapter which provide data updates for all changed rows.
Anyway - if you want only refresh changes made in other instance - just refrech DataGridView or set DataSource again.

Categories

Resources