0

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);
            }

2 Answers 2

1

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); 
            } 
        } 
    } 
Sign up to request clarification or add additional context in comments.

1 Comment

No its not showing as i excepted, please check above link which i mentioned in question.
1

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);
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.