stucker1998 stucker1998 - 1 month ago 8
ASP.NET (C#) Question

Loops in an array for iterate data fields

what should i do with this task, Im so stuck with this.

protected void Button1_Click(object sender, EventArgs e){

string FileExtension = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
if (FileExtension == ".csv" || FileExtension == ".xls" || FileExtension == ".xlsx")
{

string filename = Path.Combine(Server.MapPath("~/UploadExcels"), new Guid() + ".xlsx");
try
{
FileUpload1.PostedFile.SaveAs(filename);
string excelConnectionString = string.Empty;
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
filename + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";

if (FileExtension == ".xls")
{
excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
filename + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (FileExtension == ".xlsx")
{
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
filename + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}

string[] Lines = File.ReadAllLines(filename);

string[] Fields;
Lines = Lines.Skip(1).ToArray();

Dictionary<string, Int32> emlist = new Dictionary<string, int>();

List<TaskExport> emList = new List<TaskExport>();
foreach (var line in Lines)
{
Fields = line.Split(new char[] { ';' });

emList.Add(

new TaskExport
{
Plant = Fields[0],
Prod__Number = Fields[1],
Order_Number = Fields[2],
Vehicle_Number = Fields[3],
Delivery_Number = Fields[4],
Vehicle_Type = Fields[5],
Engine_Type = Fields[6],
Plant_Dispatch_Date = Fields[7],
Interior = Fields[8],
Paint = Fields[9],
Model = Fields[10],
Country_Name = Fields[11],
Engine_Number = Fields[12],
Sample_Digit = Fields[13],
Number_Of_Code = Fields[14],
Codes = Fields[15],
Number_Of_FZEG = Fields[215],
Country_Code = Fields[216]
});





using (ExportExcelEntities table = new ExportExcelEntities())
{
foreach (var db in emList)
{
var dt = table.TaskExports.Where(s => s.ID.Equals(db.ID)).FirstOrDefault();
if (dt != null)
{
dt.Plant = db.Plant;
dt.Prod__Number = db.Prod__Number;
dt.Order_Number = db.Prod__Number;
dt.Vehicle_Number = db.Vehicle_Number;
dt.Delivery_Number = db.Delivery_Number;
dt.Vehicle_Type = db.Vehicle_Type;
dt.Engine_Type = db.Engine_Type;
dt.Plant_Dispatch_Date = db.Plant_Dispatch_Date;
dt.Interior = db.Interior;
dt.Paint = db.Paint;
dt.Model = db.Model;
dt.Country_Name = db.Country_Name;
dt.Engine_Number = db.Engine_Number;
dt.Sample_Digit = db.Sample_Digit;
dt.Number_Of_Code = db.Number_Of_Code;
dt.Codes = db.Codes;
dt.Number_Of_FZEG = db.Number_Of_FZEG;
dt.Country_Code = db.Country_Code;
table.TaskExports.Add(db);
}
else
{
table.TaskExports.Add(db);
}
}
table.SaveChanges();
Label1.Text = "Data has been Exported";
}
}
}
catch (Exception ex)
{
Label1.Text = "cant save this data" + ex;
}
}
}
}
}


All i must to do is inserting loop to iterate this "codes = fields[15]"

List<TaskExport> emList = new List<TaskExport>();
foreach (var line in Lines)
{
Fields = line.Split(new char[] { ';' });

emList.Add(

new TaskExport
{
Plant = Fields[0],
Prod__Number = Fields[1],
Order_Number = Fields[2],
Vehicle_Number = Fields[3],
Delivery_Number = Fields[4],
Vehicle_Type = Fields[5],
Engine_Type = Fields[6],
Plant_Dispatch_Date = Fields[7],
Interior = Fields[8],
Paint = Fields[9],
Model = Fields[10],
Country_Name = Fields[11],
Engine_Number = Fields[12],
Sample_Digit = Fields[13],
Number_Of_Code = Fields[14],
Codes = Fields[15],
Number_Of_FZEG = Fields[215],
Country_Code = Fields[216]
});


I have to loop that "codes" field until the fields[15] reach to fields[214]..
somebody please help me with this problem -_-???

Answer

First you need to get all attributes of class in order they are declared.

I hope this answer will help you to do so.

Then loop over your property list (assume it's in props variable) and set values.

                    List<TaskExport> emList = new List<TaskExport>();
                    foreach (var line in Lines)
                    {
                        Fields = line.Split(new char[] { ';' });
                        TaskExport taskExport = new TaskExport();
                        for(int i = 0; i < props.Length; i++)
                        {
                            prop[i].SetValue(taskExport, Convert.ChangeType(Fields[i], propertyInfo.PropertyType), null);
                        }
                        emList.Add(taskExport);
                   }