Here is the description for each fields
- ProductId- A id which uniquely identify each product.
- Name- Name of Product
- Description- A short description about product
- Price- Price of Product.
Then open Database Explorer and drag and drop Product table on the newly generated LINQ-to-SQL Class like following.
Now our LINQ-SQL-Class is Ready. Now we will insert some data to our table and then first we will write code to get all product information from the database. I have created a function called GetProduct which will print all the product information on page with the help of Response.Write . Following is code for that.
public void GetProducts()
{
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
var Products = from product in context.Products
orderby product.Name
select product;
foreach(Product p in Products)
{
Response.Write(string.Format("<B>Id</B>:{0}", p.ProductId.ToString()));
Response.Write(string.Format("<B>Name</B>:{0}", p.Name));
Response.Write(string.Format("<B>Description</B>:{0}", p.Description));
Response.Write(string.Format("<B>Price</B>:{0}", p.Price.ToString()));
Response.Write("===========================================");
}
}
}
public void AddProduct(Product product)In the above function i am passing a new object of product and passing that object to above function following is a code for that which create a new product via calling above function.
{
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
context.Products.InsertOnSubmit(product);
context.SubmitChanges();
}
}
//Add New ProductNow we have added the code to add product now lets create a function to update the current product information. Following is a code for that.
Product product = new Product();
product.Name = "Product3";
product.Description="This is product 3 Description";
product.Price=10;
AddProduct(product);
public void UpdateProduct(int productId, string name, string description, double price)
{
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
Product currentProduct = context.Products.FirstOrDefault(p => p.ProductId == productId);
currentProduct.Name = name;
currentProduct.Description = description;
currentProduct.Price = price;
context.SubmitChanges();
}
}
////Update ProductNow we added the code for update product so now let's create a sample code to delete a specific product. Following is a code for that.
UpdateProduct(2,"New Product2","New Description for product",20);
public void DeleteProduct(int productId)You can delete the product via passing product id as following.
{
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
Product currentProduct = context.Products.FirstOrDefault(p => p.ProductId == productId);
context.Products.DeleteOnSubmit(currentProduct);
context.SubmitChanges();
}
}
//Delete productSo that's it. you can create insert,update,delete operation with the help of LINQ-To-SQL within some minutes without writing so much code for .net and queries for databases.
DeleteProduct(3);
Happy Programming...
l;k
ReplyDeleteNice coding
ReplyDeleteThanks you :)
ReplyDeleteGood one
ReplyDelete@Davda C Thanks
ReplyDeleteThank u for the good tutorial
ReplyDeleteThanks buddy!!
ReplyDelete