Inserting data into C# Datasets - c#

I am using C#.Net application in which i created a dataset.Now i want to create a method in which i will enter a record in one table which will return a value ie primary key.Now by using that primary key i have to insert records in 5 tables and i have to use that primary key as a foreign key for this 5 tables using dataset.

You need to tell your parent table's table-adapter to refresh the
data-table after update operation.
This is how you can do that.
Open the properties of ParentTableAdapter -> Default Select Query -> Advnaced options. and Check the option of Refresh the data table. Save the adapter now. Now when you call update on table-adapter, the data-table will be updated [refreshed] after the update operation and will reflect the latest values from database table. if the primary-key or any coloumn is set to auto-increment, the data-table will have those latest value post recent update.
Now you can Call the update as ParentTableAdapterObj.Update(ds.dataTable);
Read latest values from the ParentDataTable(ds.dataTable) coloumns and assign respective values into the child table before update. This will work exactly the way you want.
alt text http://ruchitsurati.net/files/tds1.png

Have you tried temporary key linkage until the DB generates the final key? Works great for me.

Related

Primary key auto increment

Problems: Previously i called the function below.
ArchiveDeletedPatient.ID = 18
_archiveDB2.patient.Add(ArchiveDeletedPatient);
_archiveDB2.SaveChanges();
Afterward I deleted all records from patient database(delete from patient), now in my database it is empty. I called the above function again, however this time it auto increment ID to 19 and inserted into the database. I spamed the above functions and it kept incrementing. How to remove such function?? I don't need the auto increment.
Some might say it is because of database setting. I tried manually insert using Microsoft SQL Server Management Studio 17 "INSERT INTO PATIENT TABLE VALUES(18, ...). It allowed me to insert and when there is such ID in the table. It will not auto increment and will just prompt me a error. How can i remove such auto incrementing feature in c# linq.
Auto increment feature is not of linq c#.
See the table properties and can you edit the pk to remove "identity" for remove autoincrement o add "identity".
If identity is activated in bbdd, the id is calculated in database and linq return this into your entity.
The auto incrementing is not the feature of Linq. It's the feature of Database.
Here are complete instructions to remove auto increment from MS SQLServer:
Open your Database.
Right click on your Table and select Design.
Select your specific column on which you want to remove auto incrementing.
In the column properties window click on Identity specification field and make this unchecked.
Save table design.
Here it is...
Note:
If you're using Entity Framework and specifying your field with [DatabaseGenerated(DatabaseGeneratedOption.Identity)] remove this annotation
or
replace it with [DatabaseGenerated(DatabaseGeneratedOption.None)].

Update master/child rows

I have tables Cases and Degrees with relation Cases.CID - CDegrees.CID. CID is Primary Key of Cases with AutoIncrement.
Both tables are used in one form and it's assumed that user can add new records to both tables same time and than save whole master/child data in one GUI action.
So in Dataset where I created FK I set "Both Relation and Foreign Key Constraint" to ensure during update of new record in Cases table, retrieved IDENTITY value will cause child records to update from -1 to retrieved CID value.
When I update Cases adapter, it causes to retrieve an new IDENTITY value and cascade update in CDegrees child record works ok too. But CDegrees's update causes Insert script with [CID]=-1 (original value). I changed Insert parameter #CID of CDegrees to "Proposed" version but same happens (seen in SQL Profiler).
Actually my task is mach more complicated, I just simplified task to localize my problem.
To describe more clearly.
before update
Both [Cases] and [CDegrees] have one new records with [CID]=-1
after [Cases] update both [Cases] and [CDegrees] have new CID identity value, just [CDegrees].[CID] "Current" value is -1 and "Proposed" is retrieved identity.
But when I call [CDegrees]'s dataset update, it sends Insert Command to SQL with [CID]=-1 regardless that I specified #CID parameter source as proposed value of [CID].
That is strange and funny. It appeared that calendar control on detailsform somehow causes but preventing details data cascade update according master identity value of CID field.
I just changed date field bound control from MonthCalendar to DateTimePicker and it worked.

How do you setup table structure for auto increment primary key with revision?

C# interface accessing Sql server db.
I have a table with a composite key ID and revision.
When I "insert" a new record I want to auto increment the ID.
When I "update" the record I want to actually insert a new record but increment revision.
What is the proper table settings for this?
What I have done in the past is actually use a trigger for UPDATE. It would copy all the values into a new record and increment the version column. I would also have an indexed column that would serve as a key, but not a primary key so that all revisions would be tied together.

Updating relations in Linq To SQL is possible?

I need some functionality in my project and I don't know if its possible.
Here is a pic with the relations:
I need to update the keys relation table LessonByFacultyMember and the same keys in Scheduling table.
I mean the keys LessonNumber,LessonCoursenumber,FacultyMemberId (LessonByFacultyMember table)
and LessonNumber,CourseNumber,FacultyMemberId (Scheduling Table).
Is it possible to update this kind of relations?
UPDATE:
I just want to be clear that i mean the possibility to change the VALUE that stored in the keys dynamically in some method.
Yes you can do so by selecting Update Cascade option in Foreign key in the database.This options automatically updates the key values in the other tables. But in your case this is not needed. The table LessonByFacultyMember should have a column LessonByFacultyMemberId as a primary key and that should be in the Scheduling table as a Reference instead of putting all the three columns in the Scheduling table. If you do so ,you don't need to worry about the updating LessonNumber,CourseNumber,FacultyMemberId in the Scheduling Table. Also in your Scheduling table there should be a column SchedulingID as a Primary Key. You can take LessonByFacultyMemberId ,SchedulingID as an auto incremented integer. Also there is no need to make LessonNumber,CourseNumber,FacultyMemberId as a Primary key in the LessonByFacultyMember table. Instead you need to make them as unique key. Similarly in Scheduling table make the current primary key as unique key and have SchedulingId as primary key. In case of showing records you need to make select statement using joins and it is better to create a view for such statement. In case if still it is not clear , create a sqlFiddle on http://sqlfiddle.com/ for your schema and share that in your question or comment to this answer. I will update the same.

C# DataTable and PK

I'm having such a problem: got dataset with a table(s). Say, we have already several records in a table which has a primary key (autoincrement). Basically, program works fine until I insert a new row. Even I use method AcceptChanges or Update, the new row is commited but it existing datatable object lacks the newest primary key value which I need for furhter processing. Is the only method to reload all the table from database again or there is a better way for it?
Thanks,
Ray
You should add ';select SCOPE_IDENTITY()' to the end of your insert sql statement attached to your data adapter. That will read back the last inserted value generated by autoincrement and update your dataset.
I.e, your insert sql should resemble this:
insert into sometable(column1,column2) values(#column1,#column2); select * from sometable where primarykeycolumn=scope_identity()
Your existing dataset will then contain the primary keys when dataadaper.Update(dataset) returns.
If you have a connection object you could use
int = (int)conn.executeScalar("SELECT ##IDENTITY")
Modified by what datatype your primary key is.
This has to be done directly before any other operations as a new insert will replace the stored value in ##IDENTIY

Categories

Resources