How do we Insert,Delete,Update from Kentico webproject by using a webpart?
In Kentico CMS we have a database with number of tables to support the CMS system, still some times we want to add a custom table for some requirement.So we need to add/edit/update that table though the KenticoCMS. To do the CRUD operation on to the custom table Kentico provides some classes that can be found under DataEngine DLL.
Suppose we have a custom Table as TestTable with fields as “FistName”, “LastName”.Now we want to do the CRUD operation on to this table through a webpart component.Then Create webpart using this following steps:
Create a folder called DatabaseRetrive Webpart unber KenticoCms/CMSWebparts.ext
Then add a web user control item with in that folder named as DataRetrive.ascx.
Add a two text box and a button for for entering the firstname and lastname into the TestTable.
On code behind on that button click method add the following code.
Add the following name spaces to the ascx.cs page:-
This DataEngine namespace contains class SimpleDataClass.
SimpleDataClass also contains so many methods like SetValue() and GetValue for the DataLayer operation.
Code For Insert:
SimpleDataClass userObj = new SimpleDataClass("custom.TestTable ");
string strName = idName.Text;
SimpleDataClass - Gets a class name. Use it to create a new item and insert it in the database.
custom.TestTable - This is class name of that corresponding database table to which we want to insert.
Similarly define two other function for the update and deletion.
Code For Delete:
SimpleDataClass userObj = new SimpleDataClass("custom.TestTable ", 6);
Here we specify the id of the row to which we want to delete.
Code For Update:
SimpleDataClass userObj = new SimpleDataClass("custom.RajashreeDocument", 3);
string userName = (string)userObj.GetValue("Name");
userObj.SetValue("FirstName", "John Smith Jr.");
Here we specify the id of the row to which we want to update.
Now add this webpart to a page go for the following steps:
Go to the Site Manager > Developement > Webparts,Add a New category named DataRetriveWebPart.
Under that add a new wepart named as TestWebpart with path of the usercotrol we are created.
That is :~\CMSWebParts\DatabaseRetrive\Dataretireve.ascx .
Go to the CMS Desk and add a new simple page.After that go the design view click on the (+) symbol
to add new web part.Chose the above web part ie.under DataRetriveWebPart.Now click on the live wesite insert the First name and last name etc.