Wednesday, December 9, 2009

Team build definition for WiX project

Here is the settings you need to do for creating team build for WiX projects.
Add the following piece of code in the .proj file;
<PropertyGroup>
    <CustomBeforeWixTargets>$(MSBuildExtensionsPath)\Microsoft\WiX\v3.0\wix.ca.targets</CustomBeforeWixTargets>
  </PropertyGroup>

  <Target Name="Wix">
    <Message Importance="low" Text="Starting WIX build"/>
    <MSBuild Projects="@(WixProjectToBuild)" Properties="OutDir=$(OutDir);SourcePath=$(OutDir);Version=$(AssemblyVersion);FileVersion=$(AssemblyFileVersion);BuildNumber=$(BuildNumber);CustomBeforeWixTargets=$(CustomBeforeWixTargets)" Targets="Rebuild"></MSBuild>
  </Target>
   <!--WIX solutions-->
  <ItemGroup>
    <WixProjectToBuild Include="$(BuildProjectFolderPath)/../../Source_Code/Development/Deployment/AAS.Database.Setup/AAS.Database.Setup.sln" />
  </ItemGroup>


Also ensure that you have the enough configurations set for processor settings;

<ConfigurationToBuild Include="Debug|x86">
      <FlavorToBuild>Debug</FlavorToBuild>
      <PlatformToBuild>x86</PlatformToBuild>
    </ConfigurationToBuild>
 
    <ConfigurationToBuild Include="Debug|Any CPU">
        <FlavorToBuild>Debug</FlavorToBuild>
        <PlatformToBuild>Any CPU</PlatformToBuild>
    </ConfigurationToBuild>

Data Collection Form design in ASP.NET (Input designer XML which can used for CodeSmith template)

Here is the simple code which will create the Form Designer XML in the format shown below which can be used for CodeSmith template to create .ascx and .ascx.cs files.


XML:
<FormDefinition xmlns="http://aas.com.au/FormGen">
  <PageName>zddas</PageName>
  <FormId>asdas</FormId>
  <Sections>
    <Section>
      <SectionTitle>Test Section</SectionTitle>
      <Controls>
        <Control Type="text" Id="txtField" Text="txtField" />
      </Controls>
    </Section>
  </Sections>
</FormDefinition>





Code:
void GenerateInputForCodeSmith()
        {
            XmlDocument doc = new XmlDocument();
            XmlElement rootEle = default(XmlElement);
            XmlElement elt = default(XmlElement);
            XmlElement eltChoice = default(XmlElement);
            XmlAttribute attr = default(XmlAttribute);

            doc.LoadXml("<FormDefinition xmlns=\"http://aas.com.au/FormGen\"></FormDefinition>");
           
            rootEle = doc.CreateElement("PageName");
            rootEle.InnerText = txtPageName.Text;
            doc.DocumentElement.AppendChild(rootEle);

            rootEle = doc.CreateElement("FormId");
            rootEle.InnerText = txtForm.Text;
            doc.DocumentElement.AppendChild(rootEle);

            rootEle = doc.CreateElement("Sections");
            doc.DocumentElement.AppendChild(rootEle);

            XmlElement elt1 = doc.CreateElement("Section");
            rootEle.AppendChild(elt1);

            elt = doc.CreateElement("SectionTitle");
            elt.InnerText = "Test Section";
            elt1.AppendChild(elt);

            XmlElement eltControls = doc.CreateElement("Controls");
            elt1.AppendChild(eltControls);

            Panel p = (Panel)this.form1.FindControl("pnlSection");
            IEnumerable<Section> sections = p.Controls.OfType<Section>();
            Section sec = sections.First<Section>();
            IEnumerable<Field> fields = sec.FindControl("pnlField").Controls.OfType<Field>();
            foreach (Field field in fields)
            {
                elt = doc.CreateElement("Control");
                attr = doc.CreateAttribute("Type");
                attr.InnerText = ((DropDownList)field.FindControl("cboFieldType")).SelectedValue;
                elt.Attributes.Append(attr);
                attr = doc.CreateAttribute("Id");
                attr.InnerText = ((TextBox)field.FindControl("txtFieldName")).Text;
                elt.Attributes.Append(attr);
                attr = doc.CreateAttribute("Text");
                attr.InnerText = ((TextBox)field.FindControl("txtFieldName")).Text;
                elt.Attributes.Append(attr);
                string[] options = ((TextBox)field.FindControl("txtValues")).Text.Split(";".ToCharArray());
                switch (((DropDownList)field.FindControl("cboFieldType")).SelectedValue)
                {
                    case "dropdown":
                        {
                            foreach (string option in options)
                            {
                                eltChoice = doc.CreateElement("option");
                                attr = doc.CreateAttribute("value2");
                                attr.InnerText = option;
                                eltChoice.InnerText = option;
                                eltChoice.Attributes.Append(attr);
                                elt.AppendChild(eltChoice);
                            }
                            break;

                        }
                    case "radio":
                        {
                            foreach (string option in options)
                            {
                                eltChoice = doc.CreateElement("choice");
                                attr = doc.CreateAttribute("value2");
                                attr.InnerText = option;
                                eltChoice.InnerText = option;
                                eltChoice.Attributes.Append(attr);
                                elt.AppendChild(eltChoice);
                            }
                            break;
                        }
                    case "checkbox":
                        {
                            foreach (string option in options)
                            {
                                eltChoice = doc.CreateElement("choice");
                                attr = doc.CreateAttribute("value2");
                                attr.InnerText = option;
                                eltChoice.InnerText = option;
                                eltChoice.Attributes.Append(attr);
                                elt.AppendChild(eltChoice);
                            }
                            break;
                        }
                    default:
                        break;
                }
                eltControls.AppendChild(elt);
            }

            //doc.Save(Server.MapPath("FormDesigner.xml"));
            string output = doc.OuterXml;
            output = output.Replace(" xmlns=\"\"","");
            FileStream fileStream = File.Open(Server.MapPath("FormDesigner.xml"), FileMode.Create);
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] bytes = encoding.GetBytes(output);
            fileStream.Write(bytes, 0, bytes.Length);
            fileStream.Close();
            fileStream.Dispose();

        }

Simple code for dynamically adding user controls using + button functionality in ASP.NET

static int i = 0;
        static List<int> count = new List<int>();
       
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            RebuildFields();
        }
        void RebuildFields()
        {
            foreach (int j in count)
            {
                UserControl uc = (UserControl)Page.LoadControl("Field.ascx");
                uc.ID = "uc" + j;
                pnlField.Controls.Add(uc);
                LiteralControl lc = new LiteralControl();
                lc.ID = "lc" + j;
                lc.Text = "<br/>";
                pnlField.Controls.Add(lc);
                Button btnAddField = new Button();
                btnAddField.ID = "btnAddField" + j;
                btnAddField.Text = "+";
                btnAddField.Click += btnAddField_Click;
                pnlField.Controls.Add(btnAddField);
                              

                lc = new LiteralControl();
                lc.ID = "lc_" + j;
                lc.Text = "<br/>";
                pnlField.Controls.Add(lc);
            }
        }
        protected void btnAddField_Click(object sender, EventArgs e)
        {
            UserControl uc = (UserControl)Page.LoadControl("Field.ascx");
            i = i + 1;
            count.Add(i);
            uc.ID = "uc" + i;
            pnlField.Controls.Add(uc);
            LiteralControl lc = new LiteralControl();
            lc.ID = "lc" + i;
            lc.Text = "<br/>";
            pnlField.Controls.Add(lc);
            Button btnAddField = new Button();
            btnAddField.ID = "btnAddField" + i;
            btnAddField.Text = "+";
            btnAddField.Click += btnAddField_Click;
            pnlField.Controls.Add(btnAddField);
                       
            lc = new LiteralControl();
            lc.ID = "lc_" + i;
            lc.Text = "<br/>";
            pnlField.Controls.Add(lc);
        }
    }