Reputation: 1489
Ho Can i Add data to list in inside list using model in c#,
I have referred code form below url but they are directly entering value, i need to loop through each row?
C# - Adding data to list inside list
below is my code
public class AllClaimDetails
{
public class AllClaims
{
public string VendID { get; set; }
public int VendKey { get; set; }
public string CompanyID { get; set; }
public IList<ClaimLots> Lots { get; set; }
}
public class ClaimLots
{
public string LotNo { get; set; }
public string InvoiceType { get; set; }
public string TranNo { get; set; }
public int TranType { get; set; }
public decimal TranAmount { get; set; }
public decimal ApplyAmount { get; set; }
public int Status { get; set; }
}
}
DataTable dt = new DataTable();
SqlDataReader rdr = cmd.ExecuteReader();
dt.Load(rdr);
List<AllClaims> requestObject = new List<AllClaims>();
for (int i = 0; i < dt.Rows.Count; i++)
{
AllClaims claims = new AllClaims()
{
VendID = dt.Rows[i]["VendID"].ToString(),
Lots = new List<ClaimLots>()
{
new ClaimLots{
LotNo=dt.Rows[i]["LotNo"].ToString(),
TranNo=dt.Rows[i]["TranNo"].ToString(),
TranType=Convert.ToInt32(dt.Rows[i]["TranType"].ToString()),
//TranAmount=Convert.ToInt32(dt.Rows[i]["TranAmt"].ToString()),
Status=Convert.ToInt32(dt.Rows[i]["Status"].ToString())
}
}
};
requestObject.Add(claims);
}
Upvotes: 0
Views: 1380
Reputation: 15101
Not sure why you want to use IList<T>
instead of List<T>
for Lots
. When you refer to your classes you have neglected to add the containing class. I also tidied up the data code and change your loop to a foreach
. Not really sure what the problem is.
public class AllClaimDetails
{
public class AllClaims
{
public string VendID { get; set; }
public int VendKey { get; set; }
public string CompanyID { get; set; }
public List<ClaimLots> Lots { get; set; }
}
public class ClaimLots
{
public string LotNo { get; set; }
public string InvoiceType { get; set; }
public string TranNo { get; set; }
public int TranType { get; set; }
public decimal TranAmount { get; set; }
public decimal ApplyAmount { get; set; }
public int Status { get; set; }
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string ConStr = "Your connection string"
private void OPCode()
{
DataTable dt = new DataTable();
using (SqlConnection cn = new SqlConnection(ConStr))
using (SqlCommand cmd = new SqlCommand())
{
cn.Open();
dt.Load(cmd.ExecuteReader());
}
List<AllClaimDetails.AllClaims> requestObject = new List<AllClaimDetails.AllClaims>();
foreach (DataRow row in dt.Rows)
{
AllClaimDetails.AllClaims claims = new AllClaimDetails.AllClaims()
{
VendID = row["VendID"].ToString(),
Lots = new List<AllClaimDetails.ClaimLots>()
{
new AllClaimDetails.ClaimLots{
LotNo=row["LotNo"].ToString(),
TranNo=row["TranNo"].ToString(),
TranType=Convert.ToInt32(row["TranType"].ToString()),
Status=Convert.ToInt32(row["Status"].ToString())
}
}
};
requestObject.Add(claims);
}
}
Upvotes: 1
Reputation: 1134
I hope this will help you.
using System;
using System.Collections.Generic;
class TestList{
public static void Main(String[] args)
{
// Creating a List of integers
List<int> firstlist = new List<int>();
// adding elements in firstlist
for (int i = 4; i < 10; i++) {
firstlist.Add(i * 2);
}
// Displaying elements of firstlist
// by using foreach loop
foreach(int element in firstlist)
{
Console.WriteLine(element);
}
}
}
Upvotes: 1