Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. public IHttpActionResult GetProduct(int id)
  2. {
  3.  
  4. List<Product> products = new List<Product>();
  5.  
  6. SqlConnection conn = null;
  7. try
  8. {
  9. conn = new SqlConnection(connectionString);
  10. conn.Open();
  11.  
  12. SqlCommand command = new SqlCommand("SELECT * FROM Prods WHERE ID = " + id , conn);
  13. SqlDataReader reader = command.ExecuteReader();
  14.  
  15. while (reader.Read())
  16. {
  17. Product p = new Product
  18. {
  19. Id = (int)reader["Id"],
  20. Name = (string)reader["Name"],
  21. Category = (reader["Category"] == DBNull.Value) ? "" : (string)reader["Category"],
  22. Price = (reader["Price"] == DBNull.Value) ? 0 : Convert.ToDecimal(reader["Price"])
  23. };
  24.  
  25. products.Add(p);
  26. }
  27.  
  28. reader.Close();
  29. conn.Close();
  30. }
  31. catch (Exception ex)
  32. {
  33. return null;
  34. }
  35. return Ok(products);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement