Monday, November 30, 2009

C# 4.0-Dynamic Data Type, Difference between var and dynamic

C# 4.0 introduces a new keyword called 'Dynamic'. It can consume any object anything. Let's see some examples for that.
dynamic intExample = 1;
Response.Write(intExample);

dynamic floatExample = 2.5;
Response.Write(floatExample);

dynamic stringExample = "DotNetJaps";
Response.Write(stringExample);
It will print out put on web page as following.

C#-4.0-Dynamic-Keyword-Diffrence-Between-Var-And-Dynamic-Type

Now, you will have question what's new in that. It could be also done with var keyword . Yes, you can do same thing with var but dynamic keyword is slightly different then var keyword.

Diffrence between var and dynamic keyword:

var keyword will know the value assigned to it at compile time while dynamic keyword will resolve value assigned to it at run time. I know you guys don't believe me without example. So let's take example of string.
string s = "DotNetJaps-A Blog for asp.net,C#.net,VB.NET";
var varstring=s;
Response.Write(varstring.MethodDoesnotExist());
Now try to compile above code it will not compile and it gives a error that 'string' does not contain a definition for 'MethodDoesnotExist' and no extension method 'MethodDoesnotExist' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?'. So var keyword knows that what value or object or anything assigned to it. Now lets try to compile same code with dynamic example like following.
string s = "DotNetJaps-A Blog for asp.net,C#.net,VB.NET";
dynamic varstring=s;
Response.Write(varstring.MethodDoesnotExist());
This will compile. So it is the difference between dynamic and var keyword. With dynamic keyword anything assigned to it like property,objects operators anything that will be determined at compile time. It can be useful while we are doing programming with com,JavaScript which can have runtime properties.

Happy Programming...

Technorati Tags: ,,,
Shout it

kick it on DotNetKicks.com
Share:
Sunday, November 29, 2009

Simple Insert,Update,View and Delete with LINQ-To-SQL

Today one of my friend asked me about simple insert,update and delete example with LINQ-To-SQL but at that time i was not having any simple example which will show the power of LINQ-To-SQL Example. So i have decided to create one simple example with LINQ-To-SQL and also decide to blog about it. Let's create a simple example which is very simple and also show basic step to use LINQ-To-SQL. First i have created a simple table called which have 4 fields like ProductId,Name,Description and Price. Like following.

Table-for-which-LINQ-To-SQL-Classes-Generated

Here is the description for each fields
  1. ProductId- A id which uniquely identify each product.
  2. Name- Name of Product
  3. Description- A short description about product
  4. Price- Price of Product.
Now, Lets create a Linq-To-SQL and rename it as MyDataContent.dbml as following. For that Go to your project ->Right Click->Add New Item->Go to Data Template-> LINQ to SQL Classes like following.

How-to-add-LINQ-To-SQL-Classes-to-your-application

Then open Database Explorer and drag and drop Product table on the newly generated LINQ-to-SQL Class like following.

How-Drag-and-Drop-Database-Tables-to-Linq-To-SQL-Classes

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("===========================================");
}
}
}
Now Let's Create a function to Add Product. Following is code for function to add product.
public void AddProduct(Product product)
{
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
context.Products.InsertOnSubmit(product);
context.SubmitChanges();
}
}
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.
//Add New Product
Product product = new Product();
product.Name = "Product3";
product.Description="This is product 3 Description";
product.Price=10;
AddProduct(product);
Now 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.
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();
}
}
With the help of above function you can update current product like following.
////Update Product
UpdateProduct(2,"New Product2","New Description for product",20);
Now 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.
public void DeleteProduct(int productId)
{
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
Product currentProduct = context.Products.FirstOrDefault(p => p.ProductId == productId);
context.Products.DeleteOnSubmit(currentProduct);
context.SubmitChanges();
}
}
You can delete the product via passing product id as following.
//Delete product
DeleteProduct(3);
So 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.

Happy Programming...

Technorati Tags: ,,,
Shout it

kick it on DotNetKicks.com
Share:
Friday, November 20, 2009

ASP.NET 4.0 New Feature- RepeatLayout property for CheckBoxList and RadioButtonList Controls.

ASP.NET 4.0 having many many new features. One of them is the RepeatLayout property for the CheckBoxList and RadioButtonList controls. The property is useful when we need decide how this controls will be render as html when its load in browser .



Code Snippet



  1. <div>
  2. <asp:CheckBoxList ID="checkboxList" runat="server" RepeatLayout=UnorderedList>
  3. <asp:ListItem Text="Checkbox1" Value="1"></asp:ListItem>
  4. <asp:ListItem Text="Checkbox2" Value="2"></asp:ListItem>
  5. </asp:CheckBoxList>
  6. </div>





There are four options available there.

  1. Flow
  2. OrderedList
  3. Table
  4. UnorderedList

Flow: This option will render control with span tag. This option will be better when you need tables less html for your site.

OrderedList:This option will load contriol as orderedlist. It will load html with <ol> and <li> Tags.

Table:If you love table structure then this is the option for you it will load html with <table><tr> and <td> tags.

UnorderedList: This option will load control as UnorderedList. It will load with <ul> and <li> tags.

So this property will useful when you want to have more control over html rendering in your browser as per your requirement.

Happy Programming...


Shout it
kick it on DotNetKicks.com
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews