Kentico CMS 7.0 Developer's Guide

Deleting a record

Deleting a record

Previous topic Next topic Mail us feedback on this topic!  

Deleting a record

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

This code example shows how a form record can be deleted.

 

[C#]

 

using System.Data;
 
using CMS.FormEngine;

using CMS.SettingsProvider;

using CMS.DataEngine;

using CMS.GlobalHelper;

 

...

 

       string bizFormName = "ContactUs";

       string siteName = "CorporateSite";

 

      // Get Form definition

      BizFormInfo bfi = BizFormInfoProvider.GetBizFormInfo(bizFormName, siteName);

 

      if (bfi != null)

       {

          // Get data type definition

          DataClassInfo dci = DataClassInfoProvider.GetDataClass(bfi.FormClassID);

          if (dci != null)

           {

              // Get all Form data

              GeneralConnection genConn = ConnectionHelper.GetConnection();

              DataSet ds = genConn.ExecuteQuery(dci.ClassName + ".selectall", null, null, null);

 

              if (!DataHelper.DataSourceIsEmpty(ds))

               {

                // Get ID of the first record

                int formRecordID = ValidationHelper.GetInteger(ds.Tables[0].Rows[0][0], 0);

 

                BizFormItemProvider bProvider = new BizFormItemProvider();

                // Get the record with specified ID

                BizFormItem formRecord = bProvider.GetItem(formRecordID, dci.ClassName);

 

                if (formRecord != null)

                 {

                    // Delete the first record

                     formRecord.Delete();

 

                    // Update number of entries in BizFormInfo

                    BizFormInfoProvider.RefreshDataCount(bfi.FormName, bfi.FormSiteID);

 

                     lblInfo.Text = "The record was deleted successfully.";

                 }

               }

              else

               {

                   lblInfo.Text = "No data found.";

               }

           }

       }