Saving data to SQL Server Kentico db

Sherry Collins asked on May 20, 2015 18:41

I have a poll webpart that has been cloned. I have added a comment box at the end of the poll so when the poll is closed the comment box will become visible. The webpart must save the comments back to the database.

I now have to supply my own implementation and I hesitate to add any new classes to the Kentico web application for obvious reasons.

Does anyone know of a way that I can save the data without creating sql data objects. (sql command, etc) I am hoping there are some kind of helpers within Kentico that I can leverage to do this, but not very familiar with the framework.

Thanks.

Recent Answers


Charles Matvchuk answered on May 20, 2015 18:51 (last edited on May 20, 2015 18:54)

Sherry, there is nothing wrong with adding classes if you need them. Every project I work with has a multitude of classes which are for data access, business logic, new transformation methods and new macro methods. That is why Kentico is so powerful, it extensible. You can also use pure ado.net and just keep the code in the custom webpart which makes it modular. Finally you can refer to the API Documention on pages and custom tables and it also goes a bit into DataQuery..

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on May 20, 2015 18:53

Additionally, I have one implementation that is using Entity Framework. It can be done but I wouldn't recommend it unless there was a business reason behind it.

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on May 20, 2015 18:58

One Last thing. You can look at the API Examples in Kentico. Go to API Examples > Modules > Content Management > Polls. Under create answer you can see the code. Perhaps this will give you inspiration on your scenario.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 20, 2015 19:09

In addition to what Charles mentions, check out the articles on DataQuery:

http://devnet.kentico.com/articles/kentico-8-technology-dataquery

http://devnet.kentico.com/articles/kentico-8-technology-dataquery-api

http://devnet.kentico.com/articles/kentico-8-technology-dataquery-advanced-api

There really is no reason to use any kind of standard SQL commands or create any methods as DataQuery can handle all your data retrieval methods in the Kentico database and any external MS SQL database.

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on May 20, 2015 19:51

Brenden, I guess the only downside I see, is another learning curve and yet another data access/manipulation framework, which if you don't work with Kentico will be another challenge for the next person. If I would have a wish list, I would prefer that the new Kentico API was based on Entity Framework 6+, with ado.net overlay's for mass joins and speed, with a BLL/DAL exposed layer. That way we could use mainstream ORM tools and create inherited classes easier.

In any event. I like to use what you know the best and as long as your are consistent then it will be fine. That is the strength of Kentico, whatever data access/manipulation technology in .NET you know you can use.

Just my two cents, which isn't worth too much today.

2 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on May 20, 2015 19:53

Thanks Charles and Brenden. You guys have been great. I owe you both a drink. I was able to construct a query using the API references. I still have one more question. Back to this custom poll webpart. This is the code I came up with.

DateTime PollClosed; var query = PollInfoProvider.GetPollInfo(5);

    PollClosed = DateTime.Parse(query.PollOpenTo.ToShortDateString());
   // pollcommentbox.Text = x.ToShortDateString();

    OK I have the date the poll closed.  The assumption is that there will only be one active poll at a time.  I am adding a flag to Polls_poll table that will get set when the poll closes.  So instead of the Pollid (5), which I won't know, I need to return the date where the poll is active.  There is no overload that allows me to pass a flag. 

Looks like I need to do a query that gets the Pollid where active flag is on and then pass that pollid to this query. Any idea on how to get the Pollid.

0 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on May 20, 2015 20:31

Can some one tell me what is wrong here. I am getting an error on QueryOperator.Equals. What is the correct syntax for a where clause.

Thanks.

var whereQuery = PollInfoProvider.GetPolls() .Where("ActiveFlag", QueryOperator.Equals,"1") .OrderByDescending("PollID");

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 20, 2015 21:03

Don't disagree one bit Charles, I like consistency as well. I'm all about education. Until I actually set a small amount of time aside to learn some of the basics, I wrote all my own code and wasted a lot of time and caused a lot of issues for upgrades. A day or two of reading, learning or self-education on how things are done vs. just starting to code, can go a very long way. Unfortunately most of us developers tend to just jump head first into a dark shallow pool.

1 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on May 20, 2015 21:14

@Brenden, I agree, I guess it depends if you are a true Developer/coder, or an End User/Designer who just needs to write a bit of custom code which are two very different animals.

1 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on May 20, 2015 21:15

@Sherry, is the ActiveFlag a string or an int or a Boolean?

0 votesVote for this answer Mark as a Correct answer

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