Articles in this section
Category / Section

How to Add, Delete and Update the SQL Database through Gantt?

1 min read

In Gantt control, you can save the changes of operations like Add, Update and Delete to the SQL database with the help of the client-side event ActionComplete and code behind in C#. You can refer the following code example for more details.

In Default.aspx page:

HTML

[ASPX] 
<form id="form1" runat="server" onsubmit="return false">
//…
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT * FROM [GanttData]"></asp:SqlDataSource>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" />
</form>
<script type="text/javascript">
        function ActionComplete(args) {
            var ganttRecord = args.data;
            if (args.requestType === 'save' && args._cAddedRecord) {
                PageMethods.AddIt(ganttRecord);
            }            
           else if (args.requestType === 'save') {
                ganttRecord.ParentId = args._cModifiedData.item.ParentId;
                PageMethods.UpdateIt(ganttRecord);
            }            
           else if (args.requestType === 'delete') {
                var children = ganttRecord.childRecords;
                PageMethods.DeleteIt(ganttRecord.taskId);
                if (ganttRecord.hasChildRecords) {
                    for (var i = 0; i < children.length; i++)
                        PageMethods.DeleteIt(children[i].taskId);
                }
            }
        }
    </script>

In Default.aspx.cs page:

HTML

[ASPX.CS]
   public partial class GanttSamples : System.Web.UI.Page
    {
        string connectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        public void Update(TaskData Task)
        {
            //Update Edited TaskDetails
            //Using UPDATE SQL Query to Save the Modified taskData into the Table
            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["ConnectionString"].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);
                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)";
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].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 == 0)
                {
                    sqlCommand.Parameters.AddWithValue("@ParentId", Task.ParentId).Value = "1";
                }
                else
                {
                    sqlCommand.Parameters.AddWithValue("@ParentId", Task.ParentId);
                }
                int test = sqlCommand.ExecuteNonQuery();
            }
            con.Close();
        }
        [WebMethod]
         public void Delete(int taskId)
         {
             //Delete Task 
             //Using Delete Query to delete Record from SQL Table
             //TaskData._TaskId = taskId;
             int IDNumber = taskId;
             SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);//connectionString
             con.Open();
             SqlCommand cmd = new SqlCommand("delete from GanttData where TaskId = '" + IDNumber + "'", con);
             //int result = cmd.ExecuteNonQuery();
             SqlDataAdapter adapter = new SqlDataAdapter();     
             adapter.DeleteCommand = con.CreateCommand();
             adapter.DeleteCommand = cmd;
             adapter.DeleteCommand.ExecuteNonQuery();        
             con.Close();
            // HttpContext.Current.Response.Redirect("~/Default.aspx");
         }
        [WebMethod]
        public static void AddIt(TaskData record)
        {
            GanttSamples sample = new GanttSamples();
            sample.Add(record);
        }
        [WebMethod]
        public static void UpdateIt(TaskData record)
        {
            GanttSamples sample1 = new GanttSamples();
            sample1.Update(record);
        }
        [WebMethod]
        public static int DeleteIt(int Id)
        {
            GanttSamples sample2 = new GanttSamples();
            sample2.Delete(Id);
            return 0;
        }
    }

 

Note

A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.

You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied