Showing posts with label editable drop down. Show all posts
Showing posts with label editable drop down. Show all posts

Tuesday, 21 February 2012

Creating csv file and editable dropdown with xml saving





 <?xml version="1.0" encoding="us-ascii" standalone="yes"?>
<Masters>

 <DESIGNATION>
    <DISG>Additional Inspector General  A/C</DISG>  
  </DESIGNATION>
</Masters>



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Net.NetworkInformation;
using System.Management;
  public partial class A: Form
  {
        string sourcefile = Application.StartupPath + "\\Master.xml";
         int cmbDDODesignationflag = 0;

           private void A_Load(object sender, EventArgs e)
        {

                   loadmasters();

        }

         public void loadmasters()
        {
            #region : XML Parsing to load all ComboBox fields :
            try
            {

               // address();
                XmlDocument doc = new XmlDocument();
                doc.Load(sourcefile);
                         
                XmlNodeList DDODISGNode = doc.GetElementsByTagName("DISG");
                foreach (XmlNode Node3Node in DDODISGNode)
                {
                    //These nodes contain the data we want so lets add it to our List.
                    cmbDDODesignation.Items.Add(Node3Node.InnerText.ToString().Trim());
                }             

            }
            catch (Exception ex)
            {
                Logging.Logflatfile_Ex(ex);
            }

            #endregion
        }

    public void updatemasters()
        {
            try
            {
                //FileStream fxml = new FileStream(sourcefile, FileMode.OpenOrCreate);
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(sourcefile);

                if (cmbDDODesignationflag == 1)
                {

                    // find channel element to append to
                    var channels = xDoc.GetElementsByTagName("DESIGNATION", string.Empty);

                    // if no channel tags then exit
                    if (channels.Count == 0)
                        return;
                    // create new item
                    var newItem = xDoc.CreateElement("DISG");
                    // add attributes (with value)
                    // Note: You do not have any attributes in your xml.
                    // This is added to show you how to do it and what the effect is.

                    newItem.InnerText = cmbDDODesignation.Text.ToString().Trim();

                    channels[0].AppendChild(newItem);
                    // add new item to first channel         
                    xDoc.Save(sourcefile);
                    cmbDDODesignation.Items.Add(cmbDDODesignation.Text.ToString().Trim());
                }
               
                          }
            catch (Exception ex)
            {
                MessageBox.Show("Exception Occurred.", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Logging.Logflatfile_Ex(ex);
            }

        }

     private void cmbDDODesignation_Leave(object sender, EventArgs e)
        {
            string cmbDDODesignationNewItem = string.Empty;
            cmbDDODesignationNewItem = this.cmbDDODesignation.Text.ToString().Trim();

            // If there is no flavor to add, do nothing
            if (cmbDDODesignationNewItem == string.Empty)
            {
                cmbDDODesignationflag = 0;
                return;
            }
            else
            {
                if (this.cmbDDODesignation.FindStringExact(cmbDDODesignationNewItem) != -1)
                {
                   
                //if (cmbDDODesignation.Items.Contains(cmbDDODesignationNewItem.ToUpper()) || cmbDDODesignation.Items.Contains(cmbDDODesignationNewItem.ToLower()))
                // {
                    //MessageBox.Show(NewItem + " exists already in the list and will not be added.");
                    cmbDDODesignationflag = 0;
                    return;
                }
                else
                {
                    //this.cmbDDODesignation.Items.Add(cmbDDODesignationNewItem);
                    cmbDDODesignationflag = 1;

                }

            }

          
        }

public void createCSVFromDatable()
        {
            try
            {
                DataTable dt = new DataTable();
                #region : Assigning Fields data to DataTable :

             
                                                   
                dt.Columns.Add("DDODisgnation");
                 dt.Columns.Add("DateTime");            



                                DataRow dr = dt.NewRow();

              
                             
                dr["DDODisgnation"] = cmbDDODesignation.Text.Trim();
                   dr["DateTime"] = DateTime.Now.ToString();
             




                          

              
                dt.Rows.Add(dr);
                #endregion
                destcsvfile = Application.StartupPath + "\\A.csv";
                string newFileName = destcsvfile;


                string clientDetails =     dr["DDODisgnation"].ToString().Trim() + ","             
                                     
                                      + dr["DateTime"].ToString().Trim() + Environment.NewLine;


                if (!File.Exists(newFileName))
                {
                    string clientHeader =  "DDODisgnation".Trim() + ","                                                       
                                        + "DateTime".Trim() + Environment.NewLine;

                    File.WriteAllText(newFileName, clientHeader);
                    File.AppendAllText(newFileName, clientDetails);

                }
                else
                {
                    File.AppendAllText(newFileName, clientDetails);
                }

                if (File.Exists(destcsvfile))
                {
                    MessageBox.Show("Record Created Sucessfully for " + txtFirstName.Text + "\n with Unique ID: " + dr["UniqueID"], "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                        updatemasters();

                }
                else
                {
                    MessageBox.Show("Unable to create csv file", "Fail", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception Occurred.", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Logging.Logflatfile_Ex(ex);
            }

        }