Are there ways to copy methods from one TableAdapter to another? - c#

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!

Related

Failed to update database with SqlCommand and also with DB Entity in C#

I have made a program with a simple SQL Server .mdf database file.
First: I have connected to the database using SqlConnection and tried to insert some data using SqlCommand. The ExecuteNonQuery returns the number of affected rows successfully for example 1 or 2 ... etc.
But when I use Server Explorer to view the data of the table, there is nothing inserted!?
Then I have used model entity object and DataSource. On SaveChanges function I get also the number of affected rows successfully, but there is no data inserted in Table on viewing it.
When I make a query using Server Explorer, the data will be inserted successfully and I can view it!!? Can you please help me to solve this problem?
thank you marc_s, you gave me a hint "ConnectionString". As I created a database mdf with server explorer, I realized now, there is two mdf files created. One is located in base folder of project and the other located in bin/debug folder. I don't why there is two of them, howerver it solved my problem. Thanks

TableAdapter fields not in right order

I have two TableAdapters in a project. Both are used to create a csv file that is sent to an application to create accounts. We recently moved a server so the connection string of one of the TableAdapters had to be changed. Now the fields in that one will not show up in the right order on the Dataset Designer page.
Thus it is sending the csv file to the other application with the fields in the wrong order. Is there any way to manually force those fields to show up in the correct order? The underlying SQL query does select them in the right order but they will not show up in the right order after hitting finish.
Thanks in advance for any help.

c# Binding ComboBoxList to SqlCe Database

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.

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

visual studio DataSet designer with stored-procedures ? what is this icon

What does this icon mean is Visual studio, visual Data Set designer.
I have several stored procedures.
can you help me understand,
why the one on top of the list has a
small "check mark"
why I can't delete it if I need to.
This is not the case with the rest.
Why is this "special" ?
Thanks
Because that item contains the method(s) that define the schema of the table the TableAdapter is part of.
The default names are Fill and GetData. Looks like you renamed them.
Each table has a default query (The one on top with the check on it). When you dragged your tables in to the dataset to create the query, it wrote a SQL statement which it uses to schema your table. Keep that query simple, you might not actually use it in code, and you can always edit that query to update the table schema. In the picture you provided, your default query is actually a stored procedure.
Every time you open the default query it connects to your datasource and allows you to select new columns that weren't in there before. If you want to update your existing columns, delete all the columns out of the table before you attempt to open the query. When you save the query, your updated columns get added back.
Make sure your connection string has permissions to view column information.

Categories

Resources