public List <ProductsInformation> GetDetails() { List <ProductsInformation> products = new List <ProductsInformation>(); using (SqlConnection connection = new SqlConnection()) { connection.ConnectionString = "Data Source=ACUPC-0906;Initial Catalog=Assignment22sep;Integrated Security=True"; connection.Open(); string sql = "select * from ProductsInfo"; ProductsInformation p; SqlCommand cmd = new SqlCommand(sql, connection); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { p = new ProductsInformation { Id = Convert.ToInt32(reader["ProductId"].ToString()), Name = reader["ProductName"].ToString(), Price = Convert.ToDouble(reader["ProductPrice"].ToString()), ImageUrl = reader["ProductImage"].ToString(), Description = reader["ProductDescription"].ToString(), }; products.Add(p); } } connection.Close(); return(products); } }
public ProductsInformation GetDetail(int id) { ProductsInformation pi = new ProductsInformation(); using (SqlConnection connection = new SqlConnection()) { connection.ConnectionString = "Data Source=ACUPC-0906;Initial Catalog=Assignment22sep;Integrated Security=True"; connection.Open(); string sql = "select * from ProductsInfo"; SqlCommand cmd = new SqlCommand(sql, connection); using (SqlDataReader reader = cmd.ExecuteReader()) { reader.Read(); pi.Id = Convert.ToInt32(reader["ProductId"].ToString()); pi.Name = reader["ProductName"].ToString(); pi.Price = Convert.ToDouble(reader["ProductPrice"].ToString()); pi.ImageUrl = reader["ProductImage"].ToString(); pi.Description = reader["ProductDescription"].ToString(); return(pi); } } }
//SqlConnection con = null; //SqlCommand cmd = null; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { RepositoryFile rf = new RepositoryFile(); int pid = Convert.ToInt32(Request.QueryString["Id"]); ProductsInformation pi = rf.GetDetail(pid); txtid.Text = pi.Id.ToString(); txtname.Text = pi.Name.ToString(); txtimage.Text = pi.ImageUrl.ToString(); txtprice.Text = pi.Price.ToString(); txtdesc.Text = pi.Description.ToString(); } }