Assign ID to each two columns imported from Excel file - c#

I trying to import data from excel and for each two connected column i store the data into two dimensional list except the last column which will be in a 1D list. i wanna assign an id for each set of data the code get to be able to build a dictionary later for them.
that means for the first( valLat and valLng together ) i want to assign id = 1 and so on for each List
for (int i = 2; i <= rowCount; i++)
{
var pickLocation = new PickLocation();
var valLat = ((Excel.Range)wks.Cells[i, PickLocation.ColumnLat]).Value;
var valLng = ((Excel.Range)wks.Cells[i, PickLocation.ColumnLng]).Value;
if (!(valLat == null) & !(valLng == null))
{
pickLocation.Lat = Convert.ToDouble(valLat);
pickLocation.Lng = Convert.ToDouble(valLng);
ListPickLocations.Add(pickLocation);
}
var setLocation = new SetLocation();
valLat = ((Excel.Range)wks.Cells[i, SetLocation.ColumnLat]).Value;
valLng = ((Excel.Range)wks.Cells[i, SetLocation.ColumnLng]).Value;
if (!(valLat == null) & !(valLng == null))
{
setLocation.Lat = Convert.ToDouble(valLat); ;
setLocation.Lng = Convert.ToDouble(valLng);
ListSetlocations.Add(setLocation);
}
var craneLocation = new CraneLocation();
valLat = ((Excel.Range)wks.Cells[i, CraneLocation.ColumnLat]).Value;
valLng = ((Excel.Range)wks.Cells[i, CraneLocation.ColumnLng]).Value;
if (!(valLat == null) & !(valLng == null))
{
craneLocation.Lat = Convert.ToDouble(valLat); ;
craneLocation.Lng = Convert.ToDouble(valLng);
ListCranelocations.Add(craneLocation);
}
var weight = ((Excel.Range)wks.Cells[i, 10]).Value;
if (!(weight == null))
{
Weights.Add(Convert.ToDouble(weight));
}
var clearance = ((Excel.Range)wks.Cells[2, 12]).Value;
if (!(clearance == null))
{
Clearance = clearance;
}
}
}
}
}
}
public class PickLocation
{
public double Lat { get; set; }
public double Lng { get; set; }
public static int ColumnLat { get; } = 4;
public static int ColumnLng { get; } = 5;
}
public class CraneLocation
{
public double Lat { get; set; }
public double Lng { get; set; }
public static int ColumnLat { get; } = 1;
public static int ColumnLng { get; } = 2;
}
public class SetLocation
{
public double Lat { get; set; }
public double Lng { get; set; }
public static int ColumnLat { get; } = 7;
public static int ColumnLng { get; } = 8;
}

Related

Error on Edit Object in entityframework An entity object cannot be referenced by multiple instances of Entity ChangeTracker

I get an error when I call this text in my project. An entity object cannot be referenced by multiple instances of Entity Change Tracker.
public static short Udf_Edit_Invoice(tblInvoice inv)
{
using (DbContextModel db = new DbContextModel())
{
using (DbContextTransaction transaction = db.Database.BeginTransaction())
{
try
{
if (db.Entry<tblInvoice>(inv).State == EntityState.Detached)
{
db.Set<tblInvoice>().Attach(inv);
}
db.Entry<tblInvoice>(inv).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
transaction.Commit();
return 1;
}
catch(Exception ee)
{
transaction.Rollback();
return 0;
}
}
}
}
Click Event: (And you can see the click event as follows, which was called inside a form and the information of the main class and the child class is poured into it, and then the editing command is called, which I encounter an error.)
tblInvoice invtPub = new tblInvoice();
private void Btn_Save_Click(object sender, EventArgs e)
{
if (TxtTax.Text == "")
{
TxtTax.Text = "0";
}
Decimal Dec_TotalPrice = 0;
Decimal Dec_TotalTakhfif = 0;
Entities.Entity.tblInvoiceDetail invdObj;
List<Entities.Entity.tblInvoiceDetail> invd = new List<Entities.Entity.tblInvoiceDetail>();
foreach (DataRow Row in FactDt.Rows)
{
invdObj = new Entities.Entity.tblInvoiceDetail();
Dec_TotalPrice += Decimal.Parse(Row["FoodTotalPrice"].ToString());
Dec_TotalTakhfif += Decimal.Parse(Row["FoodDiscount"].ToString());
invdObj.AssetID = int.Parse(Row["FoodID"].ToString());
invdObj.AssetQuantity = Double.Parse(Row["FoodQuantity"].ToString());
invdObj.Discount = Double.Parse(Row["FoodDiscount"].ToString());
invdObj.UnitPrice = Double.Parse(Row["FoodUnitPrice"].ToString());
invdObj.InvoiceID = 0;
invdObj.TotalPrice = Double.Parse(Row["FoodTotalPrice"].ToString());
invd.Add(invdObj);
}
invtPub.CreatorUserID = BLL.Class_GlobalVars.ThisPUserID;
invtPub.CustomerID = long.Parse(Txt_CustomerCode.Text.Trim());
invtPub.DateOfFactor = DateTime.Now;
invtPub.InvoiceTitleID = 0;
invtPub.IsCanceled = false;
invtPub.PayCardOfInvoice = Decimal.Parse(Txt_Card.Text.Trim());
invtPub.PayCashOfInvoice = Decimal.Parse(Txt_Cach.Text.Trim());
invtPub.PayCredOfInvoice = (Decimal.Parse(Txt_Debit.Text.Trim()) < 0 ? Decimal.Parse(Txt_Debit.Text.Trim()) : 0);
invtPub.PayDebtOfInvoice = (Decimal.Parse(Txt_Debit.Text.Trim()) >= 0 ? Decimal.Parse(Txt_Debit.Text.Trim()) : 0);
invtPub.ShamsiDateOfFactor = ShamsiTools.Class_SHamsiTools.UDF_MiladiDateToShmasi(DateTime.Now);
invtPub.StrDescription = Txt_Description.Text.Trim();
invtPub.TaxOfInvoice = Decimal.Parse(TxtTax.Text.Trim());
invtPub.TotalPriceOfFactor = Dec_TotalPrice;
invtPub.TotalPriceOfFactorWithTax = (Decimal.Parse(TxtTax.Text) + Dec_TotalPrice);
invtPub.tblInvoiceDetail = invd;
if (Entities.Classes.Class_tblInvoice.Udf_Edit_Invoice(invtPub) == 1)
{
Lbl_Total_Aglam.Text = "0";
Lbl_Total_Price.Text = "0";
Lbl_Total_Takhfif.Text = "0";
GrPr_Mohasebeh.Enabled = false;
Grpr_Custumer.Enabled = true;
Grpr_Factor.Enabled = true;
FactDt.Rows.Clear();
for (int i = FactDt.Rows.Count - 1; i >= 0; i--)
{
DataRow dr = FactDt.Rows[i];
{
dr.Delete();
}
}
Grd_Factor.DataSource = FactDt;
MessageBox.Show("Edited . . . . . .");
UDF_TXTFactorClear();
UDF_TXTCustomerClear();
}
else
{
}
}
and total error message:
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
"EntityFramework"
at System.Data.Entity.Core.Objects.ObjectContext.VerifyContextForAddOrAttach(IEntityWrapper wrappedEntity)
at System.Data.Entity.Core.Objects.ObjectContext.AttachSingleObject(IEntityWrapper wrappedEntity, EntitySet entitySet)
at System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
at System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClassa.<Attach>b__9()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)
at System.Data.Entity.Internal.InternalEntityEntry.set_State(EntityState value)
at System.Data.Entity.Infrastructure.DbEntityEntry`1.set_State(EntityState value)
at Entities.Classes.Class_tblInvoice.Udf_Edit_Invoice(tblInvoice inv) in E:\Programming\P1\ADVD9\TarkeEtiad\SourceCodeEF\Entities\Classes\Class_tblInvoice.cs:line 77
This: tblInvoice invtPub = new tblInvoice();
[Table("tblInvoice")]
public partial class tblInvoice
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public tblInvoice()
{
tblInvoiceDetail = new HashSet<tblInvoiceDetail>();
}
[Key]
public long InvoiceTitleID { get; set; }
[Required]
public Boolean IsCanceled { get; set; }
[Required]
public int CreatorUserID { get; set; }
[Required]
public long CustomerID { get; set; }
public DateTime DateOfFactor { get; set; }
[Required]
[StringLength(10)]
public string ShamsiDateOfFactor { get; set; }
public decimal? TotalPriceOfFactor { get; set; }
public decimal TaxOfInvoice { get; set; }
public decimal TotalPriceOfFactorWithTax { get; set; }
public decimal PayCashOfInvoice { get; set; } //نقدی
public decimal PayCardOfInvoice { get; set; } //کارتخوان
public decimal PayDebtOfInvoice { get; set; } //بدهی
public decimal PayCredOfInvoice { get; set; } //طلب
[MaxLength(1500)]
public String StrDescription { get; set; }
public virtual tblCustomers tblCustomers { get; set; }
public virtual tblPUsers tblPUsers { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tblInvoiceDetail> tblInvoiceDetail { get; set; }
}
Don't use any transactions since you only need to add one record.
try this
using (DbContextModel db = new DbContextModel())
{
try
{
var existedInvoice= db.Set<tblInvoice>().FirstOrDefault(i=> i.InvoiceTitleID = inv.InvoiceTitleID );
if (existedInvoice!=null)
{
db.Entry(existedInvoice).CurrentValues.SetValues(inv);
db.SaveChanges();
return 1;
} else return 0;
}
catch(Exception ee)
{
return 0;
}
}

Calculate Price Method Not Working In Visual Studio

So I've made this form that acts as a cake shop. I have everything working perfectly except for the price. For some reason it isn't displaying properly and shows "$22.60" every time. I'm thinking there might be something wrong with the method
public virtual double CalculateCakeCost()
{
return CAKE_PRICE + (LAYER_PRICE * NumOfLayers);
}
from class "Cake" since it seems to return the cake price, but not add it up with the bracket values. The calculate cost with tax method from the class "CustomCake" also seems to be working fine. You can read the rest of the code is down below and please tell me if there is a problem, because I don't see anything wrong.
namespace Lab_OrderCake_The_Bakery_
{
public partial class frmOrderCake : Form
{
Cake objcake;
CustomCake objcustcake;
Customer objcustomer;
Order objorder;
public frmOrderCake()
{
InitializeComponent();
}
private void btnOrder_Click(object sender, EventArgs e)
{
//flavour
if (radVanilla.Checked == true)
{
txtRadFlavour.Text = "Vanilla";
}
if (radChocolate.Checked == true)
{
txtRadFlavour.Text = "Chocolate";
}
if (radBanana.Checked == true)
{
txtRadFlavour.Text = "Banana";
}
if (radLemonBerry.Checked == true)
{
txtRadFlavour.Text = "Lemon banana";
}
//layers
if (rad1layer.Checked == true)
{
numRadLayers.Value = 1;
}
if (rad2layers.Checked == true)
{
numRadLayers.Value = 2;
}
if (rad3layers.Checked == true)
{
numRadLayers.Value = 3;
}
if (rad4layers.Checked == true)
{
numRadLayers.Value = 4;
}
//occassion
if (radAnniversary.Checked == true)
{
txtRadOcc.Text = "Anniversary";
}
if (radBirthday.Checked == true)
{
txtRadOcc.Text = "Birthday";
}
if (radRetirement.Checked == true)
{
txtRadOcc.Text = "Retirement";
}
if (radWedding.Checked == true)
{
txtRadOcc.Text = "Wedding";
}
//size
if (rad6inch.Checked == true)
{
numRadSize.Value = 6;
}
if (rad8inch.Checked == true)
{
numRadSize.Value = 8;
}
if (rad10inch.Checked == true)
{
numRadSize.Value = 10;
}
if (rad12inch.Checked == true)
{
numRadSize.Value = 12;
}
//design
if (radPolka.Checked == true)
{
txtRadDesign.Text = "Polka Dots";
}
if (rad8inch.Checked == true)
{
txtRadDesign.Text = "Edible Images";
}
if (rad10inch.Checked == true)
{
txtRadDesign.Text = "Fondant Bow";
}
if (rad12inch.Checked == true)
{
txtRadDesign.Text = "3D Figures";
}
objcake = new Cake(txtRadFlavour.Text, (int)numRadLayers.Value);
objcustomer = new Customer(txtFName.Text, txtLName.Text);
objcustcake = new CustomCake(txtRadFlavour.Text, (int)numRadLayers.Value, txtRadOcc.Text,
(int)numRadSize.Value, txtRadDesign.Text);
objorder = new Order();
lblOutOrder.Text = objcustomer.ToString() + objcustcake.ToString() + objorder.ToString();
}
}
namespace CakeClasses
{
public class Cake
{
public int NumOfLayers { get; set; }
public string Flavour { get; set; }
public double Price { get; set; }
public const double CAKE_PRICE = 20;
public const int LAYER_PRICE = 3;
public Cake()
{
Flavour = "";
NumOfLayers = 0;
}
public Cake(string flavour, int numLayers)
{
NumOfLayers = numLayers;
Flavour = flavour;
}
**public virtual double CalculateCakeCost()
{
return CAKE_PRICE + (LAYER_PRICE * NumOfLayers);
}**
public override string ToString()
{
return " " + Flavour + " flavoured cake with " + NumOfLayers + " layer(s)";
}
}
}
namespace CakeClasses
{
public class Order
{
public Customer Customer { get; set; }
public Cake Cake { get; set; }
public int NumOfCakes { get; set; }
public Order()
{
Customer = new Customer();
Cake = new Cake();
NumOfCakes = 1;
}
public Order(string fName, string lName, string flavour, int numLayers, string occasion, int
diameter, string design)
{
Customer = new Customer(fName, lName);
Cake = new CustomCake(flavour, numLayers, occasion,diameter,design);
NumOfCakes = 1;
}
public Order(string fName, string lName, string flavour, int numLayers)
{
Customer = new Customer(fName, lName);
Cake = new Cake(flavour, numLayers);
NumOfCakes = 1;
}
public double CalculateCostWithTax()
{
return Cake.CalculateCakeCost() * 1.13;
}
public override string ToString()
{
return "for the total cost of " + CalculateCostWithTax().ToString("C");
}
}
}
namespace CakeClasses
{
public class CustomCake : Cake
{
public string Occasion { get; set; }
public int Size { get; set; }
public string Design { get; set; }
private double DesignCost { get; set; }
public CustomCake(string flavour, int numLayers,string occasion, int diameter, string design)
:base(flavour,numLayers)
{
Occasion = occasion;
Size = diameter;
Design = design;
switch (Design)
{
case "Polka Dots":
DesignCost = 5;
break;
case "Edible Images":
DesignCost = 12;
break;
case "Fondant Bow":
DesignCost = 10;
break;
default:
DesignCost = 15;
break;
}
}
public override double CalculateCakeCost()
{
return base.CalculateCakeCost() + Size + DesignCost;
}
public override string ToString()
{
return base.ToString() + " with " + Design + " design for " + Occasion + " occassion and
size is " + Size + " inches " ;
}
}
}
Try replacing following line in your code:
objorder = new Order();
with:
objorder = new Order("First Name","Last Name",txtRadFlavour.Text, (int)numRadLayers.Value);

c# linq query extract repeating data types

I have a list of raw data, where always 8 data values are available for one timestamp Date_Time. I would like to use linq to sort the values of Data_Value column by the Data_Type into one row for each Date_Time.
Rawdata table
I have the following class and would like to return it as a list of that class.
public class MeasurementData
{
public MeasurementData();
public int Test_ID { get; set; }
public int Channel { get; set; }
public string Date_Time { get; set; }
public double Current { get; set; }
public double Voltage { get; set; }
public double Charge_Capacity { get; set; }
public double Discharge_Capacity { get; set; }
}
Here is a reduced form of the code, where i just try to extract four values.
public static List<DataStructure.MeasurementData> RawResult(List<DataStructure.MeasurementRawTableSQL> rawData, int _Test_ID)
{
if (rawData != null)
{
var result = rawData.GroupBy(x => x.Date_Time)
.Select(gr =>
{
var _Date_Time = TicksToDate(gr.FirstOrDefault().Date_Time);
var _Channel = gr.FirstOrDefault().Channel;
var _Voltage = gr.Where(x => x.Data_Type == 21).FirstOrDefault().Data_Value;
var _Current = gr.Where(x => x.Data_Type == 22).FirstOrDefault().Data_Value;
var _Charge_Capacity = gr.Where(x => x.Data_Type == 23).FirstOrDefault().Data_Value;
var _Discharge_Capacity = gr.Where(x => x.Data_Type == 24).FirstOrDefault().Data_Value;
return new DataStructure.MeasurementData
{
Test_ID = _Test_ID,
Channel = _Channel,
Date_Time = _Date_Time,
Current = _Current,
Voltage = _Voltage,
Charge_Capacity = _Charge_Capacity,
Discharge_Capacity = _Discharge_Capacity
};
}
).ToList();
return result;
}
else return null;
}
This is partially working, for the case 21 and 22 it gives me proper values, whereas I get an error "Object reference not set to an instance of an object" for the case 23 and 24. On the other hand the database has this rows for every single datapoint and is never null. If I only select First() instead of FirstOrDefault() I get an "sequence contains no elements".
I am really stuck right now and would really appriciate your help.
Below is the cleanest way of solving issue. I cannot explain why you are not getting a Data_Type for each time value, but code below should get around the exceptions. I used decimal? as the type. The exception is caused when the WHERE returns null you cannot get the property Data_Value. So you have to test for null like code below :
class Program
{
static void Main(string[] args)
{
}
//public DataStructures DataStructure = new DataStructures();
public static List<DataStructure.MeasurementData> RawResult(List<DataStructure.MeasurementRawTableSQL> rawData, int _Test_ID)
{
if (rawData != null)
{
double? _Voltage = null;
double? _Current = null;
double? _Charge_Capacity = null;
double? _Discharge_Capacity = null;
var result = rawData.GroupBy(x => x.Date_Time)
.Select(gr =>
{
var _Date_Time = gr.Key;
var _Channel = gr.FirstOrDefault().Channel;
var _Voltage_Row = gr.Where(x => x.Data_Type == 21).FirstOrDefault();
if(_Voltage_Row != null) _Voltage = _Voltage_Row.Data_Value;
var _Current_Row = gr.Where(x => x.Data_Type == 22).FirstOrDefault();
if(_Current_Row != null) _Current = _Current_Row.Data_Value;
var _Charge_Capacity_Row = gr.Where(x => x.Data_Type == 23).FirstOrDefault();
if (_Charge_Capacity_Row != null) _Charge_Capacity = _Charge_Capacity_Row.Data_Value;
var _Discharge_Capacity_Row = gr.Where(x => x.Data_Type == 24).FirstOrDefault();
if (_Discharge_Capacity_Row != null) _Discharge_Capacity = _Discharge_Capacity_Row.Data_Value;
return new DataStructure.MeasurementData
{
Test_ID = _Test_ID,
Channel = _Channel,
Date_Time = _Date_Time,
Current = _Current,
Voltage = _Voltage,
Charge_Capacity = _Charge_Capacity,
Discharge_Capacity = _Discharge_Capacity
};
}
).ToList();
return result;
}
else return null;
}
}
public class DataStructure
{
public class MeasurementData
{
public int? Test_ID { get; set; }
public int? Channel { get; set; }
public DateTime Date_Time { get; set; }
public double? Current { get; set; }
public double? Voltage { get; set; }
public double? Charge_Capacity { get; set; }
public double? Discharge_Capacity { get; set; }
}
public class MeasurementRawTableSQL
{
public DateTime Date_Time { get; set; }
public int Channel { get; set; }
public int Data_Type { get; set; }
public double Data_Value { get; set; }
}
}

powershell cmdlet in visual studio 2017, deserialization method

I have got a problem with undestanding powershell cmdlet.
Task is, to deserialize a xml file. I have got a folder with a lot of xml files, I would like to read in an filter certain information using the deserializiation method. My supervisor mentioned that I should use powershell cmdlet... but I haven't heard anything of it before. So what is the benefit? And how do I create such a cmdlet.
That is my code so far:
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
namespace EdocX
{
[Cmdlet(VerbsCommon.Get, "test", SupportsShouldProcess = true)]
public class program : Cmdlet
{
public static string dirPath = #"N:\EGS_SDRE\OB\19_Schnittstellen_eDocX_ITAS\06_Matching EdocX-Objektbrief\01_exemplarische_XMLs_fuer_den_Import\RWA-Anlage"
public static List<string> dirs = new List<string>(Directory.GetFiles(dirPath, "*.xml"));
public static (string[], string[], string[,], string[,], string[,]) deserializeobject(string filename)
{
// Liste Prüfgrundlagen
List<string> pg = new List<string>();
pg.Add("GaVO");
pg.Add("VkVO");
pg.Add("VStättVO");
pg.Add("SPrüfV");
pg.Add("BetrVO");
pg.Add("BbgSGPrüfV");
pg.Add("BremAnlPrüfV");
pg.Add("PVO");
pg.Add("TPrüfVO");
pg.Add("BauPrüfVO M-V");
pg.Add("DVO-NBauO");
pg.Add("PrüfVO NRW");
pg.Add("HTechAnlV RP");
pg.Add("TPrüfVO");
pg.Add("SächsTechPrüfVO");
pg.Add("TAnlVO");
pg.Add("PrüfVO");
pg.Add("ThürTechPrüfVO");
// Auftraggeber - Var
string[] Auftraggeber;
string Name_AG = "";
string Stadt_AG = "";
string PLZ_AG = "";
string Straße_AG = "";
string Hausnummer_AG = "";
string Region_AG = "";
string PARNR_AG = "";
// Aufstellungsort - Var
string[] Aufstellungsort;
string Name_AO = "";
string Stadt_AO = "";
string PLZ_AO = "";
string Straße_AO = "";
string Hausnummer_AO = "";
string Region_AO = "";
string Land_AO = "";
// Anlage - Var
string[,] Anlagen;
string Materialnummer = "";
string EQnummer = "";
string Anlagentyp = "RWA-Anlage";
string[] InterneBezeichnung;
string[] Hersteller;
// Vorgang rückgemeldet - Var
string[,] Vorgang_rückgemeldet;
string Typ_VR = "Prüfung";
string Dienstleister = "TÜV SÜD Industrie Service GmbH";
string Durchführungsdatum = "";
string Maengel = "";
string Bemerkungstexte = "";
string Pruefgrundlage_VR = "";
// Vorgang zukünftig - Var
string[,] Vorgang_zukünftig;
string Typ_VZ = "Prüfung";
string Fälligkeit = "";
string Pruefgrundlage_VZ = "";
//****************************************************************************************************
//********************************************SERIALIZATION*******************************************
// new instance of XMLSerializer --> specifiying type
var serializer = new XmlSerializer(typeof(Document));
// read the XML document
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var encoding = Encoding.GetEncoding("Windows-1252");
using (var sr = new StreamReader(filename, encoding, true))
using (var reader = XmlReader.Create(sr))
{
// restore the object's state using the deserialize method
var i = (Document)serializer.Deserialize(reader);
//---------------------------------------Auftraggeber-----------------------------------------------
int count_Z1ZRMPA = i.IDOC.Z1ZRMDB.Z1ZRMPA.Count;
for (int c = 0; c < count_Z1ZRMPA; c++)
{
if (i.IDOC.Z1ZRMDB.Z1ZRMPA[c].TITLE == "Auftraggeber")
{
Name_AG = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].NAME1 + i.IDOC.Z1ZRMDB.Z1ZRMPA[c].NAME2;
Stadt_AG = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].CITY1;
PLZ_AG = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].POST_CODE1;
Straße_AG = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].STREET;
Hausnummer_AG = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].HOUSE_NUM1;
Region_AG = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].REGION;
PARNR_AG = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].PARNR;
}
//---------------------------------------Aufstellungsort----------------------------------------
if (i.IDOC.Z1ZRMDB.Z1ZRMPA[c].TITLE == "Aufstellungsort")
{
Name_AO = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].NAME1 + i.IDOC.Z1ZRMDB.Z1ZRMPA[c].NAME2;
Stadt_AO = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].CITY1;
PLZ_AO = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].POST_CODE1;
Straße_AO = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].STREET;
Hausnummer_AO = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].HOUSE_NUM1;
Region_AO = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].REGION;
Land_AO = i.IDOC.Z1ZRMDB.Z1ZRMPA[c].COUNTRY;
}
}
//---------------------------------------Anlage----------------------------------------------------
Materialnummer = i.IDOC.Z1ZRMDB.MABEZ;
EQnummer = i.IDOC.Z1ZRMDB.OBJNR;
int count_ANLAGEPRFTXT = i.IDOC.Z1ZRMDB.PRFBER.PRFERG.ANLAGE.PRFTXT.Count;
int AnzahlAnlagen = count_ANLAGEPRFTXT - 1;
InterneBezeichnung = new string[AnzahlAnlagen];
Hersteller = new string[AnzahlAnlagen];
for (int c = 1; c < count_ANLAGEPRFTXT; c++)
{
InterneBezeichnung[c - 1] = i.IDOC.Z1ZRMDB.PRFBER.PRFERG.ANLAGE.PRFTXT[c].ENTRBEREICH;
Hersteller[c - 1] = i.IDOC.Z1ZRMDB.PRFBER.PRFERG.ANLAGE.PRFTXT[c].HERSTELLER;
}
//---------------------------------------Vorgang rückgemeldet-------------------------------------
Durchführungsdatum = i.IDOC.Z1ZRMDB.PRFBER.DATEN.Pruefungszeitraum;
int count_PRFTXT = i.IDOC.Z1ZRMDB.PRFBER.PRFERG.PRFTXT.Count;
for (int c = 0; c < count_PRFTXT; c++)
{
if (i.IDOC.Z1ZRMDB.PRFBER.PRFERG.PRFTXT[c].KATEGORIE == "Grundlage")
{
string Pruefgrundlage = i.IDOC.Z1ZRMDB.PRFBER.PRFERG.PRFTXT[c].Value;
int count_pg = pg.Count;
for (int cc = 0; cc < count_pg; cc++)
{
if (Pruefgrundlage.Contains(pg[cc]) == true)
{
Pruefgrundlage_VR = pg[cc];
}
}
}
if (i.IDOC.Z1ZRMDB.PRFBER.PRFERG.PRFTXT[c].ROWTYPE == "Zwischenüberschrift" && i.IDOC.Z1ZRMDB.PRFBER.PRFERG.PRFTXT[c].PATTERN == "#KATEGORIE[../#KATEGORIE='Mangel' and ../#GEWICHT[not(contains(.,'Beseitigt'))]]")
{
string Maengelabruf = i.IDOC.Z1ZRMDB.PRFBER.PRFERG.PRFTXT[c].Value;
if (Maengelabruf == "Mängel")
{
Maengel = "Mängel_ja";
}
else
{
Maengel = "Mängel_nein";
}
}
if (i.IDOC.Z1ZRMDB.PRFBER.PRFERG.PRFTXT[c].ROWTYPE == "Mangel" && i.IDOC.Z1ZRMDB.PRFBER.PRFERG.PRFTXT[c].PATTERN == "#KATEGORIE[../#KATEGORIE='Mangel' and ../#GEWICHT[not(contains(.,'Beseitigt'))]]")
{
string Bemerkungstext = i.IDOC.Z1ZRMDB.PRFBER.PRFERG.PRFTXT[c].Value;
Bemerkungstexte += " " + Bemerkungstext;
}
}
//---------------------------------------Vorgang zukünftig--------------------------------------
int count_Z1ZRMAU = i.IDOC.Z1ZRMDB.Z1ZRMAU.Count;
for (int c = 0; c < count_Z1ZRMAU; c++)
{
if (i.IDOC.Z1ZRMDB.Z1ZRMAU[c].MNAME == "Nächste Wiederkehrende Prüfung")
{
Fälligkeit = i.IDOC.Z1ZRMDB.Z1ZRMAU[c].MWERT;
}
}
for (int c = 0; c < count_PRFTXT; c++)
{
if (i.IDOC.Z1ZRMDB.PRFBER.PRFERG.PRFTXT[c].KATEGORIE == "Grundlage")
{
string Pruefgrundlage = i.IDOC.Z1ZRMDB.PRFBER.PRFERG.PRFTXT[c].Value;
int count_pg = pg.Count;
for (int cc = 0; cc < count_pg; cc++)
{
if (Pruefgrundlage.Contains(pg[cc]) == true)
{
Pruefgrundlage_VZ = pg[cc];
}
}
}
}
Auftraggeber = new string[7];
Aufstellungsort = new string[7];
Anlagen = new string[AnzahlAnlagen, 5];
Vorgang_rückgemeldet = new string[AnzahlAnlagen, 6];
Vorgang_zukünftig = new string[AnzahlAnlagen, 3];
}
Auftraggeber[0] = Name_AG;
Auftraggeber[1] = Stadt_AG;
Auftraggeber[2] = PLZ_AG;
Auftraggeber[3] = Straße_AG;
Auftraggeber[4] = Hausnummer_AG;
Auftraggeber[5] = Region_AG;
Auftraggeber[6] = PARNR_AG;
Aufstellungsort[0] = Name_AO;
Aufstellungsort[1] = Stadt_AO;
Aufstellungsort[2] = PLZ_AO;
Aufstellungsort[3] = Straße_AO;
Aufstellungsort[4] = Hausnummer_AO;
Aufstellungsort[5] = Region_AO;
Aufstellungsort[6] = Land_AO;
for (int j = 0; j < Anlagen.GetLength(0); j++)
{
Anlagen[j, 0] = Materialnummer;
Anlagen[j, 1] = EQnummer;
Anlagen[j, 2] = Anlagentyp;
Anlagen[j, 3] = InterneBezeichnung[j];
Anlagen[j, 4] = Hersteller[j];
Vorgang_rückgemeldet[j, 0] = Typ_VR;
Vorgang_rückgemeldet[j, 1] = Dienstleister;
Vorgang_rückgemeldet[j, 2] = Durchführungsdatum;
Vorgang_rückgemeldet[j, 3] = Maengel;
Vorgang_rückgemeldet[j, 4] = Bemerkungstexte;
Vorgang_rückgemeldet[j, 5] = Pruefgrundlage_VR;
Vorgang_zukünftig[j, 0] = Typ_VZ;
Vorgang_zukünftig[j, 1] = Fälligkeit;
Vorgang_zukünftig[j, 2] = Pruefgrundlage_VZ;
}
return (Auftraggeber, Aufstellungsort, Anlagen, Vorgang_rückgemeldet, Vorgang_zukünftig);
}
}
}
And here the class to simulate the xml structure:
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace EdocX
{//ZRM01 Root Environment
[Serializable()]
[XmlRoot(ElementName = "ZRM01")]
public partial class Document
{
[XmlElement()]
public ZRM01IDOC IDOC { get; set; }
}
//IDOC
[Serializable()]
public partial class ZRM01IDOC
{
[XmlElement()]
public ZRM01IDOCZ1ZRMDB Z1ZRMDB { get; set; }
}
//IDOC Elements
[Serializable()]
public partial class ZRM01IDOCZ1ZRMDB
{
public string MABEZ { get; set; } // Materialnummer
public string OBJNR { get; set; } // Equipmentnummer
[XmlElement("Z1ZRMPA")]
public List<ZRM01IDOCZ1ZRMDBZ1ZRMPA> Z1ZRMPA = new List<ZRM01IDOCZ1ZRMDBZ1ZRMPA>(); //{ get; set; }
[XmlElement("Z1ZRMAU")]
public List<ZRM01IDOCZ1ZRMDBZ1ZRMAU> Z1ZRMAU = new List<ZRM01IDOCZ1ZRMDBZ1ZRMAU>();
[XmlElement()]
public ZRM01IDOCZ1ZRMDBPRFBER PRFBER { get; set; }
}
//Z1ZRMPA --> Mandant / Gebäude
[Serializable()]
public partial class ZRM01IDOCZ1ZRMDBZ1ZRMPA
{
[XmlAttribute()]
public string TITLE { get; set; } // "Auftraggeber" "Aufstellungsort"
[XmlElement()]
public string PARNR { get; set; } // Parnr. (Mandant)
[XmlElement()]
public string NAME1 { get; set; } // Name
[XmlElement()]
public string NAME2 { get; set; } // Name
[XmlElement()]
public string CITY1 { get; set; } // Stadt
[XmlElement()]
public string POST_CODE1 { get; set; } // PLZ
[XmlElement()]
public string STREET { get; set; } // Straße
[XmlElement()]
public string HOUSE_NUM1 { get; set; } // Hausnummer
[XmlElement()]
public string REGION { get; set; } // Region
[XmlElement()]
public string COUNTRY { get; set; } // Land
}
//Z1ZRMAU
[Serializable()]
public partial class ZRM01IDOCZ1ZRMDBZ1ZRMAU
{
[XmlElement()]
public string MNAME { get; set; } // "Nächste Wiederkehrende Prüfung"
[XmlElement()]
public string MWERT { get; set; } // Fälligkeit
}
//PRFBER Elemts
[Serializable()]
public partial class ZRM01IDOCZ1ZRMDBPRFBER
{
[XmlElement()]
public ZRM01IDOCZ1ZRMDBPRFBERPRFERG PRFERG { get; set; }
[XmlElement()]
public ZRM01IDOCZ1ZRMDBPRFBERDATEN DATEN { get; set; }
}
//DATEN
[Serializable()]
public partial class ZRM01IDOCZ1ZRMDBPRFBERDATEN
{
[XmlElement()]
public string Pruefungszeitraum { get; set; } //Durchführungsdatum
}
//PRFERG Elements
[Serializable()]
public partial class ZRM01IDOCZ1ZRMDBPRFBERPRFERG
{
[XmlElement()]
public ZRM01IDOCZ1ZRMDBPRFBERPRFERGANLAGE ANLAGE { get; set; }
[XmlElement("PRFTXT")]
public List<ZRM01IDOCZ1ZRMDBPRFBERPRFERGPRFTXT> PRFTXT = new List<ZRM01IDOCZ1ZRMDBPRFBERPRFERGPRFTXT>();
}
//ANLAGE
[Serializable()]
public partial class ZRM01IDOCZ1ZRMDBPRFBERPRFERGANLAGE
{
[XmlElement("PRFTXT")]
public List<ZRM01IDOCZ1ZRMDBPRFBERPRFERGANLAGEPRFTXT> PRFTXT = new List<ZRM01IDOCZ1ZRMDBPRFBERPRFERGANLAGEPRFTXT>();
}
//PRFTXT
[Serializable()]
public partial class ZRM01IDOCZ1ZRMDBPRFBERPRFERGANLAGEPRFTXT
{
[XmlAttribute()]
public string REF { get; set; } // Anzahl Anlagen
[XmlAttribute()]
public string HERSTELLER { get; set; } // Hersteller Anlage
[XmlAttribute()]
public string ENTRBEREICH { get; set; } // Interne Bezeichnung Anlage
}
//PRFTXT
[Serializable()]
public partial class ZRM01IDOCZ1ZRMDBPRFBERPRFERGPRFTXT
{
[XmlAttribute()]
public string KATEGORIE { get; set; } // A"Grundlage" < ... >
[XmlText()]
public string Value { get; set; }
[XmlAttribute()]
public string ROWTYPE { get; set; } // "Zwischenüberschrift" (für Mangel ja/nein) "Mangel" (für Bemerkungstext)
[XmlAttribute()]
public string PATTERN { get; set; } // "#KATEGORIE[../#KATEGORIE='Mangel' and ../#GEWICHT[not(contains(.,'Beseitigt'))]]" < Mängel > bzw < ... >
}
}
I'm very thankful for help!

How to use Collection in loop

Small doubt, In Update method I had int count, let say count == 4, then i am using for loop to get id, version and set.
In this case Id,version and set values getting only the last value, but how to get the all the values
I tried but i feel its wrong and not working,
Created a seperate list for id, version and set,
eg: _details.imageList.Add(logoHeader.LogoID);
public void Updates(AUnit _aUnit, int Id)
{
ImageDetails _details = new ImageDetails(_aUnit, Id);
int count = (int) _aUnit.ReadBits(8);
for (int i = 0; i < (int) count; i++)
{
_details.ID = (int) _aUnit.ReadBits(8);
_details.Version = (int) _aUnit.ReadBits(8);
_details.set = (int) _aUnit.ReadBits(24);
}
_details.Rset = _aUnit.Buffer.Skip(10).Take(_details.set).ToArray();
//MemoryStream ms = new MemoryStream(_details.PortrateImages.First());
//Image image = Image.FromStream(ms);
//Bitmap bmp = new Bitmap(image);
_details.UpdateTime = DateTime.Now.ToString("h:mm:ss tt");
newData.Add(_details);
}
public class ImageDetails
{
public ImageDetails(AUnit _au, int carouselId)
{
carId = carouselId;
_AUnit = _au;
_updateTime = "";
}
private string _updateTime;
public int ID { get; set; }
public int Version { get; set; }
public int set { get; set; }
public int carId { get; set; }
public byte[] Rset { get; set; }
public AUnit _AUnit { get; set; }
public byte[] bytes { get; set; }
public List<byte[]> dataArray = new List<byte[]>();
public string UpdateTime
{
get { return _updateTime; }
set { _updateTime = value; }
}
public List<byte[]> PImages
{
get
{
List<byte[]> Plogos = new List<byte[]>();
if (carId == 2)
{
Plogos.Add(Rset);
}
return Plogos;
}
}
public List<byte[]> LImages
{
get
{
List<byte[]> Llogos = new List<byte[]>();
if (carId == 1)
{
Llogos.Add(Rset);
}
return Llogos;
}
}
}
You could make detail an object(class) and make a list of details.
like,
public class Detail
{
public int Id{get;set;}
public int Version{get;set;}
public int set{get;set;}
}
and make a list of detail like,
var Details = new List<Detail>();
and you could access it using the foreach loop. like,
foreach(var detail in Details){
Console.log(detail.Id)
.....
}
hope this helps.

Categories

Resources