protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            string result = "";
            try
            {
                List<DisplayState> dispstate = (List<DisplayState>)Session["display_states"];
                DisplayState temp_state = null;
                string initState = "";
                string filePath = Session["filepath"].ToString().Trim();
                List<string> new_states = new List<string>();
                DataObjectClass obj_dataobject = new DataObjectClass();
                DataTable dtable = new DataTable();

                string qstring = "";

                if (row2.Visible == true)
                {
                    foreach (ListItem item in lstDestinationStates.Items)
                    {
                        new_states.Add(item.Value.ToString().Trim());
                    }

                    foreach (string item in new_states)
                    {
                        temp_state = new DisplayState();
                        temp_state.disp_state_name = item.ToString().Trim();
                        temp_state.InitialState = false;

                        qstring = string.Format("select k.txt_state_name state,k.txt_state_display_name from workflow_state_master k" +
                            " where k.txt_start_comp_link_id=(select p.txt_start_comp_link_id from workflow_state_master p where p.txt_state_display_name='{0}')" +
                            " order by k.txt_state_id", temp_state.disp_state_name.ToString().Trim());

                        dtable = obj_dataobject.getSQLDataTable(qstring);
                        if (dtable != null && dtable.Rows.Count > 0)
                        {
                            temp_state.actual_state_name = dtable.Rows[0][0].ToString().Trim();
                            temp_state.completed_state_name = dtable.Rows[1][0].ToString().Trim();
                        }

                        dispstate.Add(temp_state);
                        temp_state = null;
                    }

                    List<string> every_state = new List<string>();

                    foreach (DisplayState item in dispstate)
                    {
                        every_state.Add(item.disp_state_name.ToString().Trim());
                    }

                    foreach (DisplayState item in dispstate)
                    {
                        item.all_states = null;
                        item.all_states = new List<string>();
                        item.all_states = every_state;
                    }

                    Session["display_states"] = dispstate;

                    grdStateModification.DataSource = null;
                    grdStateModification.DataSource = dispstate;
                    grdStateModification.DataBind();
                    UpdatePanel2.Update();

                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append(@"<script language='javascript' type='text/javascript'>");
                    string script = "HideAllStates();";
                    sb.Append(script);
                    sb.Append(@"</script>");
                    ScriptManager.RegisterClientScriptBlock(this.Page, Page.GetType(), "none", sb.ToString(), false);

                }
                else
                {
                    if (filePath != null)
                    {

                        XDocument doc = XDocument.Load(filePath);
                        XNamespace w = "http://schemas.microsoft.com/winfx/2006/xaml/workflow";
                        XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml";

                        initState = dispstate.Where(state => state.InitialState == true).Select(state => state.actual_state_name).First().ToString().Trim();

                        foreach (XElement element in doc.Root.Elements(w + "StateActivity"))
                        {
                            if (element.Attribute(x + "Name").Value.ToString().Trim() == "START_WORKFLOW")
                            {
                                foreach (XElement inner_element in element.Elements(w + "EventDrivenActivity"))
                                {
                                    inner_element.Attribute(x + "Name").Value = "ON_START_WORKFLOW_" + initState.ToString().Trim();
                                    inner_element.Element(w + "HandleExternalEventActivity").Attribute(x + "Name").Value = "START_WORKFLOW_" + initState.ToString().Trim();
                                    inner_element.Element(w + "HandleExternalEventActivity").Attribute("EventName").Value = initState.ToString().Trim();
                                    inner_element.Element(w + "SetStateActivity").Attribute("TargetStateName").Value = initState.ToString().Trim();
                                    inner_element.Element(w + "SetStateActivity").Attribute(x + "Name").Value = "setStateActivity_START_WORKFLOW_" + initState.ToString().Trim();
                                }
                            }
                            //else
                            //{
                            //    element.RemoveAll();
                            //}
                        }
                        doc.Save(filePath);

                        //foreach (XNode node in doc.Root.Nodes())
                        //{
                        //    node.NodesAfterSelf().Remove();
                        //}

                        doc.Root.Descendants(w + "StateActivity")
                            .Where(node => node.Attribute(x + "Name").Value.ToString().Trim() != "START_WORKFLOW")
                            .Remove();

                        doc.Save(filePath);

                        StringBuilder str = new StringBuilder();
                        List<string> targetStates = new List<string>();

                        str.Append("<StateMachineWorkflowActivity x:Class=" + "\"" + "WorkflowLibrary1." + Session["filename"].ToString().Trim() + "\"" + " InitialStateName=" + "\"" + "START_WORKFLOW" + "\"" + " x:Name=" + "\"" + Session["filename"].ToString().Trim() + "\"" + " DynamicUpdateCondition=" + "\"" + "{x:Null}" + "\"" + " CompletedStateName=" + "\"" + "{x:Null}" + "\"" + " xmlns:x=" + "\"" + "http://schemas.microsoft.com/winfx/2006/xaml" + "\"" + " xmlns=" + "\"" + "http://schemas.microsoft.com/winfx/2006/xaml/workflow" + "\"" + " > ");

                        foreach (DisplayState state in dispstate)
                        {
                            targetStates.Add(GetEndStateName(state.actual_state_name.ToString().Trim()));
                            str.Append(CreateStateForMarkup(GetStartStateName(state.actual_state_name.ToString().Trim()), targetStates));

                            str.Append(CreateStateForMarkup(targetStates.First().ToString().Trim(), state.target_states));
                            targetStates.Clear();
                        }

                        str.Append("</StateMachineWorkflowActivity>");

                        XDocument ndoc = XDocument.Parse(str.ToString().Trim());

                        foreach (XNode node in ndoc.Root.Nodes())
                        {
                            doc.Root.Add(node);
                            doc.Save(filePath);
                        }

                        doc = null;
                        str.Clear();
                        result = "success";
                    }

                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append(@"<script language='javascript' type='text/javascript'>");
                    string script = string.Format(@"SaveConfirmation('{0}')", result.ToString().Trim());
                    sb.Append(script);
                    sb.Append(@"</script>");
                    ScriptManager.RegisterClientScriptBlock(this.Page, Page.GetType(), "none", sb.ToString(), false);

                }

            }
            catch (Exception ex)
            {

                result = ex.Message.ToString().Trim();
            }
        }
        protected void btnDisplayStateTransition_Click(object sender, EventArgs e)
        {
            string result = "";
            try
            {
                if (Session["filepath"] != null)
                {
                    List<StateActivity> actual_states = (List<StateActivity>)Session["actual_states"];
                    List<DisplayState> dispState = new List<DisplayState>();
                    DisplayState state = null;
                    List<string> targetStates = null;
                    List<string> targetstate_displayname = null;
                    StateActivity tempstate = null;
                    string completed_state = "";
                    string qstring = "";
                    DataRow drow = null;
                    DataObjectClass obj_dataobject = new DataObjectClass();
                    string state_status = "";

                    for (int i = 1; i < actual_states.Count; i++)
                    {
                        state = new DisplayState();

                        qstring = string.Format("select decode(s.txt_state_display_name,'','End_State','Start_State'),s.txt_state_display_name Display_Name" +
                                                " from workflow_state_master s where txt_state_name='{0}'", actual_states[i].state_name.ToString().Trim());

                        drow = obj_dataobject.getSQLDataRow(qstring.Trim());

                        if (drow != null)
                        {
                            state_status = drow[0].ToString().Trim();
                        }

                        if (state_status == "Start_State")
                        {
                            state.actual_state_name = actual_states[i].state_name.ToString().Trim();
                            state.disp_state_name = drow[1].ToString().Trim();
                            drow = null;

                            state.InitialState = actual_states[i].InitialState;
                            completed_state = actual_states[i].event_driven[0].setState.ToString().Trim();
                            state.completed_state_name = completed_state.ToString().Trim();

                            tempstate = (from temp in actual_states where temp.state_name == completed_state select temp).First();

                            targetStates = new List<string>();
                            foreach (EventDrivenActivity ediv in tempstate.event_driven)
                            {
                                targetStates.Add(ediv.setState.ToString().Trim());
                            }
                            state.target_states = targetStates;

                            targetstate_displayname = new List<string>();
                            foreach (string target in targetStates)
                            {
                                qstring = string.Format("select s.txt_state_display_name Display_Name" +
                                                        " from workflow_state_master s where txt_state_name='{0}'", target.Trim());
                                drow = obj_dataobject.getSQLDataRow(qstring.Trim());
                                if (drow != null)
                                {
                                    targetstate_displayname.Add(drow[0].ToString().Trim());
                                }
                            }
                            state.targetstate_displayname = targetstate_displayname;

                            targetstate_displayname = null;
                            targetStates = null;
                            tempstate = null;
                            dispState.Add(state);
                            state = null;
                        }

                    }
                    List<string> all_states = new List<string>();
                    foreach (DisplayState item in dispState)
                    {
                        all_states.Add(item.disp_state_name.ToString().Trim());
                    }

                    foreach (DisplayState item in dispState)
                    {
                        item.all_states = all_states;
                    }

                    Session["display_states"] = dispState;
                    grdStateTransition.DataSource = null;
                    grdStateTransition.DataSource = dispState;
                    grdStateTransition.DataBind();
                    result = "success";
                }
                else
                {
                    result = "Please read the file to proceed";
                }

            }
            catch (Exception ex)
            {

                result = ex.Message.ToString().Trim();
            }

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script language='javascript' type='text/javascript'>");
            string str = string.Format(@"DisplayStateTransition('{0}')", result.ToString().Trim());
            sb.Append(str);
            sb.Append(@"</script>");
            ScriptManager.RegisterClientScriptBlock(this.Page, Page.GetType(), "none", sb.ToString(), false);
        }