How to Import/Export in CSV in AX 2012


Description:-


Various versions of the Data Import/Export Framework are available. The version that you use depends on the version of Microsoft Dynamics AX that you run in your environment:

  1. For Microsoft Dynamics AX 2012 R3, use the version of the Data Import/Export Framework that is included in that release.
  2. For Microsoft Dynamics AX 2012 R2, use the version of the Data Import/Export Framework that is available in cumulative update 7 for Microsoft Dynamics AX 2012 R2.
  3. For AX 2012 or Microsoft Dynamics AX 2012 Feature Pack, use the version of the Data Import/Export Framework that is available from the Lifecycle Services Downloadable Tools (formerly on InformationSource). 
Here i have given brief explanationn for Import\Export in Ax 2012.

Step 1: Create Table Name it “A_PurchaseOrder”.
  • Expand AOT Node.
  • Open Data Dictionary Node.
  • Select Table Node.
  • Right Click in table Node Select New table.

Insert Fields like Purchase_ID (String), Vender_Code (String), and Vendor_Code (Date) in ExtendedDataType Property select BaseDate, Status (Enum) in EnumType Select PO_Status Enum (Create in Step-2) and Purchase_Amount (Integer).

Step 2: Create BaseEnum Name it “PO_Status”.
  • Expand AOT Node.
  • Open Data Dictionary Node.
  • Select Base Enums right Click and Select New base Enum.
  • Now Select Created New Enums right click and Select New Element Name it “Open”.
  • Add New Element and Name it “Close”.

Step 3: Create Form Name it “ImportExportCsv”.
  • Expand AOT Node.
  • Select Form Node right click Select New Form and Name it ‘ImportExportCsv.
  • Drag and Drop Created table in Form DataSource.
  • Design Proper in Design Node.
  • Create Two Button for Import/Export Data.
  • Create Search Purpose StringEdit Control and two Button for Search Button and Clear Value.
  • Create Grid for Data Storing So User Can See.
  • Now Drag and Drop DataSource Fields in Created Grid Control.

Step 4: Now Generate Import/Export functionality We Will Create Class to check weather data is there or not. For import Data We Have to Check Data is Newer or Existing. So Create Class Name it “CSVData”.
  • Expand AOT Node.
  • Select Classes Node Right Click and Select New Class and Name it “CSVData”.

  • Now Create New Method and Name it “CheckPID”.
  • For Creating New Method in Class Select New to Create New Method.

  • Now Created CheckPID Method Change Return type to Boolean.
  • And Code for Checking Data in Table.
Step 5: Now Create another Method and Name it “main”.
  • Check Return Type Should Void.
  • Create TextIO for Creating Text, Csv, and Doc File.
  • I Had Created Container for Save File in Csv, Text, and Doc.
  • Check Complete Code and See How I Created TextIO.
  • Here I Had used different Controls to Understand How to Use, Enum in TextIO, Integer, Strings, and Date etc.
Step 6: Now Open Previously Created Form and Open Design Node.
·       Generate Import button Click event.
1. Expand Import Button.
2. Select Method Node right Click Mouse over Override Method and Select Clicked Method.
·          





Now Code in generated Clicked Method for Import Csv Data in table.


Step 7: In Import Button We Will Create File Dialog to Select File for Importing in Table.

Step 8: Here we are changing permission to Read Mode, Write Mode, Or Both.

Step 9: In Csv File if Delimiter Found Then we Have to Remove it So Here we Used FiledDelimiter.

Step 10: Now Check for Csv File or Lines in That File and pick First Column Value.

Step 11: Here Tacking Value We Will use Conpeek.

Step 12: Now we Will Check Taken PID is already in Table or its New for that we Already Created Method in CSVData Class CheckPID.

Step 13: Now we Will Check for Data is Exist or What for that we have to have to Call CheckPID method from Class if Data Exist then Update or not then insert in table and Give info for how many record Inserted and Updated.

Step 14: Now Open Generate StringEdit Modified Event to Code for Searching in Table.

Step 15: Here I have Used Searching data from table so Value Found in StringEdit Control it means StringEdit modified Then Directly Check from Table.

Step 16: for searching in Table like Value could be String, Enums, Date, Integer etc. so I have used Criteria for Searching Value.

Step 17: so Criteria will occurs and if value found in table then Check for Data in Table through while loop so we can easily check one by one row in table.


Step 18: Here I have Used Date Format in range which separated by DateSeparator like.
Step 19: After it will Check the Combo box Value is Yes or No. if Yes Found Then Create Csv Otherwise Not. So here we are Checking Value if yes then create otherwise not. So directly it will not create Csv file When Search finished here we are maintaining value. And After Searching by Clicking on Export Button Create Csv File.

Step 20: If Value Not found from table then Clear Range and executeQuery Method Occurs in DataSource.

Step 21: Now Generate Export Button Click Event (Refers Step-6) for Export Data in Csv File in this Event we Will Maintain Value if StringEdit is Null then Export All Data otherwise based on value export Data.

Step 22: After that Generate Search Button Click event so by clicking on We Can Generate Data Based on Searching Controls.to Generate Click event refers Step- 6. And Code for Search Value in table. Here we check if Value is blank in Searching Control then we will give Info Message Otherwise if Value found then call the StringEdit Modified Method.

Step 23: for clearing value in StringEdit Control Generate Clear Button Click event. To Generate Click event (refers Step-6).Here we check if Value in StringEdit Control that it will clear otherwise not.

Step 24: Don’t forgot it Check Grid Control DataSource Property Should Must be DataSource Name.

Step 25: Now open your form Click on Export button to Generate Csv File.

Step 26: Now One File Dialog Will Open Enter File Name and Save and Select extension in which format you have to save File.

Step 27: Now Click on Search Edit Control and Enter Value to Search and Click Search Button.

Step 28: Now Click on Export Csv Button to Generate Csv File and Enter File Name and Click on Save Button.

Step 29: Now modified you Data in Csv File. For Modified right click your Newly Created Csv File Click Edit So File open in Edit Mode.

Step 30: Now Enter New Entry in Text File and Save it.

Step 31: Now double Click on Modified Csv File so you can see your Edited Data in Csv File. File Open in Browser.

Step 32: Now Click on Import button to Create Data in Table which one we Added in Csv File.

Step 33: After File Dialog Will Open for selecting File.

Step 34: Now Click on Folder to open you Modified Csv File and Click Open Button to Open in File FileDialog Form.

Step 35: Now you can See Your File will be selected in File Dialog Form.

Step 36: Now Click on OK Button to Import Csv File Data in table. Here you Can See Info Log will open with How Many Data Inserted and how many data updated in table.

Step 37: Now you can see in your form Inserted Data Which we modified in Csv File it inserted in Table.

Step 38: Now Without Entering Value Click on Search Button to See Infolog with Information.

Step 38: When you Have Entered Value in StringEdit Control if you Want to Clear Value Then Click Clear Button to Clear that Value from StringEdit Controls.

How to Add Row in Gridview Dynamically in Asp.Net

Description:-

This article talks about a small project that presents the idea of having a GridView which facilitates the add rows dynamically at run time. In this article, I'm going to wrap up everything into one for easy reference. The following are the main features that you will see: 
  • Adding rows of TextBox, DropDownlist, Radio button and Checkbox.
  • Retain TextBox values and DropDownlist selected values across postback
  • Ability to remove rows
  • Save all values at once 
To get started fire up Visual Studio and then add a new Web Form page. Add a GridView control to the page. Here's the GridView HTML markup:

Html Markup:-

<div>
  <asp:GridView ID="Gridview1" runat="server" ShowFooter="True"
  AutoGenerateColumns="False" BackColor="White" BorderColor="#999999"
  BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
  GridLines="Vertical">
  <AlternatingRowStyle BackColor="#CCCCCC" />
  <Columns>
    <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
      <asp:TemplateField HeaderText="First Name">
        <ItemTemplate>
          <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="Last Name">
        <ItemTemplate>
          <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="Gender">
        <ItemTemplate>
          <asp:RadioButtonList ID="RBLGender" runat="server" RepeatDirection="Horizontal">
            <asp:ListItem Value="M">Male</asp:ListItem>
            <asp:ListItem Value="F">Female</asp:ListItem>
          </asp:RadioButtonList>
        </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="Status">
        <ItemTemplate>
          <asp:CheckBoxList ID="Status" runat="server" RepeatDirection="Horizontal">
            <asp:ListItem>Single</asp:ListItem>
            <asp:ListItem>Married</asp:ListItem>
          </asp:CheckBoxList>
        </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="Qualification">
        <ItemTemplate>
          <asp:DropDownList ID="drpQualification" runat="server">
            <asp:ListItem Value="G">Graduate</asp:ListItem>
            <asp:ListItem Value="P">Post Graduate</asp:ListItem>
          </asp:DropDownList>
        </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="Grade">
        <ItemTemplate>
          <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </ItemTemplate>
        <FooterStyle HorizontalAlign="Right" />
        <FooterTemplate>
          <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
        </FooterTemplate>
      </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
  </asp:GridView>
</div>

Code behind:-

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    SetInitialRow();
  }
}

private void SetInitialRow()
{
  DataTable dt = new DataTable();
  DataRow dr = null;
  dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
  dt.Columns.Add(new DataColumn("Column1", typeof(string)));
  dt.Columns.Add(new DataColumn("Column2", typeof(string)));
  dt.Columns.Add(new DataColumn("Column3", typeof(string)));
  dt.Columns.Add(new DataColumn("Column4", typeof(string)));
  dt.Columns.Add(new DataColumn("Column5", typeof(string)));
  dt.Columns.Add(new DataColumn("Column6", typeof(string)));

  dr = dt.NewRow();
  dr["RowNumber"] = 1;
  dr["Column1"] = string.Empty;
  dr["Column2"] = string.Empty;
  dr["Column3"] = string.Empty;
  dr["Column4"] = string.Empty;
  dr["Column5"] = string.Empty;
  dr["Column6"] = string.Empty;

  dt.Rows.Add(dr);
  //dr = dt.NewRow();
  //Store the DataTable in ViewState
  ViewState["CurrentTable"] = dt;
  Gridview1.DataSource = dt;
  Gridview1.DataBind();
}

private void AddNewRowToGrid()
{
  int rowIndex = 0;
  if (ViewState["CurrentTable"] != null)
  {
    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
    DataRow drCurrentRow = null;
    if (dtCurrentTable.Rows.Count > 0)
    {
      for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
      {
        //extract the TextBox values
        TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
        TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
        RadioButtonList box4 = (RadioButtonList)Gridview1.Rows[rowIndex].Cells[3].FindControl("RBLGender");
        DropDownList box5 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("drpQualification");
        CheckBoxList box6 = (CheckBoxList)Gridview1.Rows[rowIndex].Cells[6].FindControl("Status");
        drCurrentRow = dtCurrentTable.NewRow();
        drCurrentRow["RowNumber"] = i + 1;
        dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
        dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
        dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
        dtCurrentTable.Rows[i - 1]["Column4"] = box4.SelectedValue;
        dtCurrentTable.Rows[i - 1]["Column5"] = box5.SelectedValue;
        dtCurrentTable.Rows[i - 1]["Column6"] = box6.SelectedValue;
        rowIndex++;
      }
      dtCurrentTable.Rows.Add(drCurrentRow);
      ViewState["CurrentTable"] = dtCurrentTable;
      Gridview1.DataSource = dtCurrentTable;
      Gridview1.DataBind();
    }
  }
  else
  {
    Response.Write("ViewState is null");
  }
  //Set Previous Data on Postbacks
  SetPreviousData();
}

private void SetPreviousData()
{
  int rowIndex = 0;
  if (ViewState["CurrentTable"] != null)
  {
    DataTable dt = (DataTable)ViewState["CurrentTable"];
    if (dt.Rows.Count > 0)
    {
      for (int i = 0; i < dt.Rows.Count; i++)
      {
        TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
        TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
        RadioButtonList box4 = (RadioButtonList)Gridview1.Rows[rowIndex].Cells[4].FindControl("RBLGender");
        DropDownList box5 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("drpQualification");
        CheckBoxList box6 = (CheckBoxList)Gridview1.Rows[rowIndex].Cells[6].FindControl("Status");
        box1.Text = dt.Rows[i]["Column1"].ToString();
        box2.Text = dt.Rows[i]["Column2"].ToString();
        box3.Text = dt.Rows[i]["Column3"].ToString();
        box4.SelectedValue = dt.Rows[i]["Column4"].ToString();
        box5.SelectedValue = dt.Rows[i]["Column5"].ToString();
        box6.SelectedValue = dt.Rows[i]["Column6"].ToString();
        rowIndex++;
      }
    }
  }
}

protected void ButtonAdd_Click(object sender, EventArgs e)
{
  AddNewRowToGrid();
}

Now run your Web Application and Add value click on AddNewRow button to generate new row with previous Row Maintain Data.