Create Family Tree Chart from Access Database using C# and VB.Net in ASP.Net

Last Reply on May 24, 2017 09:13 AM By AnandM

Posted on May 24, 2017 05:17 AM

I want to the database form of your family tree script. I want to use access database instead of sql database. How can I change th codes? I tried to change the codes. But I am getting the error "undefined". What is the problem?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Configuration;
using System.Data.OleDb;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    [WebMethod]

    protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; DATA Source=" + Server.MapPath("app_data/familytree.accdb"));
    }
    public static List<object> GetChartData()
    {
       
        
        string query = "SELECT MemberId, Name, ParentId";
        query += " FROM FamilyHierarchy";
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (OleDbConnection con = new OleDbConnection(constr))
        {
            using (OleDbCommand cmd = new OleDbCommand(query))
            {
                List<object> chartData = new List<object>();
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                using (OleDbDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        chartData.Add(new object[]
                        {
                            sdr["MemberId"], sdr["Name"], sdr["ParentId"]
                        });
                    }
                }
                con.Close();
                return chartData;
            }
        }
    }

}

 

Posted on May 24, 2017 06:22 AM
mfatih says:
 [WebMethod]
 
    protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; DATA Source=" + Server.MapPath("app_data/familytree.accdb"));
    }
    public static List<object> GetChartData()
    {
        
         
        string query = "SELECT MemberId, Name, ParentId";
        query += " FROM FamilyHierarchy";
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (OleDbConnection con = new OleDbConnection(constr))
        {
            using (OleDbCommand cmd = new OleDbCommand(query))
            {
                List<object> chartData = new List<object>();
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                using (OleDbDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        chartData.Add(new object[]
                        {
                            sdr["MemberId"], sdr["Name"], sdr["ParentId"]
                        });
                    }
                }
                con.Close();
                return chartData;
            }
        }
    }

 replace above code with below and one more thing database table should contain records same as per the table structure used in sample

[WebMethod]
public static List<object> GetChartData()
{        
    string query = "SELECT MemberId, Name, ParentId";
    query += " FROM FamilyHierarchy";        
    using (OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; DATA Source=" + HttpContext.Current.Server.MapPath("app_data/familytree.accdb")))
    {
        using (OleDbCommand cmd = new OleDbCommand(query))
        {
            List<object> chartData = new List<object>();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            con.Open();
            using (OleDbDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    chartData.Add(new object[]
                    {
                        sdr["MemberId"], sdr["Name"], sdr["ParentId"]
                    });
                }
            }
            con.Close();
            return chartData;
        }
    }
}