Runtime form controls using Axapta and save data

This is sample code for Runtime form controls using MSD Axapta and save data to table

 public void GenerateControl()  
 {  
 int i;  
 int a;  
 int d;  
 str lable;  
 DictTable dictTable;  
 TableId tableID;  
 ;  
 i = 1;  
 d=1;  
 a=0;  
 lable = "";  
 formTabControl = this.form().addControl(FormControlType::Tab,"MainTab");  
 formTabControl.heightMode(1);  
 formTabControl.widthMode(1);  
 formTabPageControl = formTabControl.addControl(FormControlType::TabPage,"MainTabPage");  
 formTabPageControl.caption("Test");  
 this.controlMethodOverload(true);  
 formGroupControl = formTabPageControl.addControl(FormControlType::Group, "MyGroup");  
 formGroupControl.caption("Test");  
 while select * from _Question order by RecId  
 {  
 a = 1;  
 while select * from answer where answer.GroupId == _Question.GroupId  
 {  
 if(_Question.ControlId == QuestionControlInputType::CheckBox)  
 {  
 formCheckBoxControl = formGroupControl.addControl(FormControlType::CheckBox, num2str(i,2,0,0,0) + num2str(a,2,0,0,0) + "Question" );  
 formCheckBoxControl.label(answer.Text);  
 }  
 if(_Question.ControlId == QuestionControlInputType::RadioButton)  
 {  
 formRadioControl = formGroupControl.addControl(FormControlType::RadioButton, num2str(i,2,0,0,0) + num2str(a,2,0,0,0) + "Question" );  
 formRadioControl.caption(answer.Text);  
 }  
 a = a+1;  
 }  
 if(_Question.ControlId == QuestionControlInputType::Text)  
 {  
 formStringControl = formGroupControl.addControl(FormControlType::String, "QuestionStr" ); //num2str(i,2,0,0,0) +  
 formStringControl.label( num2str(i,2,0,0,0) + ". "+ _Question.QText + ":");  
 }  
 else if (_Question.ControlId == QuestionControlInputType::Grid)  
 {  
 formStaticTextControl = formGroupControl.addControl(FormControlType::StaticText, "Question" );//num2str(i,2,0,0,0) +  
 formStaticTextControl.text(num2str(i,2,0,0,0) + ". "+ _Question.QText + ":");  
 formDataSource = new FormBuildDataSource();  
 // this.form().addDataSource("_dataGridTbl");  
 formDataSource = this.form().dataSource(d);  
 tableID =_dataGridTbl.TableId;  
 dictTable = new DictTable(this.form().dataSource(d).table());  
 formDataSource.name( num2str(i,2,0,0,0) + "QuestionDb");  
 formDataSource.name(this.form().dataSource(d).name());  
 formDataSource.name(dictTable.name());  
 // formDataSource.table(this.form().dataSource(d).table());  
 formDataSource.autoQuery(true);  
 formGridControl = formGroupControl.addControl(FormControlType::GRID, "QuestionGri" );//num2str(i,2,0,0,0) +  
 formGridControl.dataSource(dictTable.name());  
 formGridControl.widthMode(1);  
 formGridControl.heightMode(1);  
 this.showFields(tableNum(_dataGridTbl), formGridControl, formDataSource);  
 d =d+1;  
 }  
 i = i+1;  
 }  
 formButtonControl = this.form().addControl(FormControlType::Button, "CmdSave");  
 formButtonControl.text("Save");  
 form1 = this.form();  
 }