Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to make an organizational chart in a web from using sql command getting data from oracle, I have searched so much and found Google's Organization chart. So I am trying to pass the data to the js code but i don't know what to do to send the data in JSON format.

C#
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Test4.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type='text/javascript'>
        google.load('visualization', '1', { packages: ['orgchart'] });
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Node');
            data.addColumn('string', 'Parent');
            data.addRows(JSON.parse(document.getElementById("<%=HiddenField1.ClientID%>").value));


            var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));
            chart.draw(data);
        }
    </script>
</head>
<body>
    <div id='chart_div'>
    </div>
    <form id="as" runat="server">
        <asp:HiddenField ID="HiddenField1" runat="server" />
    </form>
</body>
</html>
Posted
Comments
ZurdoDev 29-Apr-14 15:35pm    
There are many samples including how to do json. Where are you stuck exactly?
Mohamed Ghandour 30-Apr-14 3:47am    
try
{
OracleCommand cmd = new OracleCommand(stSQL, oradb.con);
OracleDataReader dr = cmd.ExecuteReader();
string str, str1;

int rowCount = 0;
while (dr.Read())
{
rowCount++;
str = dr["EMPLOYEE_NUMBER"].ToString();
str1 = dr["SUPERVISOR_ID"].ToString();
}
}

Thats how i return data so i want to set the org chart with these data.
I need to convert that datatable to JSON string to send it to google's JS code
Mohamed Ghandour 30-Apr-14 4:54am    
I have done this in code behind:

protected void Page_Load(object sender, EventArgs e)
{
HiddenField1.Value = ConvertDataTabletoString();
}

public string ConvertDataTabletoString()
{
DataTable dt = new DataTable();
using (OracleConnection conn = new OracleConnection("User Id=apps;Password=appsnewuser;Data Source=PROD"))
{
using (OracleCommand cmd = new OracleCommand("SELECT HAO_PARENT.ORGANIZATION_ID parent, HAO_CHILD.ORGANIZATION_ID FROM HR_ALL_ORGANIZATION_UNITS hao_parent, HR_ALL_ORGANIZATION_UNITS hao_child, PER_ORG_STRUCTURE_ELEMENTS pos WHERE hao_parent.ORGANIZATION_ID = POS.ORGANIZATION_ID_PARENT AND hao_child.ORGANIZATION_ID = POS.ORGANIZATION_ID_CHILD ORDER BY 1, 2", conn))
{
conn.Open();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<dictionary<string, object="">> rows = new List<dictionary<string, object="">>();
Dictionary<string, object=""> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object="">();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
}
And this in the Java script:

google.load('visualization', '1', { packages: ['orgchart'] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Node');
data.addColumn('string', 'Parent');

data.addRows(JSON.parse(document.getElementById("<%=HiddenField1.ClientID%>").value));

//
var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));
chart.draw(data);
}
But i got this error:

"Every row given must be either null or an array"
Mohamed Ghandour 30-Apr-14 6:38am    
http://forums.asp.net/p/1984303/5687261.aspx?Re+Google+Organizational+Chart+in+Asp+net+from+oracle

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900