Monday, 2 April 2012

To fill the combobox from the XML file

             //Presentation Layer
            DataSet dsVatDetails = tc.GetVatDetails();
            cboVatCode.DataSource = dsVatDetails.Tables["Vat"];
            cboVatCode.DisplayMember = "VatCode";
            cboVatCode.ValueMember = "Id";
            dsVatDetails.Tables["Vat"].DefaultView.Sort = "VatCode";

        public DataSet GetVatDetails()
        {
            DataSet dsVat = new DataSet();
            string appPath =  GetExecutingPath();
            dsVat.ReadXml(appPath + "VatDetails.xml");
            return dsVat;
        }

public string GetExecutingPath()
        {

            string appPath = string.Empty;// Assembly.GetAssembly(typeof(TrialCalculation)).Location;
            //System.Windows.Forms.MessageBox.Show(appPath);
            string[] appPathArray = GetExecutingAssemblyDirectory().Split("\\//".ToCharArray());          
            if (appPathArray.Contains("bin") == true)
            {
                appPath = string.Empty;
                for (int cnt = 0; cnt < appPathArray.Length - 4; cnt++)
                {
                    appPath = appPath + appPathArray[cnt] + "\\";
                }
            }
            return appPath;
        }

private  string GetExecutingAssemblyDirectory()
        {

            string currentAssembly = Assembly.GetExecutingAssembly().Location; // Get current assembly with filename
            int nbCharToKeep = currentAssembly.Length; // Initialize length

            // Decrease length until an escape caracter is found
            while (currentAssembly[nbCharToKeep - 1] != '\\')
            {
                nbCharToKeep--;
                System.Diagnostics.Debug.Assert(nbCharToKeep >= 0, "GetExecutingAssemblyDirectory() - nbCharToKeep < 0");
            }

            // Return the dir including the last escape
            return currentAssembly.Substring(0, nbCharToKeep);

        }

No comments:

Post a Comment