Articles in this section
Category / Section

How to save and load diagram from Database in ASP.NET Web Forms?

3 mins read

Diagram can be saved and loaded back from database.

  1. Save method of diagram returns a serialized JSON object that contain the current state of the diagram.
  2. You can convert the JSON object into a string and it can be stored anywhere (file, database, etc...)
  3. This string can be converted back to JSON object and loaded using Load method.
  4. The same methodology can be used for both ASP.NET and ASP.NET MVC application.

The following section explains you on how to implement the above.

ASP.NET MVC

In ASP.NET MVC, save and load in database is performed using AJAX Post method. The following code illustrates how to save the diagram in database.

Save

CSHTML

     function save() {
        var diagram = null;
        var diagram = $("#diagram1").ejDiagram("instance");
        //returns diagram model’s json data
        var save = diagram.save();
        //convert JSON object to JSON string
        var Jsonstring = JSON.stringify(save);
        //pass JSON string to server via AJAX post
        $.ajax({
            url: "/Entity/save",
            type: "Post",
            dataType: "json",
            data: { "stringify": Jsonstring },
            success: function (Jsonstring) {
                alert("Success");
                return true;
            },
        });

The following HttpPost is executed when the jQuery AJAX call is made from client side. This method accepts the JSON string from client side and it is inserted in the database.

CONTROLLER

[HttpPost]
        public ActionResult save(string stringify)
        {
                NorthwindcontextsDiagramEntities entities = new NorthwindcontextsDiagramEntities();
                SaveDatabase SaveJSON = new SaveDatabase();
                SaveJSON.JSONString = stringify;
                SaveJSON.Name = "Sam";
                entities.SaveDatabases.Add(SaveJSON);
                entities.SaveChanges();
            return null;
        }

The following code illustrates how to load the diagram from the database.

Load

CSHTML

function load() {
        $.ajax({
            url: "/Entity/load",
            type: "Post",
            success: function (Jsonstring) {
                var diagram = null;
                var diagram = $("#diagram1").ejDiagram("instance");
                //convert JSON string to JSON object
                var loadDiagram = JSON.parse(Jsonstring);
                //load the diagram using saved json data
                diagram.load(loadDiagram);
            },
        });
    }

The following HttpPost is executed when the jQuery AJAX call is made from client side. This method retrieves the JSON string from the database and send to client side.

CONTROLLER

[HttpPost]
        String name,jsonstring;
        public ActionResult load()
        {
            NorthwindcontextsDiagramEntities entities = new NorthwindcontextsDiagramEntities();
            entities = SqlCE;
            List<SaveDatabase> loadJSON = entities.SaveDatabases.ToList<SaveDatabase>();
            foreach (SaveDatabase data in loadJSON)
            {
                jsonstring = data.JSONString;
                name = data.Name;
            }
            string loadcontent= jsonstring;
            return Content(loadcontent);
        }

 

Here is the sample of ASP.NET MVC

Sample:https://www.syncfusion.com/downloads/support/forum/121121/ze/saveloadsample1583379421

 

ASP.NET

In ASP.NET, save and load in database is performed using AJAX PageMethods. PageMethods is a new mechanism in ASP.NET applications where the server code can be bound to ASP.NET pages. It is an easy way to communicate asynchronously with the server using ASP.NET AJAX technologies. It is an alternate to calling the page that is reliable and carries a low risk. It is basically used to expose the web methods to the client script. 

Set EnablePageMethods=”true” in ScriptManger tag that allows to call a web page method directly from JavaScript.

ASPX

<asp:ScriptManager ID="ScriptManager1" runat="server"   EnablePageMethods="true"></asp:ScriptManager>

The following code illustrates how to save the diagram in database.

Save

ASPX

function save() {
            var diagram = null;
            var diagram = $("#Diagram1").ejDiagram("instance");
            //returns diagram model’s json data
            var saving = diagram.save();
            //convert JSON object to JSON string
            var stringify = String(JSON.stringify(saving));
            PageMethods.save(stringify);
        }

The following WebMethod is executed when the AJAX PageMethods call is made from client side. This method accepts the JSON string from client side and it is inserted in the database.

ASPX.CS

[WebMethod]
        public static void save(string stringify)
        {
            NorthWindDataContextDiagramEntities entities = new NorthWindDataContextDiagramEntities();
            SaveDatabase SaveJSON = new SaveDatabase();
            SaveJSON.JSONString = stringify;
            SaveJSON.Name = "Sam";
            entities.SaveDatabases.Add(SaveJSON);
            entities.SaveChanges();
        }

The following code illustrates how to load the diagram from the database.

ASPX

function load() {
            PageMethods.load(OnSuccess);
        }
        function OnSuccess(stringify) {
            var diagram = null;
            var diagram = $("#Diagram1").ejDiagram("instance");
            //convert JSON string to JSON object
            var loaddiagram = JSON.parse(stringify);
            //load the diagram using saved json data
            diagram.load(loaddiagram);
        }

The following HttpPost is executed when the AJAX PageMethods call is made from client side. This method retrieves the JSON string from the database and send to client side.

ASPX.CS

[WebMethod]
        String name,jsonstring;
        public static string load()
        {
            NorthWindDataContextDiagramEntities entities = new NorthWindDataContextDiagramEntities();
            entities = SqlCE;
            List<SaveDatabase> loadJSON = entities.SaveDatabases.ToList<SaveDatabase>();
            foreach (SaveDatabase data in loadJSON)
            {
                jsonstring = data.JSONString;
                name = data.Name;
            }
            string loadcontent = jsonstring;
            return loadcontent;
        }

Note:

A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.

You can also contact us through our support forumsDirect-Trac, or feedback portalWe are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied