Ramco Ramco

Ramco Ramco

  • 471
  • 2.8k
  • 393.6k

How to get data by Id

Jun 18 2021 6:50 AM

Hi

  I have belwo for all Customers. What will bet the procedure for GetById & how to fecth in Controller

public List<Customer> GetAllCustomer()
        {
            List<Customer> objCustomer = new List<Customer>();
                using (SqlConnection con = new SqlConnection(Common.Con))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("sp_Customer", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Action", "R");
                    SqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        objCustomer.Add(new Customer
                        {
                            Id = rdr["Id"].ToString(),
                            Description = rdr["Description"].ToString(),
                            IsActive = Convert.ToBoolean(rdr["IsActive"]),
                        });
                    }
                    return objCustomer;
                }
        }

Thanks


Answers (4)