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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to update changes from Gantt control to SQL database

Platform: ASP.NET Web Forms |
Control: Gantt |
Published Date: November 6, 2015 |
Last Revised Date: May 28, 2019

Actions done in Gantt such as add, edit, delete and row drag/drop can be updated to SQL database using the client side event ActionComplete with the help of AJAX post method. The event argument ‘requestType’ of the ‘ActionComplete’ client side event is used to identify the type of action performed in Gantt such as save, update, delete, add new task. Please refer the below code example for updating the changes to the database.

ASPX

<ej:Gantt ID="Gantt" runat="server
          ActionComplete="ActionComplete">
</ej:Gantt>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SelfReferenceConnectionString %>"
            SelectCommand="SELECT * FROM [GanttData]"></asp:SqlDataSource>
       <asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="True" />
        <script type="text/javascript">
            // To update the database through dialog editing or tool bar editing
            function ActionComplete(args) {
                var ganttObj = $("#Gantt").data("ejGantt");
                if (args.requestType === 'save' && args.addedRecord) {
            //Newly Added Record is obtained here , which can be updated to database
                    var ganttRecord = args.addedRecord.item;
                    if (args.addedRecord.parentItem)
                        ganttRecord["ParentId"] = args.addedRecord.parentItem.taskId;
                    PageMethods.AddIt(ganttRecord);
                    if (args.updatedRecords && args.updatedRecords.length)
                        updateRecords(args.updatedRecords);                }
                else if (args.requestType === 'delete') {
                    var data = args.data;
                    var ganttRec = data.item;
                    PageMethods.DeleteIt(ganttRec);
                    if (data.hasChildRecords) {
                        deleteChildRecords(data);
                    }
                    if (args.updatedRecords && args.updatedRecords.length)
                        updateRecords(args.updatedRecords);
                    }
                }
              // To update the database during Outdent,editing,indent,predecessor update operation
                else if (args.requestType === 'outdent' || args.requestType === 'recordUpdate' || args.requestType === 'indent' || (args.requestType === 'save' && args.modifiedRecord)) {
                    var ganttRec = [];
                    if (args.requestType == "save")
                        ganttRec.push(args.modifiedRecord);
                    else
                        ganttRec.push(args.data);
                    if (args.updatedRecords && args.updatedRecords.length)
                        ganttRec = ganttRec.concat(args.updatedRecords);
                    updateRecords(ganttRec);
                }
                
                // To update the database during drag and drop
                else if(args.requestType == "dragAndDrop")
                {
                    var ganttRec = [];
                        ganttRec.push(args.draggedRow);
                        if (args.updatedRecords && args.updatedRecords.length)
                            ganttRec = ganttRec.concat(args.updatedRecords);
                        updateRecords(ganttRec);
                }
            }
            //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];
                    PageMethods.DeleteIt(currentRecord.item);
                    if (currentRecord.hasChildRecords) {
                        deleteChildRecords(currentRecord);
                    }
                }
            }
            //update the record
            function updateRecords(records)
            {
                var updateRecord = [];
                if (records && records.length) {
                    var length = records.length;
                    for (var i = 0; i < length; i++)
                        updateRecord.push(records[i].item);
                }
                PageMethods.UpdateIt(updateRecord);
            }
        </script>

Code behind [C#]:

Creating Gantt object

  public class TaskData
    {
        public int TaskId { get; set; }
        public string TaskName { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public string Progress { get; set; }
        public int Duration { get; set; }      
        public int? ParentId { get; set; }  
    }

Update method to update the changes from Gantt to SQL database:

    [WebMethod]
       public static void UpdateRecord(List<TaskData> records)
       {
           GanttSample sample1 = new GanttSample();
           sample1.Update(records);
 
       }
        public string GetDate(string date)
        {
            DateTime convertedDate = Convert.ToDateTime(date);
            return (convertedDate.Month + "/" + convertedDate.Day + "/" + convertedDate.Year);
        }
         public void Update(List<TaskData> Tasks)
        {
             //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");
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["SelfReferenceConnectionString"].ConnectionString);//connectionString
 
            con.Open();
            foreach (TaskData Task in Tasks)
            {
                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 + "'";
                using (SqlCommand sqlCommand = new SqlCommand(cmdString, con))
                {
                    sqlCommand.Parameters.AddWithValue("@TaskName", Task.TaskName);
                    sqlCommand.Parameters.AddWithValue("@Duration", Task.Duration);
                    sqlCommand.Parameters.AddWithValue("@StartDate", this.GetDate(Task.StartDate));
                    sqlCommand.Parameters.AddWithValue("@TaskId", Task.TaskId);
                    sqlCommand.Parameters.AddWithValue("@EndDate", this.GetDate(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();
        }

Add method to add new tasks to SQL database

      [WebMethod]
       public static void AddRecord(TaskData record)
       {
           GanttSample sample = new GanttSample();
           sample.Add(record);
         
       }
 
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", this.GetDate(Task.StartDate));
                sqlCommand.Parameters.AddWithValue("@TaskId", Task.TaskId);
                sqlCommand.Parameters.AddWithValue("@EndDate",this.GetDate(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();
        }

Delete method to delete the tasks from SQL database

 
       [WebMethod]
       public static void DeleteRecord(TaskData record)
       {
           GanttSample sample2 = new GanttSample();
           sample2.Delete(record);
       }   
   
        public void Delete(TaskData Task)
        {
            //Delete Task 
            //Using Delete Query to delete Record from SQL Table
 
            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();
 
        }

A simple sample to add, edit, delete and update the data to the SQL database through Gantt is available in the following link.

Sample

2X faster development

The ultimate ASP.NET Web Forms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon