Entity Framework - Insert Update and Delete in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Please don't use database first and edmx files.

If you want to design in the database, use the wizard to regenerate code-first after making db changes.

๐Ÿ‘๏ธŽ︎ 6 ๐Ÿ‘ค๏ธŽ︎ u/throwaway_lunchtime ๐Ÿ“…๏ธŽ︎ Dec 07 2017 ๐Ÿ—ซ︎ replies

Windows Forms? Geezโ€ฆ I guess heโ€™s still gunning for Windows XP support with his products.

๐Ÿ‘๏ธŽ︎ 2 ๐Ÿ‘ค๏ธŽ︎ u/rekabis ๐Ÿ“…๏ธŽ︎ Dec 07 2017 ๐Ÿ—ซ︎ replies
Captions
cube demo of this video tutorial this is a step-by-step tutorial on how to implement crude operations insert update delete and select using entity framework DB first approach in in c-sharp windows form application in order to insert an your code you can do this now click on save so here we have inserted a new record for gemstones in order to update a record you can double click on that row then it will populate the selected customer details inside these text boxes now I can update the customer details like this James Jones - James Smith click on update so here we have updated the record for James Jones - James Smith in order to delete a record you can double click on the row then click on delete it will ask for confirmation are you sure to delete this record or not click on yes to delete the record so here we have deleted the record for Tony Abraham I am sure you will find this tutorial helpful to understand entity framework DB first approach crudo operations in c-sharp windows form application so please watch till the end of this video tutorial what's up YouTube welcome to dotnet move in this tutorial I will show you how to implement entity framework insert update and delete operation in c-sharp before starting this video tutorial I would like to ask you a favor if you found this video helpful please thumbs up this video if you are new here please subscribe to this channel dotnet Mobe we have already done a video on crude operations in c-sharp windows form application using old ad or dotnet method in this video we will use the entity framework method which is much faster and easier than the old ad or dotnet method using entity framework we will do operations like insert update delete and select inside this windows form application first and foremost we have to create a windows form application for that you can use the shortcut ctrl shift N or you can click on this new project here then I will select windows under wishes C sharp then select windows form application I will name this project as entity framework crude app then select the location where you want to save this application then click on OK so here we have created a brand new windows form application inside this form we have to add the required controls for this application so open toolbox here then under all windows forms we need a label then text box then we need buttons and finally we need a data grid view so here we have added required controls inside this windows form now we can align and arrange these controls accordingly first of all we need for lab textbox pair in order to copy this label textbox pair we can select these two controls then hold ctrl then drag the selection below so that we can make copy of this pair like this we need two more labelled textbox pair so I will select these two pairs then hold control drag them below so here we have in total 4 label textbox pair like that we need 3 buttons select the button then hold ctrl then drag them here ok so here we have 3 buttons now we can change properties of these controls first of all right click on this form then go to properties I want to change the form title here for that you want to change this text here I will set it as entity framework insert update and delete now we have the first level it is for first name first name and then below that we have last name then we have city finally we have address and we have to name these text boxes this is for first name right so I will name this as txt first name I use prefix txt for test boxes and we have last name txt last name txt city txt city and we have txt address txt address inside this application in to demonstrate these crude operations we will be using details of a kasama like first name last name city and address of a customer now we have three buttons first one is for say purpose so I will name it as BTN save and then we have one button for delete operation BTN delete and final button which is for reset or cancer purpose so I believe it as btncancel and we have to change the dash accordingly this is for cancel purpose and this is for delete purpose and here we have a button for save operation so here we have done with design in this windows form except this that I gave you that we can do later before that I want to increase the font size then font color than background color things like that so I will be back after that boo so here we have done with final touch-up first of all I have made this address text box as multi-line most often we may need more than one line for this address details and then I have named this data grid view as dgv customer then we have made some normal design modifications like background color foreground color than font size for this data grid view I have set background color as white then we have made some changes for default cell style inside that I have said this normal back color and selection back color like this then we have increased the row height inside this autumn blade here okay have set the height as 30 so these are the major design modifications made for this form now before continue with this form we have to create our secure server database for that here is my SQL Server management studio here we have connected to local SQL Server instance is clearly 2012 which is an Express Edition now we have to create a database for that you can right click on databases here then new database I will name this database as entity framework DB in short I will name it as EF BB click on ok so here is the newly created DB inside that we have to create a table so I click on tables then new tab now we can list details of columns inside this table so here we are going to deal with details of customer so we can start with customer ID customer ID it softly type in the jaw then we have forced me first name it's of the type marker 50 then we have last name it's of the type Bach 50 then we have city it's of the type work 50 finally we have a column for address it's of the type work 250 I will set the customer ID column as the primary key then I want to set the same column as identity specification for this table so select the column customer ID and expand this column properties inside that you can see the identity specification here set East identity as yes so it will start from 1 and increment by 1 upon new recording session these increment operation is done by SQL Server itself so that we don't want to insert values into this customer ID column in order to save this tab we can use the shortcut control s or you can click on this save I can here I will name this table as customer click on OK now back to visual studio now inside this windows form application we have to add the ADT framework model for this is pure server Tybalt customer for that right click on this project then add new item select data from the left panel then select ad or dotnet edit e data model I will name this model as DB models click on add select generate from database click on next now click on new connection here here we need to provide the SQL Server instance details for me it is local backward slash SQL e 2012 and then from this drop-down we have to select the corresponding database i will select entity framework DB e f DB click on ok after creating this entity model there will be a class with this name we have to create an object of this class in order to interact with SQL Server so I want to edit the class name here I will set it as DB entities now click on next if you have two or more entity framework versions you have to select one version I will select entity framework 6.0 click on next in this step we have to select the database objects that we need to add inside this entity model so I will select customer tab click on finish so here we have added ADR ordinate entity model for the tablet customer this is the diagrammatic representation of customer table here you can see the newly created entity data model inside that we have a customer class it has properties corresponding to these columns inside the customer tablet that we have just created apart from this customer class file here we have DB models dot context or CS file inside that you can see a cross with these name DB entities we need to create an object of this class in order to interact with SQL Server db2 the windows form here form one now right-click on it then click on view code so here we have the code behind c-sharp code behind this form design ok first therefore I will declare an object of this customer class here so I will create a customer class object customer I will name it as model is equal to new customer class ok first therefore I will implement cancel or reset operation inside the button click event btncancel so double click on it so here we are inside the click event of button Kancil inside that we have to clear these controls inside this form these text boxes okay for that I'm going to define another function void clear first of all I will clear these test boxes here in total we have four text boxes txt first name dot text is equal to txt last name dot text is equal to TX T C T dot txt is equal to txt address dot txt is empty string so here we have reset the text boxes now I want to set this Save button text as save it is already sale but we will change that during update or delete operation so during the reset operation we have to set back to save so BTN save dot txt is equal to save after that I will disable the delete button enabled is equal to force now we will be using this customer object model in order to store the currently active customer record details in C shop okay so I want to set the model customer ID as 0 so here we have done with clear or reset operation inside this clear operation we have disabled delete button we will reenact this delete button during update or delete operation now just call this clear function inside the cancel button click event I want to call the same function inside the form load event for that I click on form here and go to properties then go to events look for load event double-click on it inside that we can call the same function clear next we are going to imply insert operation using entity framework in SQL server DB for that I will use this Save button click event so double click on this button in order to create the click event now inside this a button click when we have to insert a new customer record with given details from these stash boxes so let's look how we can do that so I will start with using statement here using as a resource I am going to use an object of DB entities DB entities as DB is equal to u DB entities before this using statement we have to retrieve the customer details from these text boxes using this customer object model okay first of all we have first name model dot first name is equal to txt first name dot text dot trim trim function is used to remove whitespace from left and right side so like this we have three more text boxes so I will copy this line and passing below that three times so this one is for last name okay txt last name then we have city txt city and finally we have address we have to retrieve the address details from this test box txt address as you can see here here we don't set because some iid property it is already said to zero during the form load even here now inside this using statement we can insert new customer record using discuss some object model for that we can do this DB dot customers dot add inside this function we have to pass this object model okay model and finally we have to call this function DB dot Save Changes after insert or update operation we have to clear these form controls so I will call this clear function here clear after all we have to show a message box showing submitted successfully so I will do this message box dot show inside that we need to show this message submitted successfully now let me run this application now I am going to insert a new custom or a code here now click on save so here you can see the success message submitted successfully click on OK now let me check our DB here right click on the tabble customer then click on select top thousand rows so here we have the newly added a record for Fiona clean now back to visual studio now inside this data grid view I want to list or bind customer of course that we have already inserted into this customer table for that we have to add data grid view columns corresponding to these SQL Server table columns so click on this right arrow here then edit columns click on add first of all I will add a column for customer ID column name will be customer ID then header text will be customer ID ok normally ID columns will not be visible in that I get views I will uncheck this visible checkbox click on add now we have first-name will set the name as first-name and I will set the header text as first space name now check this visible click on add now we have last name last name header text will be last space name click on add and we have city header text will be CD we can't add now close this window normally address column will have more text in it so we skipped at this column we don't want to add all columns from SQL Server table into that I get view you only need to add columns which are required to identify a single customer now we have to set this data-property-name it should be match it with this SQL server table so here we have customer ID first of all customer ID after that we have first-name sorry first name then last name then city now click on ok now you want to change few more properties from this data accurate view so right click on it then go to properties here we need to reset this property allow user to delete rows as false then I want to make this that a grid view as read-only so here you can see the property read-only set it as true now I will bind this data grid view using entity framework for that I will create a new function here void populate data grid view I will start with using statement as a resource I will create an object of DB entities class DB entities as DB is equal to MU DB entities now in order to bind the data grid view using entity framework you can do this dgv customers dot data source data source is equal to B B dot customers here you can see that these customers properties of the tab DB set of kasama i want to convert that into customer list for that we can do this to list want to convert that into customer list so we will do this customer okay here we have done with populating that a grid view now we can call this function inside the form load even here so I will call the same function before this success message now let me run this application here we have done with populating this data grid view as you can see here here we have an extra column which is not added into this data grid we Cola in order to prevent auto generated columns like this we can do this inside the populate data grid View function dgv customers dot Auto generated column e forced okay now let me run this application again so here we don't have the extra column address okay now in order to fill this free extra space here back to the form design then click on this arrow here edit columns then select first name I will set auto size mode as filled so that this first name column will take the extra free space here next we are going to implement update and delete operation for that we have to select a custom or a code from this data grid view for that you can double click on this bad a grid view row and we will show the corresponding customer details inside these test boxes here then we will update the Save button text as update and then we will enable this delete button here so let's look how we can do that first of all back to the visual studio here then we have to implement the double click event for this that I grid view so right click on it then go to properties then go to events then look for double click even here so here it is never click on it first of all we will make sure that the double click row is a data row because the same even double click event will be triggered even if you double click on this column headers here so first of all we want to make sure that we have double click on a data row for that we can do this dgv customer dot Karen row sorry current row dot index is not equal to -1 inside that we can fill the click draw details inside these text boxes okay first and foremost we have to retrieve the selector draw customer ID for that we can do this model dot customer ID is equal to we have to convert the value into integer convert to in 32 sorry in the in order to rotate the ID column value or customer ID column value from this data grid view before this first name column we have added a customer ID column which is already hidden okay and we have named that drawer as customer ID so using this customer ID column we can retrieve this selector draw ID for that we can do this DTV customer dot current drawer dog says inside that we can pass the column name I will pass customer ID here dot value using this customer ID value we will select the customer record from SQL Server tablet customer for that we can do this I will start with using statement as a resource I will create an object of DB entities class as DB is equal to new DB entities inside that we can do this model is equal to BB . customers dot were X X such that X dot X dot customer ID if equal to is equal to this customer ID here that we have retrieved from the data grid view column model dot customer ID dot first or default now we can fill the corresponding customer details inside these text boxes here so first of all we have txt first name dot txt is equal to model dot first name like this we have three more text boxes so let me copy these and pasting below that three times after first name we have last name txt last name is equal to model dot last name then we have txt C T is equal to model door see T then we have final text box txt address is equal to model dot address and finally we have to set the Save button text as update BTN save dot text is equal to update after that I will enable the delete button BTN delete dot enabled is true now let's check how it works double click on this row so here we have the details of Fiona green here we have updated this same button text to update and we have reenable this delete operation after changing the details of this customer user can click on this Update button in order to update the customer details you have to add few more changes inside this a button click even in order to do the update operation so let's look how we can do that so we need to add an if cross if model dot customer ID is equal to zero then we will do the insert operation here else we will do the update operation in any different work in order to update our code we can do this debby dot angry inside that we can pass this object model model dot state is equal to nd T State for that we have to import this namespace system dot dot dot entity dot modified after all these insert or update operation we will call this say changes function ok so here we have done with insert and update operation now let me run this application double click on this row I want to change the city from London to Canada then I will change the last name as green too now click on update so here we have the success message submitted successfully here you can see the update customer record in two in ncte sub data to Canada after all we have reset this form controls here now let's look how we can implement delete operation here for that I will use this delete button here double click on it in order to implement the click event of delete button first therefore we will confirm the delete operation from user side using message box because delete is a loss of data so we want to confirm the operation message box door show we will ask are you sure to delete this record as a second parameter we have to provide the message box title I will set it as entity framework crude operation as a third parameter we can pass the message box buttons message box buttons dot yes/no if you sir click on yes it will return a dialog result yes dialog result yes in that case we can go ahead with delete operations okay so I will start with using statement as a resource of use an object of DB entities as DB is equal to new DB entities we will do this delete operation after that accurate view double-click so when we double click on arrow corresponding row details will be saved inside this ksama object model so we can use this same object here also so first of all I will create an object VAR entry is equal to B B dot entry inside there we can pass this model here model if entry dot state is equal to end it is state dot detached if it is detached we have to attach this model object in to the customer list so we can do this debbie dot customers dot attach side that we can pass this object model after all we can do this maybe dot customers dot remove and say that we have to pass the same object model finally call this Save Changes method V B dot C changes after delete operation we have to refresh the data grid view so I will call this populate that I give you function again after that we'll call the clear function in order to clear the form controls finally I will show the message box showing deleted successfully message box dot show inside that we can pass this deleted successfully now let me run this application let me add one more record here so here we have had a new record for Tony Abraham now I want to delete this quote Fiona green - so double click on it so here we have populated the corresponding customer details here now in order to delete this report click on delete now we have the message box are you sure to delete this record click on yes to delete so here we have deleted zero code for Fiona agree if you found this video helpful please thumbs up this video and for more awesome videos like this please be subscribed to this channel dotnet mob that's it guys in this tutorial we have discussed step by step implementation of editing framework root operations insert update and delete using database first approach we can download the project source code and DB script from the link given below in video description please like and share this video with your friends and colleagues so that they can benefit from this have a nice day bye
Info
Channel: CodAffection
Views: 195,766
Rating: 4.9105144 out of 5
Keywords: entity framework insert update delete in C#, entity framework - insert update delete and select in windows application, entity framework CRUD using DBatabase First Approach, C# EF - Insert Update and Delete, Using DB First, ADO.NET Entity Model in Windows Application, Bind Datagrideview using entity framework, entity framework database first example, Entity Framework - Insert Update and Delete in SQL Server, C# Entity Framework 6 - Database First, CodAffection
Id: hg3H_pAzoPI
Channel Id: undefined
Length: 32min 7sec (1927 seconds)
Published: Thu Dec 07 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.