Friday, September 2, 2016

LINQ Search


using (POHDataContext pos = new POHDataContext())
            {
                var seachResult = pos.ProductTables.Where(p => p.ProductName.Contains(txtSearch.Text));
             

                grd_product.DataSource = seachResult;

            }

LINQ LOADGRD


 DataGridViewCell cell = null;
            foreach (DataGridViewCell selectedCell in grd_product.SelectedCells)
            {
                cell = selectedCell;
                break;
            }

            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;
                txtId.Text = row.Cells["ProductId"].Value.ToString();
                txtProductName.Text = row.Cells["ProductName"].Value.ToString();
                txtProductPrice.Text = row.Cells["ProductPrice"].Value.ToString();
                txtStock.Text = row.Cells["Stock"].Value.ToString();
            }

LINQ delete


 using (POHDataContext pos = new POHDataContext())
            {
                ProductTable newProduct = pos.ProductTables.SingleOrDefault(p => p.ProductId == int.Parse(txtId.Text));
                pos.ProductTables.DeleteOnSubmit(newProduct);
                pos.SubmitChanges();
            }
            GetProducts();


LINQ edit


using (POHDataContext pos = new POHDataContext())
            {
                ProductTable newProduct = pos.ProductTables.SingleOrDefault(p => p.ProductId == int.Parse(txtId.Text));
                newProduct.ProductName = txtProductName.Text;
                newProduct.ProductPrice = decimal.Parse(txtProductPrice.Text);
                newProduct.Stock = int.Parse(txtStock.Text);
                pos.SubmitChanges();
            }
            GetProducts();

LINQ Insert

 using (POHDataContext pos = new POHDataContext())
                {
                    ProductTable newProduct = new ProductTable
                    {
                        ProductName = txtProductName.Text,
                        ProductPrice = decimal.Parse(txtProductPrice.Text),
                        Stock = int.Parse(txtStock.Text)
                    };
                    pos.ProductTables.InsertOnSubmit(newProduct);
                    pos.SubmitChanges();
                }
                GetProducts();