Back to Devexpress

How to: Save and load documents from a database

aspnet-119387-components-rich-text-editor-examples-how-to-save-and-load-documents-from-a-database.md

latest10.0 KB
Original Source

How to: Save and load documents from a database

  • Dec 17, 2020
  • 4 minutes to read

This code example demonstrates how to save and restore ASPxRichEdit documents from a database using a Binary column.

Use the Open to load a document and call the SaveCopy method to save changes. It is also possible to handle the Saving event to save a document by clicking the ribbon’s built-in Save button.

See Also:

MVC Version:

T352035: RichEdit - How to save and load documents from a database

View Example

aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPxRichEditBinding.Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxRichEdit.v15.2, Version=15.2.11.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxRichEdit" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v15.2, Version=15.2.11.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <dx:ASPxRichEdit ID="RichEdit" runat="server" WorkDirectory="~\App_Data\WorkDirectory" OnSaving="RichEdit_Saving"></dx:ASPxRichEdit>
        <dx:ASPxButton ID="OpenFromStreamBtn" runat="server" Text="Open from Stream" OnClick="OpenFromStreamBtn_Click"></dx:ASPxButton>
        <dx:ASPxButton ID="OpenFromByteArrayBtn" runat="server" Text="Open from Byte Array" OnClick="OpenFromByteArrayBtn_Click"></dx:ASPxButton>
        <dx:ASPxButton ID="SaveButton" runat="server" Text="Save" OnClick="SaveButton_Click"></dx:ASPxButton>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DocumentsConnectionString %>" OnUpdating="SqlDataSource1_Updating" SelectCommand="SELECT * FROM [Docs]" UpdateCommand="UPDATE [Docs] SET [DocBytes] = @DocBytes WHERE [Id] = @Id">
            <UpdateParameters>
                <asp:Parameter Name="DocBytes" />
                <asp:Parameter Name="Id" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </form>
</body>
</html>
csharp
using System;
using System.Data;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.Office;
using DevExpress.XtraRichEdit;

namespace ASPxRichEditBinding {
    public partial class Default : System.Web.UI.Page {
        string SessionKey = "EditedDocuemntID";
        protected string EditedDocuemntID {
            get { return (string)Session[SessionKey] ?? string.Empty; }
            set { Session[SessionKey] = value; }
        }
        protected void SaveButton_Click(object sender, EventArgs e) {
            RichEdit.Save();
        }

        protected void OpenFromStreamBtn_Click(object sender, EventArgs e) {
            if(!string.IsNullOrEmpty(EditedDocuemntID)) {
                DocumentManager.CloseDocument(DocumentManager.FindDocument(EditedDocuemntID).DocumentId);
                EditedDocuemntID = string.Empty;
            }
            DataView view = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            EditedDocuemntID = view.Table.Rows[0]["Id"].ToString(); // Guid type
            if(view.Count != 0)
                RichEdit.Open(
                    EditedDocuemntID,
                    DocumentFormat.Rtf,
                    () => {
                        byte[] docBytes = (byte[])view.Table.Rows[0]["DocBytes"];
                        return new MemoryStream(docBytes);
                    }
                );
        }

        protected void OpenFromByteArrayBtn_Click(object sender, EventArgs e) {
            if(!string.IsNullOrEmpty(EditedDocuemntID)) {
                DocumentManager.CloseDocument(DocumentManager.FindDocument(EditedDocuemntID).DocumentId);
                EditedDocuemntID = string.Empty;
            }
            DataView view = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            if(view.Count != 0)
                EditedDocuemntID = view.Table.Rows[0]["Id"].ToString(); // Guid type
                RichEdit.Open(
                    EditedDocuemntID,
                    DocumentFormat.Rtf,
                    () => { return (byte[])view.Table.Rows[0]["DocBytes"]; }
                );
        }

        protected void RichEdit_Saving(object source, DocumentSavingEventArgs e) {
            // Save document with the Ribbon Save button
            e.Handled = true;
            SqlDataSource1.Update();
        }

        protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e) {
            e.Cancel = RichEdit.DocumentId == string.Empty; //Save only opened documents
            e.Command.Parameters["@Id"].Value = RichEdit.DocumentId;
            e.Command.Parameters["@DocBytes"].Value = RichEdit.SaveCopy(DocumentFormat.Rtf);
        }
    }
}
aspx
<%@ Page Language="vb" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="ASPxRichEditBinding.Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxRichEdit.v15.2, Version=15.2.11.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxRichEdit" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v15.2, Version=15.2.11.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <dx:ASPxRichEdit ID="RichEdit" runat="server" WorkDirectory="~\App_Data\WorkDirectory" OnSaving="RichEdit_Saving"></dx:ASPxRichEdit>
        <dx:ASPxButton ID="OpenFromStreamBtn" runat="server" Text="Open from Stream" OnClick="OpenFromStreamBtn_Click"></dx:ASPxButton>
        <dx:ASPxButton ID="OpenFromByteArrayBtn" runat="server" Text="Open from Byte Array" OnClick="OpenFromByteArrayBtn_Click"></dx:ASPxButton>
        <dx:ASPxButton ID="SaveButton" runat="server" Text="Save" OnClick="SaveButton_Click"></dx:ASPxButton>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DocumentsConnectionString %>" OnUpdating="SqlDataSource1_Updating" SelectCommand="SELECT * FROM [Docs]" UpdateCommand="UPDATE [Docs] SET [DocBytes] = @DocBytes WHERE [Id] = @Id">
            <UpdateParameters>
                <asp:Parameter Name="DocBytes" />
                <asp:Parameter Name="Id" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </form>
</body>
</html>
vb
Imports System
Imports System.Data
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DevExpress.Web.Office
Imports DevExpress.XtraRichEdit

Namespace ASPxRichEditBinding
    Partial Public Class [Default]
        Inherits System.Web.UI.Page

        Private SessionKey As String = "EditedDocuemntID"
        Protected Property EditedDocuemntID() As String
            Get
                Return If(DirectCast(Session(SessionKey), String), String.Empty)
            End Get
            Set(ByVal value As String)
                Session(SessionKey) = value
            End Set
        End Property
        Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs)
            RichEdit.Save()
        End Sub

        Protected Sub OpenFromStreamBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
            If Not String.IsNullOrEmpty(EditedDocuemntID) Then
                DocumentManager.CloseDocument(DocumentManager.FindDocument(EditedDocuemntID).DocumentId)
                EditedDocuemntID = String.Empty
            End If
            Dim view As DataView = DirectCast(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
            EditedDocuemntID = view.Table.Rows(0)("Id").ToString() ' Guid type
            If view.Count <> 0 Then
                RichEdit.Open(EditedDocuemntID, DocumentFormat.Rtf, Function()
                    Dim docBytes() As Byte = CType(view.Table.Rows(0)("DocBytes"), Byte())
                    Return New MemoryStream(docBytes)
                End Function)
            End If
        End Sub

        Protected Sub OpenFromByteArrayBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
            If Not String.IsNullOrEmpty(EditedDocuemntID) Then
                DocumentManager.CloseDocument(DocumentManager.FindDocument(EditedDocuemntID).DocumentId)
                EditedDocuemntID = String.Empty
            End If
            Dim view As DataView = DirectCast(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
            If view.Count <> 0 Then
                EditedDocuemntID = view.Table.Rows(0)("Id").ToString() ' Guid type
            End If
                RichEdit.Open(EditedDocuemntID, DocumentFormat.Rtf, Function() CType(view.Table.Rows(0)("DocBytes"), Byte()))
        End Sub

        Protected Sub RichEdit_Saving(ByVal source As Object, ByVal e As DocumentSavingEventArgs)
            ' Save document with the Ribbon Save button
            e.Handled = True
            SqlDataSource1.Update()
        End Sub

        Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs)
            e.Cancel = RichEdit.DocumentId = String.Empty 'Save only opened documents
            e.Command.Parameters("@Id").Value = RichEdit.DocumentId
            e.Command.Parameters("@DocBytes").Value = RichEdit.SaveCopy(DocumentFormat.Rtf)
        End Sub
    End Class
End Namespace