Import from mySQL database


https://www.nevron.com/Forum/Topic8910.aspx
Print Topic | Close Window

By Cristianf - 9 Years Ago
I'm trying to generate a diagram importing the information from a MySQL database.
I followed this tutorial: http://support.nevron.com/KB/a27/automatically-create-a-diagram-from-a-database.aspx
from the knowledge base, but the result is this:


How you can see, the shapes are not connected and the layout seems to be not applied.

Here is the 2 tables I'm using:

PAGES:


LINKS:


And here is my FULL code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Nevron.GraphicsCore;
using Nevron.Diagram;
using Nevron.Diagram.Shapes;
using Nevron.Diagram.WinForm;
using Nevron.Diagram.Layout;
using Nevron.Diagram.DataImport;

using MySql.Data.MySqlClient;

namespace Databaseimport1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dbdatasetlinks = new DataTable();
            DataTable dbdatasetpages = new DataTable();

            try
            {
                string myConnection = "datasource=localhost;port=3306;username=root;password=";
                MySqlConnection myConn = new MySqlConnection(myConnection);
                MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
                myDataAdapter.SelectCommand = new MySqlCommand("SELECT * FROM nevron.links", myConn);
                MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
                myConn.Open();

                myDataAdapter.Fill(dbdatasetlinks);
                //BindingSource bSource = new BindingSource();
                //bSource.DataSource = dbdatasetlinks;
                //dataGridView1.DataSource = bSource;
                //myDataAdapter.Update(dbdatasetlinks);

                myConn.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


            try
            {
                string myConnection = "datasource=localhost;port=3306;username=root;password=";
                MySqlConnection myConn = new MySqlConnection(myConnection);
                MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
                myDataAdapter.SelectCommand = new MySqlCommand("SELECT * FROM nevron.pages", myConn);
                MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
                myConn.Open();

                myDataAdapter.Fill(dbdatasetpages);
                myConn.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
           
            // begin view init
            nDrawingView1.BeginInit();

            // display the document in the view
            nDrawingView1.Document = nDrawingDocument1;

            // configure the view
           
            nDrawingView1.ViewLayout = ViewLayout.Fit;
            nDrawingView1.Grid.Visible = false;
            nDrawingView1.GlobalVisibility.ShowPorts = false;
            nDrawingView1.HorizontalRuler.Visible = false;
            nDrawingView1.VerticalRuler.Visible = false;
           

            // create two stylesheets - one for the vertices and one for the edges
            NStyleSheet vertexStyleSheet = new NStyleSheet();
            vertexStyleSheet.Name = "Vertices";
            nDrawingDocument1.StyleSheets.AddChild(vertexStyleSheet);

            NStyleSheet edgeStyleSheet = new NStyleSheet();
            edgeStyleSheet.Name = "Edges";
            edgeStyleSheet.Style.StartArrowheadStyle = new NArrowheadStyle(ArrowheadShape.Circle, "", new NSizeL(5, 5), new NColorFillStyle(Color.Gray), new NStrokeStyle(1, Color.Black));
            edgeStyleSheet.Style.EndArrowheadStyle = new NArrowheadStyle(ArrowheadShape.Arrow, "", new NSizeL(5, 5), new NColorFillStyle(Color.Gray), new NStrokeStyle(1, Color.Black));
            nDrawingDocument1.StyleSheets.AddChild(edgeStyleSheet);

            // configure the graph data source importer
            NGraphDataSourceImporter GraphImporter = new NGraphDataSourceImporter();

            // set the document in the active layer of which the shapes will be imported
            GraphImporter.Document = nDrawingDocument1;

             // SET DATA SOURCES
            GraphImporter.VertexDataSource = dbdatasetpages;
            GraphImporter.EdgeDataSource = dbdatasetlinks;

            // vertex records are uniquely identified by their Id (in the Pages table)
            // edges link the vertices with the FromPageId and ToPageId (in the Links table)
            GraphImporter.VertexIdColumnName = "Id";
            GraphImporter.FromVertexIdColumnName = "FromPageId";
            GraphImporter.ToVertexIdColumnName = "ToPageId";

            // create vertices as rectangles shapes, with default size (60, 30)
            NBasicShapesFactory shapesFactory = new NBasicShapesFactory();
            shapesFactory.DefaultSize = new NSizeF(60, 30);
            GraphImporter.VertexShapesFactory = shapesFactory;
            GraphImporter.VertexShapesName = BasicShapes.Rectangle.ToString();

            // set stylesheets to be applied to imported vertices and edges
            GraphImporter.VertexStyleSheetName = "Vertices";
            GraphImporter.EdgeStyleSheetName = "Edges";

            // use layered graph layout
            NLayeredTreeLayout layout = new NLayeredTreeLayout();
            layout.Direction = LayoutDirection.TopToBottom;
            layout.LayerAlignment = RelativeAlignment.Center;
            GraphImporter.Layout = layout;

            // subscribe for the vertex imported event,
            // which is raised when a shape was created for a data source record
            GraphImporter.VertexImported += new ShapeImportedDelegate(OnVertexImported);

            // import
            GraphImporter.Import();

            // end view init
            nDrawingView1.EndInit();
        }

        private void OnVertexImported(NDataSourceImporter dataSourceImporter, NShape shape, INDataRecord record)
        {
            // display the page title in the shape
            object text = record.GetColumnValue("Title");
            if (text == null)
            {
                shape.Text = "Title not specified";
            }
            else
            {
                shape.Text = text.ToString();
            }

            shape.SizeToText(new NMarginsF(10));
        }

    }
}


Any idea why is not working?
Maybe the problem is the database which is not supported? But I see that it successfully reads the tables.... Any help please?