Answer to Question #200545 in C# for sana

Question #200545

Create Product Detail form and perform CRUD using Entity framework model first approach.

Database Colum’s.

-ProductId

-ProductCatagory

-ProductName

-ProductPrice

-ProductQuantity


1
Expert's answer
2021-05-31T07:34:12-0400

Create a new project "ProductsProject" using VS 2017:


Add a new database "ProductsDB" to the project:




Copy and paste the following script:







CREATE TABLE [dbo].[Products] (
    [ProductId]        INT          NOT NULL,
    [ProductCatagory] VARCHAR (20) NULL,
    [ProductName]  VARCHAR (20) NULL,
    [ProductPrice]       FLOAT (53)   NULL,
	[ProductQuantity]  INT          NULL,
    PRIMARY KEY CLUSTERED ([ProductId] ASC)
);


INSERT INTO [dbo].[Products] ([ProductId], [ProductCatagory], [ProductName], [ProductPrice], [ProductQuantity]) VALUES (1, N'Pen', N'Common', 5.5, 500)
INSERT INTO [dbo].[Products] ([ProductId], [ProductCatagory], [ProductName], [ProductPrice], [ProductQuantity]) VALUES (2, N'Box', N'Common', 3.6, 400)
INSERT INTO [dbo].[Products] ([ProductId], [ProductCatagory], [ProductName], [ProductPrice], [ProductQuantity]) VALUES (3, N'Test', N'Common', 5.4, 300)


Add ADO.net Entity Data Model:




Add the following c sharp code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProductsProject
{
  class Program
  {
    static void Main(string[] args)
    {
       
      int productId;
      string productCatagory;
      string productName;
      double ProductPrice;
      int ProductQuantity;
      var context = new ProductsDBEntities();
      int menuItem = -1;

      while (menuItem != 5)
      {
        //Main menu
        Console.WriteLine("1. Add a new product.");
        Console.WriteLine("2. Display all students.");
        Console.WriteLine("3. Update a product.");
        Console.WriteLine("4. Remove a product.");
        Console.WriteLine("5. Exit");
        Console.Write("Your choice: ");
        menuItem = int.Parse(Console.ReadLine());

        if (menuItem == 1)//Add a new product
        {

          Console.Write("Enter the product ID: ");
          int.TryParse(Console.ReadLine(), out productId);
          Product selectedProduct = context.Products.Find(productId);

          if (selectedProduct == null)
          {
            Console.Write("Enter the product name: ");
            productName = Console.ReadLine();
            Console.Write("Enter the product catagory: ");
            productCatagory = Console.ReadLine();
            Console.Write("Enter the product price: ");
            double.TryParse(Console.ReadLine(), out ProductPrice);
            Console.Write("Enter the product quantity: ");
            int.TryParse(Console.ReadLine(), out ProductQuantity);
            var newProduct = new Product()
            {
              ProductId = productId,
              ProductName = productName,
              ProductCatagory = productCatagory,
              ProductPrice = ProductPrice,
              ProductQuantity = ProductQuantity,
            };
            context.Products.Add(newProduct);
            context.SaveChanges();
            Console.WriteLine("\nA new product has been added.\n");
          }
          else
          {
            Console.WriteLine("\nSelect other ID.\n");
          }

        }
        else if (menuItem == 2)//Display all students
        {
          Console.WriteLine("{0,-10}{1,-20}{2,-20}{3,-20}{4,-20}", "Id", "Catagory", "Name", "Price", "Quantity");

          for (int i = 0; i < context.Products.Count(); i++)
          {
            Console.WriteLine("{0,-10}{1,-20}{2,-20}{3,-20}{4,-20}", context.Products.ToList()[i].ProductId,
              context.Products.ToList()[i].ProductCatagory, context.Products.ToList()[i].ProductName,
              context.Products.ToList()[i].ProductPrice, context.Products.ToList()[i].ProductQuantity);
          }

        }
        else if (menuItem == 3)// Update a product
        {

          Console.Write("Enter the product id you want to edit: ");
          int.TryParse(Console.ReadLine(), out productId);
          Product selectedProduct = context.Products.Find(productId);
          if (selectedProduct != null)
          {
            Console.Write("Enter a new product name: ");
            productName = Console.ReadLine();
            Console.Write("Enter a new product catagory: ");
            productCatagory = Console.ReadLine();
            Console.Write("Enter a new product price: ");
            double.TryParse(Console.ReadLine(), out ProductPrice);
            Console.Write("Enter a new product quantity: ");
            int.TryParse(Console.ReadLine(), out ProductQuantity);

            selectedProduct.ProductId = productId;
            selectedProduct.ProductName = productName;
            selectedProduct.ProductCatagory = productCatagory;
            selectedProduct.ProductPrice = ProductPrice;
            selectedProduct.ProductQuantity = ProductQuantity;

            context.SaveChanges();
            Console.WriteLine("\nThe product has been updated.\n");
          }
          else
          {
            Console.WriteLine("\nThe product ID {0} does not exist.\n", productId);
          }
        }
        else if (menuItem == 4)//Remove a product
        {

          Console.Write("Enter the product id you want to remove: ");
          int.TryParse(Console.ReadLine(), out productId);
          Product selectedProduct = context.Products.Find(productId);
          if (selectedProduct != null)
          {
            context.Products.Remove(selectedProduct);
            context.SaveChanges();
            Console.WriteLine("\nThe product has been deleted.\n");
          }
          else
          {
            Console.WriteLine("\nThe product ID {0} does not exist.\n", productId);
          }

        }
        else if (menuItem == 5)
        {
          //exit
        }
        else
        {
          Console.WriteLine("Wrong menu item.");
        }
      }

      Console.ReadLine();
    }
  }
}



Example:









Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS