I have C# Window Forms project and MySQL ODBC connector to localhost server with all privileges.
I also succeeded to fill dataGridView with data, but the changes I make are not stored back to the database. How do I create save button?
One easy, as in automagical, way to do it is to make a dataset: Menu>Data>Add New Data Source
When the wizard shows, choose Database, DataSet and choose new connection to connect to the database.
When you have the dataset in DataSource window move the dataset over a empty form and VS will create a number of buttons for you including a Save button.
When you have done that you can look at the code to see how it works and adapt that to your own need.
Good luck
edit
based on the comment you made to may answer I can guess that the database adapter you are using doesn't allow the generation of methods that talks directly to the database, hence the errrors dependency on the database connector. That is at least my understanding of this MSDN page
(See headline TableAdapter GenerateDbDirectMethods)
To resolve this you have to set the GenerateDbDirectMethods to false in TableAdapter and only use the InsertCommand, UpdateCommand, and DeleteCommand
Related
I've created a DataSet related with SQL server database. This DataSet has a set of tables with TableAdapters where I've written methods to access database data. Later I've moved my database to another server, changed a connection string and tried to update the DataSet. But the Wizard just adds copies of the tables with empty TableAdapters to the DataSet.
So now I want to copy the methods of original TableAdapters to the new ones and to delete original tables after. Can I do it? Or maybe someone show me another solution.
With many tries I've got the solution! It's pretty easy:
Open your DataSet.
Right-click on first table.
Click "Configure...".
Click "Done".
Repeat steps 3-4 with all of your tables.
If you got no errors your table is updated now!
I downloaded source code of an ASP.NET MVC book and it looks this, I can see the table but I need to enter some data in it so it can show them to me
Where should I go to enter data?
While you should actually do this in SQL Server Management Studio, Visual Studio does offer you some support to deal with data. To do so, Open Server Explorer (Ctrl+Alt+S) in VS. Create a data connection, if it doesnt exist already to the DB in question by right clicking on Data Connection and click on Add Connection. Since you downloaded the source code for exercises, there is a good chance that they already created one for you. Once you have a connection to your desired database, you will be able to see database object under that including tables. To add data to a table, right click on that table and click on Show Table Data. You can then go to individual cells and enter data. Hitting enter should submit the data change to SQL Server.
Hope this helps..
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.
I'm currently coding a WinForm application with a MS SQL database in background. I made a ListBox diplaying all the rows of a table from my database using a BindingSource with Linq-To-SQL and I do not understand how I can delete a record from the table and automatically update the ListBox. Is it not the interest of the BindingSource, to report changes to the data ?
I'm just looking for advice, not a ready-made solution ;-)
Thanks you.
There is no live connection between the database and the application. The BindingSource is a link between the in-memory data cache and the UI. It knows nothing about the database. LINQ to SQL works by pulling data into the application from the database. The application has to initiate the request. The database doesn't push changes to the application because it has no idea what applications there are that may be using that data. There might be a single instance of a single application or hundreds of users of multiple applications.
This is more of an understanding / clarification question than a technical one. I am currently trying to bind some data from a database to a ComboBoxList.
The datatabase is an SqlCe type. It's called voodoobase.sdf. It is listed under Server Explorer along with all its tables.
Q1) What is the correct procedure to bind a dropdown ComboBoxList to the data in the database?
Do you have to go through Data Sources?
What is a Model container?
I am used to PHP/MySQL where you just connect and do it all yourself. If you have any good clear links or are able to answer this in a human readable way I would be grateful.
I am close to figuring this all out, but I think just I need to put it all together.
Q2) The databse was designed and comes in Solution Explorer as a DBModel.edmx. If I make changes here and "Generate Database From Model" It seems to update the Database (which is good) but Data Sources do not update with it ? <-- is this a correct feature?
I know this is wordy question but if you can shed some clarity on this would make me happy =))
Q1:
This is a fairly roundabout way maybe, but this is how i do it for a combobox:
Create a dataset for the database (when i added my sqlce db, i chose to add a dataset during the wizard that automatically popped up)
Open your dataset by doubleclicking on it
From your "Server Explorer", create your tables & edit your data as needed, then drag & drop your table/s to the dataset designer
Open your windows form's design view and from your "Data Sources" (which should have your dataset in it), drag & drop some fields that you'd want to display for this table on to your form (e.g. name, surname, age, gender, etc). Visual Studio will then auto-add the following objects (visible on the bottom of your design view): dataset, bindingsource, table adapter and a table adapter manager.
Now add your Combobox that you want to bind to this table (e.g "cboName")
In code-behind in the Form_Load event, bind it as follows:
cboName.DataSource = myTableBindingSource;
cboName.DisplayMember = "Name"; //will bind to the "Name" column
That should be it. Now when the app runs you'll be able to select the people by name from the combobox and their details will show accordingly in the other controls you dropped on the form. I'm still very new to this, so there might be better ways. But this works for me so far :). Unfortunately i'm not sure what a model container is.
Q2:
I'm not sure about edmx database models. My database is an sdf file, my dataset is an xsd file and i edit my database through the Server Explorer.
...However VERY important to know is that your "designed" database is not the db your app is using in runtime. Go check your bin folder - in there you'll find the db your app runs with. (With SQLCE the database in design is seperated from the db used and you have to manually update (copy-paste overwrite) your db in the bin folder after changes made in your design or data).
Take note that your "data sources" is like a map to your database. You'll have to update (drag-dropping your table again)... or recreate, etc. the data source (e.g. your dataset) if your db changes doesn't reflect on it.