Insert values to custom class:

sarathlal s asked on April 23, 2014 02:01

Hi,

I created a custom document named test with custom.test table and Name,Age, Status fields.

After that i created a custom webpart for inserting values to custom.test. But values are not inserting to the table. Here i am giving my entire source code. Please give a solution.

Do i need to add connection strings for this??

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CMS.DataEngine; using CMS.PortalControls; public partial class CMSWebParts_MyWebpart_Test : CMSAbstractWebPart { protected void Page_Load(object sender, EventArgs e) {

}
protected void Button1_Click(object sender, EventArgs e)
{
    string strName = TextBox1.Text;
    string age = TextBox2.Text;
    Boolean stat = false;

    IDataClass userObj = DataClassFactory.NewDataClass("custom.test");
    userObj.SetValue("Name", strName);
    userObj.SetValue("Age", age);
    userObj.SetValue("Status", stat);
    userObj.Insert();
}

}

Recent Answers


sarathlal s answered on April 23, 2014 02:01

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CMS.DataEngine; using CMS.PortalControls; public partial class CMSWebParts_MyWebpart_Test : CMSAbstractWebPart { protected void Page_Load(object sender, EventArgs e) {

}
protected void Button1_Click(object sender, EventArgs e)
{
    string strName = TextBox1.Text;
    string age = TextBox2.Text;
    Boolean stat = false;

    IDataClass userObj = DataClassFactory.NewDataClass("custom.test");
    userObj.SetValue("Name", strName);
    userObj.SetValue("Age", age);
    userObj.SetValue("Status", stat);
    userObj.Insert();
}

}

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on April 23, 2014 06:45 (last edited on April 23, 2014 06:45)

Document types are used in the content tree so you'd need to follow the structure to find the tree node you're in and such. Here is an example right out of the API Examples for v7: `

// Create an instance of the Tree provider first
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Get the parent node - the API Example folder
TreeNode parentNode = tree.SelectSingleNode(SiteContext.CurrentSiteName, "/API-Example", "en-us");

if (parentNode != null)
{
    // Create a new instance of the Tree node
    TreeNode newNode = TreeNode.New("CMS.MenuItem", tree);

    // Set the document's properties
    newNode.DocumentName = "My new document";
    newNode.DocumentCulture = "en-us";

    // Insert the document into the content tree
    newNode.Insert(parentNode);
}`
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.