Click here to Skip to main content
15,886,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have list where I am filling it with different records(say 25) but getting in all 25 records same data only.

C#
for (var invoiceRow = 0; invoiceRow < dataTableInvoice.Rows.Count; invoiceRow++)
{
    invoiceEntity.Rowno = Convert.ToInt32(dataTableInvoice.Rows[invoiceRow]["Rowno"]);
    invoiceEntity.ProjectName = Convert.ToString(dataTableInvoice.Rows[invoiceRow]["ProjectName"]);
    invoiceEntity.EmployeeRoleTypeName = Convert.ToString(dataTableInvoice.Rows[invoiceRow]["EmployeeRoleTypeName"]);
    invoiceEntity.WorkHours = Convert.ToInt32(dataTableInvoice.Rows[invoiceRow]["WorkHours"]);
 
    if (invoiceEntity.Rowno == -1)
    {
        invoiceEntity.InvoiceStartNo = "";
        invoicestartupno--;
    }
   
    var invoiceNumberFormat = invoicestartupno.ToString("D4");
    invoiceEntity.InvoiceStartNo = String.Format("{0}-{1}", invoiceno, invoiceNumberFormat);
    dataTableInvoice.Rows[invoiceRow]["InvoiceStartNo"] = invoiceEntity.InvoiceStartNo;
    invoiceEntity.CurrentDate = (dataTableInvoice.Rows[invoiceRow]["CurrentDate"]) == DBNull.Value 
        ? (DateTime?)null 
        : Convert.ToDateTime(dataTableInvoice.Rows[invoiceRow]["CurrentDate"]
    );
    invoiceEntity.TotalWorkingDays = workingDays;
    invoiceEntity.TotalWorkingHrs = workingHrs;
    invoiceEntity.BillableHrs = billableHrs;
    invoiceEntity.NonBillableHrs = nonBillableHrs;
    invoiceEntity.DateRange = dateSeries;

    invoicestartupno++;

    invoiceList.Add(invoiceEntity);
}


when using invoiceList getting same data in each record

how can I resolve this

help will be appreciable
Posted
Updated 27-Aug-14 21:05pm
v4

1 solution

You're adding the same invoiceEntity again and again... this could be your bug... or this intentionally?

If not, you should do
C#
var invoiceEntity = new [WHATEVER YOUR TYPE IS]();
and set the data afterwards...
 
Share this answer
 
v2
Comments
shivani 2013 28-Aug-14 3:21am    
then how to iterate this....i want everytime new invoiceentity data
StM0n 28-Aug-14 3:24am    
Improved the solution... maybe this will help.
shivani 2013 28-Aug-14 3:36am    
Thanks ....i forgot the basic thing
StM0n 28-Aug-14 3:47am    
You're welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900