Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public IHttpActionResult GetProduct(int id)
- {
- List<Product> products = new List<Product>();
- SqlConnection conn = null;
- try
- {
- conn = new SqlConnection(connectionString);
- conn.Open();
- SqlCommand command = new SqlCommand("SELECT * FROM Prods WHERE ID = " + id , conn);
- SqlDataReader reader = command.ExecuteReader();
- while (reader.Read())
- {
- Product p = new Product
- {
- Id = (int)reader["Id"],
- Name = (string)reader["Name"],
- Category = (reader["Category"] == DBNull.Value) ? "" : (string)reader["Category"],
- Price = (reader["Price"] == DBNull.Value) ? 0 : Convert.ToDecimal(reader["Price"])
- };
- products.Add(p);
- }
- reader.Close();
- conn.Close();
- }
- catch (Exception ex)
- {
- return null;
- }
- return Ok(products);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement