Skip to main content

Save to Database

  • 2 minutes to read

The ASPxHtmlEditor content can be saved to a database as HTML markup in the following ways:

  • Using two-way (read/write) Data-Binding Expression (the <%# bind> expression)

    The following example illustrates the data-bound ASPxHtmlEditor within the ASPxGridView. After editing, the updated ASPxGridView content is saved to the database.

    <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
        DataFile="~/App_Data/Data_28072008.mdb"
        SelectCommand="SELECT * FROM [Table1]" 
        DeleteCommand="DELETE FROM [Table1] WHERE [ID] = ?" 
        InsertCommand="INSERT INTO [Table1] ([ID], [Name], [DescriptionHtml]) VALUES (?, ?, ?)" 
        UpdateCommand="UPDATE [Table1] SET [Name] = ?, [DescriptionHtml] = ? WHERE [ID] = ?">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="DescriptionHtml" Type="String" />
            <asp:Parameter Name="ID" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="ID" Type="Int32" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="DescriptionHtml" Type="String" />
        </InsertParameters>
    </asp:AccessDataSource>
    
    <dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" 
    Width="369px" DataSourceID="AccessDataSource1" KeyFieldName="ID">
        <Templates>
            <EditForm>
                <dx:ASPxHtmlEditor ID="ASPxHtmlEditor2" runat="server" 
                    Html='<%# Bind("DescriptionHtml") %>'>
                </dx:ASPxHtmlEditor>
                <br/>
                <div style="text-align: right; padding: 2px 2px 2px 2px">
                <dx:ASPxGridViewTemplateReplacement ID="UpdateButton" 
                    ReplacementType="EditFormUpdateButton" runat="server">
                </dx:ASPxGridViewTemplateReplacement>
                <dx:ASPxGridViewTemplateReplacement ID="CancelButton" 
                    ReplacementType="EditFormCancelButton"    runat="server">
                </dx:ASPxGridViewTemplateReplacement>
                </div>
            </EditForm>
        </Templates>
        <Columns>
            <dx:GridViewCommandColumn VisibleIndex="0">
                <EditButton Visible="True">
                </EditButton>
            </dx:GridViewCommandColumn>
            <dx:GridViewDataTextColumn FieldName="ID" ReadOnly="True" 
                VisibleIndex="1">
                <EditFormSettings Visible="False" />
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn FieldName="Name" VisibleIndex="2">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn FieldName="DescriptionHtml" VisibleIndex="3">
                <PropertiesTextEdit EncodeHtml="False">
                </PropertiesTextEdit>
            </dx:GridViewDataTextColumn>
        </Columns>
    </dx:ASPxGridView>
    
  • Using database queries

    In the following example, the Save To DataBase button creates a connection with the Access DataBase and inserts the ASPxHtmlEditor contents into a new tuple.

    <div id="Div1">Your name:</div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <div id="Div2">Your message:</div>
    <dx:ASPxHtmlEditor ID="ASPxHtmlEditor1" runat="server">
    </dx:ASPxHtmlEditor>
    <asp:Button ID="Button1" runat="server" Text="Save to DataBase" onclick="Button1_Click" />
    
    public partial class _Default : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
        }
        protected void Button1_Click(object sender, EventArgs e) {
            string MyHtml = ASPxHtmlEditor1.Html.ToString();
            OleDbConnection myConnection = new OleDbConnection();
            myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; 
                Data Source=C:/Solutions/MyProject/App_Data/test.mdb";
            myConnection.Open();
            OleDbCommand myCommand = new OleDbCommand();
            myCommand.Connection = myConnection;
            myCommand.CommandText = "INSERT INTO Table1(Name,DescriptionHtml) 
            VALUES (@name,@message)";
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
            dataAdapter.SelectCommand = myCommand;
            dataAdapter.SelectCommand.Parameters.Add("@name", 
                OleDbType.VarChar, 50).Value = TextBox1.Text;
            dataAdapter.SelectCommand.Parameters.Add("@message", 
                OleDbType.VarChar, 200).Value = ASPxHtmlEditor1.Html.ToString();
            DataSet ds = new DataSet();
            dataAdapter.Fill(ds, "Table1");
            myConnection.Close();
        }
    }