Hi
I have below object. How to pass values from database to object. There can be more than 1 items
C#
public class DocDtls
{
public string Typ { get; set; }
public string No { get; set; }
public string Dt { get; set; }
}
public class ItemList
{
public int ItemNo { get; set; }
public string SlNo { get; set; }
public string IsServc { get; set; }
public string PrdDesc { get; set; }
public string HsnCd { get; set; }
public int Qty { get; set; }
public int FreeQty { get; set; }
public string Unit { get; set; }
public int UnitPrice { get; set; }
public int TotAmt { get; set; }
public int Discount { get; set; }
public int PreTaxVal { get; set; }
public int AssAmt { get; set; }
public int GstRt { get; set; }
public int IgstAmt { get; set; }
public int CgstAmt { get; set; }
public int SgstAmt { get; set; }
public int OthChrg { get; set; }
public int TotItemVal { get; set; }
}
public class Root
{
public DocDtls DocDtls { get; set; }
public List ItemList { get; set; }
}Thanks

Gowtham CpRank #1,552 · 1K reputationPosted Aug 19, 2024, 6:42 AM EDT
To pass values from a database to your objects in C#, you need to establish a database connection, execute SQL queries to fetch data, and then map the retrieved data to your object's properties.
Here's the key part of the code:
using (SqlConnection connection = new SqlConnection("your_connection_string_here")) { connection.Open(); // Fetch DocDtls data SqlCommand cmdDocDtls = new SqlCommand("SELECT Typ, No, Dt FROM DocDtlsTable", connection); using (SqlDataReader reader = cmdDocDtls.ExecuteReader()) { if (reader.Read()) { root.DocDtls = new DocDtls { Typ = reader["Typ"].ToString(), No = reader["No"].ToString(), Dt = reader["Dt"].ToString() }; } } // Fetch ItemList data SqlCommand cmdItemList = new SqlCommand("SELECT ItemNo, SlNo, PrdDesc, Qty, UnitPrice FROM ItemListTable", connection); using (SqlDataReader reader = cmdItemList.ExecuteReader()) { root.ItemList = new List(); while (reader.Read()) { root.ItemList.Add(new ItemList { ItemNo = Convert.ToInt32(reader["ItemNo"]), SlNo = reader["SlNo"].ToString(), PrdDesc = reader["PrdDesc"].ToString(), Qty = Convert.ToInt32(reader["Qty"]), UnitPrice = Convert.ToInt32(reader["UnitPrice"]) }); } } }Ajay BansodeRank #7,443 · 241 reputationPosted Aug 19, 2024, 4:33 AM EDT
using ADO.NET, we can pass values from database to object,
there are multiple ways to fetch that data,
1st way :- >
first, we can create a select statement to get all data in the Database,
there are 2 separate select queries for both document details and item lists. create and procedure for it.
second on the API side or Framework side execute that procedure and get data in the data table.
there are 2 rows in the data table due to 2 select queries, filling data in the respective contract.
2nd way :->
if we are ready to send multiple requests to the database then send 2 different requests to the database and fetch data one after another that is a simple way to get data and fill it according to it in the root contract.
this is the solution as per my knowledge,