Professional Documents
Culture Documents
@ltml ActionLink("Product Master Update", "Index", "Home")
Creating the Dataset to Contain the AS400 table We then create a DataSet in the Models folder. Right click the folder ‘Models’ and add item, select Data and choose DataSet. a viatce ao 4G sconereiyows nots Viale oa a aa = 4B. tesa 01conoe Gear oath hae tas rodvmotseoa Wie Predicted Inthe Server Explorer toolbox, we connect as follows:x0gj00L 4, Gi Dits Connections select the “# icon and the following window wll appear. The name ofthe server wl appear inthe blue rectangle shown below Enter information to conmectto the elected dat source or lek “Change” to choose asiferent dos source and/or provider Osta sauce NET Frama Oats Brovider for OLE DE 2 ‘OLE DB Provider (leba2terisiosteMoAa OLE DB Provider =) Datatinks, Enter server oe file narne Serverorfilenamne: PEPKORAL {og onto the server Use Windows NTIntegrated Seu © sea specific user mare and passwerd: E-Binkpsewerd ElAliw suing psiword Advanced = Gea a ‘As shown above, the main thing is to get the correct Data source followed by the OLEDS provider. The other field to complete is the ‘Server or File name’ Don't forget to click on the Test Connection’ button to confirm that the remote server is connectable. Following that, you can click “Ok’. The following connection will be shown below, under the Data Connections section as TBMDA4@@ . BARBUDA, Sever plore - ox os Heo & 4 Data Connections By clicking on the Tables folder, you will see the following lst of tables existing in that particular library (eg, RAYALS), We will be focusing on the product table. We click on the PRODUCT table on the left pane (server explorer) and drag it to the ProductDataSet.xsd file on the right. This will create an instance of the table in the xsd.aes 5Q. Fill,GetData 0 Model Setup In the Models folder, we then create a class of ProductsMaster that is responsible for entering data from the web UI to the 'AS400 table Product. Every time a new product is added to the AS400 table, we have an audit trail of who did it and when, Hide Copy Code CRUD Creation with Mvcscaffolding We go to the package manager console and call the command Mvcscaf folding to create the relevant CRUD features for the ProductMaster class. First, we install mvcscaffolding by typing install-package mvcscaf folding on the PM> prompt as shown below: ackage micscaffolding Once everything is installed for mvcscaf folding, the prompt will appear again as below with the trail of messages above it Successfully installed ‘Tascatfoléing.Core 1.0.0". Installing 'T4scaffolding 1.0.8". Successfully installed 'TaScaffolding 1.0.8". ‘Installing 'MvcScaffolding 1.@.9". Successfully installed ‘MvcScaffolding 1.0.9". Adding ‘TaScaffolding Core 1.0.0" to SampleProduct Interface. Successfully acded ‘T4Scaffolding.Core 1.0.0° to SampleProductInterface. Adding ‘T4Scaffolding 1.0.2" to SampleProductInterFace. Successfully added ‘T4Scaffolding 1.0.8" to SampleProductinterface. Adding ‘MveScaffelding 1.0.9" to SenplefroductInterfece. Successfully acded ‘MvcScaffolding 1.0.9" to SampleProductinterface. PHD all the following command in the Package Console Manager prompt to create the necessary CRUD views for the ProductMaster classin the Views/ProductMasters folder.Scaffold Controller ProductMaster -Repository -Force. der and Class Creating DAL Fo! Create a DAL folder in the project as follows. This is where classes to access and modify the AS400 tables will be written 4% ProductMastersControllercs b_ #3 ProductMastersController ial bia Filters > iad Images 4 tel Models > #c* AccourtModeln.cs 4 sgl ProduciDataSetxed b-©3A ProductDataSecDesianenes “Then, create a class in DAL folder called UnitO/Workcs or whatever you may prefer. using SampleProductinterface.Models; using Sample?roductInterface.Models.ProductDataSetTableadapters; using Systens using Systen.collections.Generic; using Systen.bata.O1eDb; using System.Lings using System.Web public class UnitofWork « public string HostName, Library, UserName, Password, DBConnectionstrings public static string GetConnectionstring (string host, string Library, string username, string password) t ‘string _sqlConnectionString = string.Format (C’Provider=18H0A400.DataSource.1;ata Source={@};Persist Security Info=True; Password={3};User ID={2);Catalog Library List=(1)", host, library, username, password); return _sqlConnectionString; y public UnitofWork(string environment) { HostNane = "BARBUDA"; UserNane = "MICKY"s Password = "MOUSE"; Library = "RAYALS"; DBConnectionString = GetConnectionstring(HostNane, Library, UserName, Password); y public void WeiteToproduct(ProductMaster prd) { using (var ta = new PRODUCTTableAdapter()) ta.Connection = new OLedbConnection(DaConnectionstring) ; using (var ds = new ProductDataset()) { var productRow = ds.PRODUCT.NewPRODUCTRON() 5 productRow.NAME = prd.ProductNane roductRow.DESCRP = prd.Description; productRow.PRICE = prd.Price; productRow.QTY = prd. Quantity; dds . PRODUCT .AddPRODUCTRow( product Row); try { //insert into as4ee table ‘ta.Update(ds.PRODUCT) ; catch (Exception ex) {‘throw new ApplicationException("Failed to insert Row into PRODUCT File", ex); y In the above code, the WriteToProduct() methad is the main process used to write to the AS400 file. The constructor sets the login credentials for the AS4OO server, with Host, Library, Username and Password, The WriteToProduct() method will ‘then connect to the AS400 using the connection string derived from the credentials initiated in the constructor and write to the dataset (ProductDataSet) from the product master fle (ProductMas ter). The table adapter (PRODUCTTableAdapter) will then execute an insert statement to get the dataset values and write to the PRODUCT table in AS400. Modifying Controller Methoc Inthe controller, the Create method should look like this: [nttpPost] public ActionResult Create(ProductMaster productnaster) t UnitOFWork Uow = new Uni tOFWOrk(); Sf (Modelstete. IsValid) { //call uow to insert into AS408 vow hniteToProduct (productnaster); productnaster.Usernane = Environnent.UserNane; productnaster Datetntered = DateTine. Now; productnasterfepository..InsertOrUpdate(productnaster); productnasterRepository.Save() return RedirectTokction( "Index" ); p else { return View(); > y Inthe above code, we are caling the UnitOfWork method WriteToProduct() which will write to the AS400 table. Then prior to updating the SQL server table ProductMaster, we ensure that the Username and DateEntered fields are populated by assigning the current Windows username and current time (DateT ime. NOW) to ther respective values. So in this instance, we willbe populating 2 tables, one in the AS400 via the method Wr'iteToProduct() and the other using the controller Editt_ method productmasterRepository.InsertOrUpdate (productmaster) and productmasterRepository.Save() Nodifying the cshtml View In the_Layoutcshtmt file (located in Views/Shared), we will also add an extralline to enable access to the Indexshtm page for updating the ProductMaster table, The extra line (third line) shown below is where the ActionLink for ProductMasters will pint to the Indexcshim! page when the tab labelled ‘Products Update’ is selected