Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Insert into Custom table View modes: 
User avatar
Member
Member
hitesh - 4/26/2013 6:16:38 AM
   
Insert into Custom table
Hi everyone,
I have a table TaskComment. I want on click of button "Post Comment" the comment is inserted int table with taskid.
Please help me how to add functionality for inserting values in database.

Thanks

User avatar
Kentico Support
Kentico Support
kentico_filipl - 4/26/2013 9:36:03 AM
   
RE:Insert into Custom table
Hello,

If I understand what you are trying to accomplish correctly, do you want a form on a page where the user (user or editor - please specify) enters some values to the fields, clicks on the Post Comment button and these values are then added to the custom table TaskComment? Can you confirm that?

If that is not the case, try to describe the functionality you need in further details, please.

Best regards,
Filip Ligac

User avatar
Member
Member
matt-awg - 4/26/2013 12:43:47 PM
   
RE:Insert into Custom table
If you are just asking for a way to insert row in a custom table.... here is a generic function I wrote that you can reuse for any table. you pass it a dictionary of field name/values to insert so it is totally flexible.

    public static bool AddNewCustomTableItem(CMS.SiteProvider.UserInfo uiObj, string customTableClassName, Dictionary<string, Object> setNameAndValues, ref string statMsg)
{
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(uiObj);

// Checks if Custom table exists
DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);
if (customTable != null)
{
// Creates new custom table item
CustomTableItem newCustomTableItem = CustomTableItem.New(customTableClassName, customTableProvider);

// Set new values based on looping through dictionary object of fields and values to update
foreach (KeyValuePair<String, Object> pair in setNameAndValues)
{
string fldName = pair.Key;
if (fldName.EndsWith("~NULL"))
{
//ending in "~NULL" is a code we are using to indicate a field should be null not empty string, which requires different SetValue call
newCustomTableItem.SetValue(fldName.Replace("~NULL", ""), pair.Value, false);
}
else
{
newCustomTableItem.SetValue(fldName, pair.Value);
}
}

// Inserts the custom table item into database
try
{
newCustomTableItem.Insert();
statMsg += "Success<br />";
return true;
}
catch (Exception ex)
{
statMsg += " Error was: " + ex.Message + ".<br />";
}
}
else
{
statMsg += " This custom table does not exist.<br />";
}

return false;
}

User avatar
Member
Member
hitesh - 4/29/2013 8:13:24 AM
   
RE:Insert into Custom table
Hi matt,
The message is displayed "this custom table doesn't exist." The table is in database its "PM_TaskComment". What could be the problem.

Thanks.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 4/30/2013 7:44:43 AM
   
RE:Insert into Custom table
matt-awg wrote: If you are just asking for a way to insert row in a custom table.... here is a generic function I wrote that you can reuse for any table. you pass it a dictionary of field name/values to insert so it is totally flexible.

    public static bool AddNewCustomTableItem(CMS.SiteProvider.UserInfo uiObj, string customTableClassName, Dictionary<string, Object> setNameAndValues, ref string statMsg)
{
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(uiObj);

// Checks if Custom table exists
DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);
if (customTable != null)
{
// Creates new custom table item
CustomTableItem newCustomTableItem = CustomTableItem.New(customTableClassName, customTableProvider);

// Set new values based on looping through dictionary object of fields and values to update
foreach (KeyValuePair<String, Object> pair in setNameAndValues)
{
string fldName = pair.Key;
if (fldName.EndsWith("~NULL"))
{
//ending in "~NULL" is a code we are using to indicate a field should be null not empty string, which requires different SetValue call
newCustomTableItem.SetValue(fldName.Replace("~NULL", ""), pair.Value, false);
}
else
{
newCustomTableItem.SetValue(fldName, pair.Value);
}
}

// Inserts the custom table item into database
try
{
newCustomTableItem.Insert();
statMsg += "Success<br />";
return true;
}
catch (Exception ex)
{
statMsg += " Error was: " + ex.Message + ".<br />";
}
}
else
{
statMsg += " This custom table does not exist.<br />";
}

return false;
}

I'm using something similar but actually getting the class definition and comparing it with the data class info object being passed in. Also in my instance, I'm using a generic UserInfo object (ui) to insert into the custom table.
public static bool CreateCustomTableItem(DataClassInfo Dci, DataRow Row)
{
bool okInsert = false;

// Creates new Custom table item provider
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(ui);

if (Dci == null)
{
throw new Exception("Data class info cannot be null.");
}
else
{
// Creates new custom table item object
CustomTableItem newCustomTableItem = CustomTableItem.New(Dci.ClassName, customTableProvider);

List<string> classProperties = new List<string>();
// get the class definition
var doc = XDocument.Parse(Dci.ClassFormDefinition);
// get a list of all the tables properties by column value
List<string> elements = doc.Descendants("field").Select(el => el.Attribute("column").Value).ToList();

// got the class properties now compare
foreach (string s in elements)
{
// see if the class info contains the column in the data row passed in this method
if (Row.Table.Columns.Contains(s))
{
// contains the column so set a value
newCustomTableItem.SetValue(s, Row);
okInsert = true;
}
}

if (okInsert)
{
// Inserts the custom table item into database
newCustomTableItem.Insert();
}
}
return okInsert;
}

User avatar
Member
Member
hitesh - 4/27/2013 12:10:37 AM
   
RE:Insert into Custom table
Hi Filip,

Actually the functionality is for posting a comment on the task assigned to user(s). So creator of task and the task participant(s) can post comment. I have created a table in database (TaskComment) similar to BlogComment.

But the problem is how to insert values i.e how to use executequery. I'm a beginner using KenticoCMS to develop a portal project for my graduation.

Thanks

User avatar
Member
Member
hitesh - 4/27/2013 12:12:40 AM
   
RE:Insert into Custom table
Hi again,

Right now I'm trying to use the function which Matt has suggested. I hope that will solve my problem

User avatar
Member
Member
kentico_sandroj - 4/27/2013 1:47:35 PM
   
RE:Insert into Custom table
Hi,

Upon a brief review of the code, it seems that the solution should work but I have not tested it. Please let us know if you have any issues with this.

Thanks,
Sandro

User avatar
Member
Member
hitesh - 4/30/2013 2:09:55 AM
   
RE:Insert into Custom table
Hi Sandro,
I tried Matt's code but I think I'm missing something, I don't know what. I had created a table in the database using create query in MSSql2005. I passed table name "PM_TaskComment" for the parameter "string customTableClassName" but the code below ( as provided by Matt) return null
DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);

Please help me what could be the problem.
Thanks
Hitesh

User avatar
Certified Developer 8
Certified Developer 8
Petr Dvorak - 4/30/2013 5:33:26 AM
   
RE:Insert into Custom table
For your custom data Kentico uses "Custom tables" module -- http://devnet.kentico.com/docs/devguide/index.html?custom_tables_module_overview.htm. Matt's code is for this module.

The GetDataClass method expects internal custom table codename, not the real database table name. You need to create your own database table "PM_TaskComment" through Kentico CMS Site Manager / Development / Custom table and then you can pass its codename to API.

Take a look at the "Custom table" module and API examples in documentation. You can then query/modify the data very easily and they can also be managed through Kentico CMS Desk.

User avatar
Member
Member
hitesh - 4/30/2013 7:57:17 AM
   
RE:Insert into Custom table
Hi Petr,

Thanks! Got it. I was expecting something like this but wasn't sure. Thanks again.

Hitesh

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 4/30/2013 7:35:46 AM
   
RE:Insert into Custom table
hitesh wrote: Hi Sandro,
I tried Matt's code but I think I'm missing something, I don't know what. I had created a table in the database using create query in MSSql2005. I passed table name "PM_TaskComment" for the parameter "string customTableClassName" but the code below ( as provided by Matt) return null
DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);

Please help me what could be the problem.
Thanks
Hitesh

I suggest checking out the Dev Guide http://devnet.kentico.com/docs/devguide/index.html?custom_tables_module_overview.htm

You need to initially create the custom table using the Site Manager>Development>Custom Tables. OR if you have already created the table like you mentioned above, then you still need to go into the user interface to create the custom table object/class and link it to your existing custom table. If you don't create the object/class in the UI then you won't be able to query it using the API.

User avatar
Member
Member
hitesh - 4/30/2013 7:53:09 AM
   
RE:Insert into Custom table
Hi Frogg,
Thanks a lot. Yeah I was expecting that but wasn't sure. Thanks again.

Hitesh

User avatar
Member
Member
matt-awg - 4/30/2013 9:21:00 PM
   
RE:Insert into Custom table
hitesh,

I thought you were already using the Kentico module to create your custom tables and just needed help inserting data. Sorry, I would have elaborated more. That code is part of a set of utility functions I created because I use custom tables often and got tired of copying and pasting code to add, update, delete, etc. from them. I hope it still helps you. I am not sure exactly what you are trying to do, but recently I needed to create a Custom Document Type that I wanted users to be able to comment on... I realized it was very similar to the built in Blog module in Kentico w/comments, so I basically created my own custom version of the blog post document type (with the extra fields I needed) and then I was able to use the comments feature and the subscription features that all come with the blog module. It was a bit more complicated than that but depending on what exactly you are trying to do, something like that may work. In your case it could be "task" that would be your version of blog post.

Good Luck,
Matt

User avatar
Member
Member
hitesh - 4/30/2013 11:07:20 PM
   
RE:Insert into Custom table
Hi Matt,

Thanks a lot. Yeah your code will still help me. I think I should create custom table and then link it with the code.
Yeah I'm creating something like comment post in blog. I'm creating a new module in ProjectManagement called task management(it will have recurring task etc). Once again thanks for you help.

Thanks
hitesh


User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 5/9/2013 8:39:27 AM
   
RE:Insert into Custom table
I've finally finished the webpart I was working on for inserting a custom table record outside of the CMSDesk or Site Manager. Take a look here. You could even customize the before and after events if you need any special error handling.