c# Binding ComboBoxList to SqlCe Database - c#

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.

Related

Are there ways to copy methods from one TableAdapter to another?

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!

How to add / edit / delete an element in both the database and the listbox properly using a BindingSource

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.

Update MVC database first to reflect db changes

i'm new to MVC and have to update my model.
I have an SQL Server database already connected to my project, today I add a new column. Now I need to update my MVC code to reflect the change.
Everything online tells me to update the .edmx and .tt files with the Update Model Wizard since they are auto generated, but I cannot find the wizard in VS 2012 so I am at a stand still.
Is this the right way I should be doing this? Any tips would be great...thanks
There are a couple options you can do:
If the database is empty or there isn't a lot of data that you care about, you can drop the database and recreate it.
Manually add the column in the database (after property is in model)
There is something called Code First Migrations. This will allow you to update your database. Here is a tutorial you can look at for this: http://msdn.microsoft.com/en-us/data/jj591621.aspx
Double click the edmx file. This opens the diagram of the database model.
Right click and select "Update Model from Database..."
That will start the wizard you're looking for.
Be aware that not all changes come through automatically.
Most obvious is that columns will not be removed from model just because they're gone from the db. If you're dealing with lots of modifications or deletes rather than additions, it may be best to just drop the model and recreate it (as suggested above by Andrew).
No matter which approach you take, I'd suggest making sure you have all other local source changes committed or shelved before starting because you may need to do an undo check out if EF gets freaked out.
I ended up downloading this update of VS 2012 and repaired. Now I am able to update my model.

.NET 4.0 EDMX Generate DB from model and then Execute SQL removes the DATA from Table

I'm newbie with .NET Entity Framework 4.0 I'm using VS2010 and I created the EDMX file and then added my Entities in this model. After I've finished my Datamodel and set the Asscociation, I right click on and choose Generate DB from model.
Then it creates SQL scripts *.sql then I open the SQL and right click on Execute SQL Statements... What happens is all my tables are re-created...
ISSUE: All my test data in MasterData Tables are removed/deleted. I need to add the data back again manually...
I googled on "keep data generated Data model EDMX" etc. but couldn't find... Can someone advice me how I can avoid this problem?
Because I've e..g 15 MasterDataTable + other tables linked with these and I need to re-add the records back manually for all these tables... and 9 of 10 case I don't touch those tables in UI.
please advice how I can avoid this situation.
thanks
You can update the model from the database (the reverse way you are taking now):
Double click the .edmx file to open the designer. In the middle of the designer, right click to show the contextual menu and click "update model from database". It will prompt a screen, check the DB parameters and click next. Now, you're in a sreen with TABS. The first tab is for adding tables as entities to the model. The second tab will its for update the model. Select the tables and views you need to add/update and click next. That's all.

Save changes in dataGridView

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

Categories

Resources