We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Update data in my Database

Good Morning,

How I get the data source from the treegrid.

I am using asp.net and I need to send the data source like a datatable to my database by a webservice

1 Reply

MK Mahalakshmi Karthikeyan Syncfusion Team July 23, 2015 08:32 AM UTC

Hi Michael,

Thanks for using Syncfusion product.

We can add, delete and update the TreeGrid data to the database using “EndEdit” and “ActionComplete” client side events. Please refer the below code snippet the do the operation over the TreeGrid data and send it the database via webservice.

[aspx]

<ej:TreeGrid ID="TreeGrid1" runat="server" //… EndEdit="endEdit" ActionComplete="ActionComplete">

</ej:TreeGrid>

<script type="text/javascript">

    function endEdit(args) {

        var editedRecord = args.data.item;

        //This varible holds the data of the edited record. You can updated it to your remote datasource

        PageMethods.UpdateIt(editedRecord);

    }


    function ActionComplete(args) {

        var record = args.data;

        if (args.requestType === 'addNewRow') {

            //Newly Added Record is obtained here , which can be updated to database

            addedRecord = args.addedRow;

            PageMethods.AddIt(addedRecord);


        } else if (args.requestType === 'delete') {

            var data = args.data;

            var deletedRecord = data.item; //This is the deleted item.

            PageMethods.DeleteIt(deletedRecord);

            //If deleted item has child records, we need to delete that too


            if (data.hasChildRecords) {

                deleteChildRecords(data);

            }

        }


            // To update the database during editing and update operation

        else if (args.requestType === 'recordUpdate') {

            var item = args.data.item;

            PageMethods.UpdateIt(item);

        }

    }


    //Delete inner level child records

    function deleteChildRecords(record) {

        var childRecords = record.childRecords,

            length = childRecords.length,

            count, currentRecord;

        for (count = 0; count < length; count++) {

            currentRecord = childRecords[count];

            var deletedChildRecord = currentRecord.item; //This is the deleted child item.

            //If the above deleted child record has child records, then we need to delete that too.

            if (currentRecord.hasChildRecords) {

                deleteChildRecords(currentRecord);

            }

        }

    }

</script>


[aspx.cs]

public partial class TreeGridSample : System.Web.UI.Page

{

    //…

    public void Update(TaskData Task)

    {

        //Update Edited TaskDetails

        //Using UPDATE SQL Query to Save the Modified taskData into the Table


        connectionString = string.Format(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + Server.MapPath("App_Data") + @"\GanttDatabase2008.mdf;Integrated Security=True;Connect Timeout=30");


        int IDNumber = Task.TaskId;


        string cmdString = "UPDATE GanttData SET TaskId=@TaskId, TaskName=@TaskName,StartDate=@StartDate,EndDate=@EndDate,Duration=@Duration,Progress=@Progress,ParentId=@ParentId WHERE TaskId = '" + IDNumber + "'";


        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["SelfReferenceConnectionString"].ConnectionString); //connectionString


        con.Open();

        using (SqlCommand sqlCommand = new SqlCommand(cmdString, con))

        {

            sqlCommand.Parameters.AddWithValue("@TaskName", Task.TaskName);

            sqlCommand.Parameters.AddWithValue("@Duration", Task.Duration);

            sqlCommand.Parameters.AddWithValue("@StartDate", Task.StartDate);

            sqlCommand.Parameters.AddWithValue("@TaskId", Task.TaskId);

            sqlCommand.Parameters.AddWithValue("@EndDate", Task.EndDate);

            sqlCommand.Parameters.AddWithValue("@Progress", Task.Progress);

            if (Task.ParentId == null)

            {

                sqlCommand.Parameters.AddWithValue("@ParentId", Task.ParentId).Value = "0";

            }

            else

            {

                sqlCommand.Parameters.AddWithValue("@ParentId", Task.ParentId);

            }


            sqlCommand.ExecuteNonQuery();

        }


        con.Close();

    }



    public void Add(TaskData Task)

    {


        //Here Task is the added New Task

        //Add Task

        //Using INSERT Query to add the New Record to SQL Table


        string cmdString = "INSERT INTO GanttData ([TaskName],[TaskId],[StartDate],[EndDate],[Duration],[Progress],[ParentId])" + "VALUES(@TaskName,@TaskId,@StartDate,@EndDate,@Duration,@Progress,@ParentId)";


        connectionString = string.Format(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + Server.MapPath("App_Data") + @"\GanttDatabase2008.mdf;Integrated Security=True;Connect Timeout=30");


        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["SelfReferenceConnectionString"].ConnectionString); //connectionString


        con.Open();

        using (SqlCommand sqlCommand = new SqlCommand(cmdString, con))

        {

            sqlCommand.Parameters.AddWithValue("@TaskName", Task.TaskName);

            sqlCommand.Parameters.AddWithValue("@Duration", Task.Duration);

            sqlCommand.Parameters.AddWithValue("@StartDate", Task.StartDate);

            sqlCommand.Parameters.AddWithValue("@TaskId", Task.TaskId);

            sqlCommand.Parameters.AddWithValue("@EndDate", Task.EndDate);

            sqlCommand.Parameters.AddWithValue("@Progress", Task.Progress);

            if (Task.ParentId == null)

            {

                sqlCommand.Parameters.AddWithValue("@ParentId", Task.ParentId).Value = "0";

            }

            else

            {

                sqlCommand.Parameters.AddWithValue("@ParentId", Task.ParentId);

            }


            int test = sqlCommand.ExecuteNonQuery();

        }


        con.Close();


    }

    public void Delete(TaskData Task)

    {


        //Delete Task

        //Using Delete Query to delete Record from SQL Table



        connectionString = string.Format(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + Server.MapPath("App_Data") + @"\GanttDatabase2008.mdf;Integrated Security=True;Connect Timeout=30");


        int IDNumber = Task.TaskId;

        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["SelfReferenceConnectionString"].ConnectionString); //connectionString

        con.Open();

        SqlCommand cmd = new SqlCommand("delete from GanttData where TaskId = '" + IDNumber + "'", con);

        int result = cmd.ExecuteNonQuery();

        con.Close();


    }



    [WebMethod]

    public static void AddIt(TaskData record)

    {

        TreeGridSample sample = new TreeGridSample();

        sample.Add(record);


    }

    [WebMethod]

    public static void UpdateIt(TaskData record)

    {

        TreeGridSample sample1 = new TreeGridSample();

        sample1.Update(record);


    }

    [WebMethod]

    public static void DeleteIt(TaskData record)

    {

        TreeGridSample sample2 = new TreeGridSample();

        sample2.Delete(record);


    }


}


We have also prepared a sample based on this and you can find the sample under the following location.

Sample: http://www.syncfusion.com/downloads/support/directtrac/141720/ze/TreeGridSample1204993939

Please let us know if you need further assistance on this.

Regards,

Mahalakshmi K.


Loader.
Live Chat Icon For mobile
Up arrow icon