Page functionality works in preview but not on live site

Susanne Hou asked on January 18, 2016 14:57

Hello.

I have a page where I have an online form. The messages sudmitted through this online form is stored in the Kentico database, and then displayed using the "RepeaterWithCustomQuery" web part. There is also a delete button that allows for the visitors to delete a message.

For the repeater, I wrote the transformation myself.

The page build up looks like this in design view in the online portal:

Image Text

I have a custom query:

    USE [Kentico82]

SELECT [LeaveAMessageID]
      ,[FormInserted]
      ,[TextBoxControl]
      ,[TextAreaControl]
  FROM [dbo].[Form_IrisInformationSystem_LeaveAMessage]
order by [FormInserted] desc;

And i have my own homecooked transformation to go with it:

    <%@ Import Namespace="System.Windows.Forms" %>
<%@ Import Namespace="System.Web.UI" %>



<div id="messages">
<table id="t01">
    <thead>
        <tr>
            <th id="date">ID</th>
            <th id="date">Date</th>
            <th id="name">Name</th>
            <th id="message">Message</th>
            <th id="delete">Delete</th>
        </tr>
    </thead>
    <tbody>
        <tr>
          <asp:HiddenField ID="idfield" runat="server" value='<%# GetNotEmpty("LeaveAMessageID") %>' />
            <td id="idfield1"><%# GetNotEmpty("LeaveAMessageID") %></td>
            <td id="datefield"><%# GetNotEmpty("FormInserted") %></td>
            <td id="namefield"><%# GetNotEmpty("TextBoxControl") %></td>
            <td id="messagefield"><%# GetNotEmpty("TextAreaControl") %></td>
            <td id="deletebutton"><asp:Button id="Button1" Text="Delete Message" onclick="Button1_Click" 
             runat="server"> </asp:Button></td>
        </tr>
    </tbody>
</table>
</div>
<script runat="server">  

    void Button1_Click(object sender, EventArgs e) {
      ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Hello');", true);
      try
    {
      var msgID = idfield.Value;//Request["idfield"];
      //ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert(" + msgID +");", true);
      System.Data.SqlClient.SqlConnection sqlConnection1 = 
      new System.Data.SqlClient.SqlConnection("myconnectionstringhere");

      System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
      cmd.CommandType = System.Data.CommandType.Text;
      cmd.CommandText = "DELETE FROM Form_IrisInformationSystem_LeaveAMessage WHERE LeaveAMessageID ="+msgID;
      cmd.Connection = sqlConnection1;

      sqlConnection1.Open();
      cmd.ExecuteNonQuery();
      sqlConnection1.Close();

    }
    catch (Exception ex)
     {
       ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('An error occurred.');", true);
      }
    finally
      {
       // Response.Redirect(Request.RawUrl);
        Response.Redirect("/Notes.aspx");
        //Response.Write("<h1>Note deleted</h1>");
      }
  }
</script>

Now, this actually works great! I can submit a message, and see that it does appear both on the live site and in the database using SQL management studio. I can also see that is i press the dele button, the message disappears, both on the live site and on the database.

However, it only works when i display the page in preview mode. On the live site, I can still submit a message, but it does not show up on the live site even though I can see that it does appear in the database. The same goes for deleting, the message disappears from the database but remains on the live page.

It must have something to do with the repeater not refreshing the data, but I cannot for the life of me figure out why, especially since it works fine in preview mode.

Any suggestions?

Thanks in advance.

Recent Answers


David te Kloese answered on January 18, 2016 15:12

Hi,

do you have some kind of cache enabled?

Can you check:

the Page

Properties > General > Output cache

Or the webparts involved

Configure> performance > partial cache in minutes

Greets,

David

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on January 18, 2016 16:04

  1. You shouldn't be using direct t-sql to delete those records as there are other pieces of the API which are run when items are added, updated or deleted. So what will end up happening is you will have bad or orphaned data.
  2. I believe the issue you're having is due to page lifecycle. You need to give that button the event in the OnControlsLoaded method vs. page load method.
  3. A better method to remove that record would be something like:

    CMS.OnlineForms.BizFormItem item = CMS.OnlineForms.BizFormItemProvider.GetItem(CMS.Helpers.ValidationHelper.GetInteger(Eval("LeaveAMessageID"), 0), "YourFormCodeName");  
    
    if (item != null)
    {
        CMS.OnlineForms.BizFormItemProvider.DeleteItem(item);
    }
    

It's a lot less code, handles any API issues and doesn't directly access the database which is never good.

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on January 18, 2016 16:07

Hi,

On a side node you do realize anybody can delete any message (not just their own) this way right?

David

1 votesVote for this answer Mark as a Correct answer

Susanne Hou answered on January 19, 2016 08:01

Hello David. Thanks for your reply!

Yeah, I do realize that for now everyone can delete any message. So far, that's OK.

I did check the Properties > General > Output cache for the page, and it looks like this:

Image Text

Does this look like it is supposed to? I can't remember editing it, and I'm not sure if this is the standard default settings.

I went on to check the web parts Configure> performance > partial cache in minutes and the settings look like this for the RepeaterWithCustomQuery:

Image Text

And this for the OnlineForm:

Image Text

Is there any of these settings that I need to change?

Brenden, I'll look into your reply now, thanks for taking the time to look into it. I'll get back to you when I've tried it out.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on January 19, 2016 09:26

From your screenshots I am bit confused. The web part says "Message board" - could you please clarify whether you are using the online forms module or message boards? For message boards the cause could be that they may be set to be visible to authenticated users only and not to the public users/visitors.

0 votesVote for this answer Mark as a Correct answer

Susanne Hou answered on January 19, 2016 09:37

Hello Juraj

It is a bit confusing i realize. I made the name of the web part MessageBoard, but it is actually an Online Form that works sort of like a message board (hence the title).

Visitors can submit a message through the online form (named MessageBoard), and the RepeaterWithCustomQuery then displays the messages submitted.

I hope this clarifies.

0 votesVote for this answer Mark as a Correct answer

Susanne Hou answered on January 19, 2016 12:35

Brenden, I've tried what you suggested in 2). However, I'm working in the online portal (free edition), and find it difficult to figure out how to change that without opening the project in visual studio.

This may be a silly question, but can i open the transformation and edit it through visual studio somehow?

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on January 19, 2016 14:39

Hi,

you can, but I personally don't like it much. You can by using virtual objects.

To store objects in the file system, open the System application and select the Virtual objects tab. The options in the Source control section allow you to select which objects are stored in the file system.

More info: https://docs.kentico.com/display/K82/Editing+code+externally+and+using+source+control+systems

Greets,

David

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on January 25, 2016 09:29

Hi,

Thank you for the details. I would also try using the Security debug just in case there are some permissions restrictions. I agree with Brenden - could you please try using the API to access the form data? I think this should help.

1 votesVote for this answer Mark as a Correct answer

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