Hi piyushpaljn,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Model
public class ProductListResponse
{
public List<Product> Products { get; set; }
}
public class Product
{
public string ProductId { get; set; }
public string ProductName { get; set; }
public string Amount { get; set; }
public string Currency { get; set; }
}
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
ProductListResponse response = new ProductListResponse();
List<Product> products = new List<Product>();
string query = "SELECT TOP 2 ProductId, ProductName, UnitPrice FROM Products";
string constr = @"Data Source=.;Initial Catalog=northwind;uid=sa;pwd=pass@123;";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
products.Add(new Product
{
ProductId = sdr["ProductId"].ToString(),
ProductName = sdr["ProductName"].ToString(),
Amount = sdr["UnitPrice"].ToString(),
Currency = "Rs"
});
}
}
con.Close();
}
}
return Content(JsonConvert.SerializeObject(products));
}
}
Screenshot