Skip to main content

Posts

Showing posts from November, 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.



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 …

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.



Here is the description for each fields
ProductId- A id which uniquely identify each product.Name- Name of ProductDescription- A short description about productPrice- 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.



Then open Database Explorer and drag and drop Product table on the newly generated LINQ-to-…

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


<div><asp:CheckBoxListID="checkboxList"runat="server"RepeatLayout=UnorderedList><asp:ListItemText="Checkbox1"Value="1"></asp:ListItem><asp:ListItemText="Checkbox2"Value="2"></asp:ListItem></asp:CheckBoxList></div>




There are four options available there.Flow OrderedList Table 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><t…

C# 4.0 New feature - Named Parameter

C# 4.0 has one new cool features which is the named parameter. Suppose you have so many parameter in function and when you call them its hard to remember the sequence of the code now with the named parameter you can have name of the parameter with value like Parameter:value and one another cool feature of the name parameter is the you don't need to pass the parameter in exact sequence. Let see the following example

Code Snippet


protectedvoid Page_Load(object sender, EventArgs e) {foreach (int i in Square( max:10,min:1)) { Response.Write(i.ToString() + " "); } }
publicstaticIEnumerable<int> Square(int min, int max) {for (int i = min; i < max; i++) {yieldreturn i*i; } }




In above example I am having a function called square with two parameter min and max and having a function which will return the square of the given range between min and max. See the for each loop carefully. I hav…

Visual Studio 2010- New Features Selected Text.

I am loving more and more visual studio 2010 for some of the cool things. One of the new feature i have found in visual studio 2010 is the selected text. In the earlier version of visual studio 2010 when we select tax then its not maintaining the highlighted code but with visual studio 2010 it is maintain the colored syntax. Isn't that cool?. We can still know that what we are selecting. Like following.Technorati Tags: ,,

Yield statement in C#

Yield statement is one of most interesting statement in the C# 2.0. I have heard about lot of yield statement but today i have learned about yield statement. Yield statement can be used to return multiple object from a method while retaining its state. You can get item in sequence with help of yield.Let take a simple example to see the power or yield statement.Let's create one example which will help you the understand how its works Let create a function will return the square each time this function is called.

Code Snippet


publicstaticIEnumerable<int> Square(int min, int max) {for (int i = min; i < max; i++) {yieldreturn i*i; } }




Now each time this method is called it will return the square of current value within a given range and its also maintains the state between calls. Let create for each loop to call the the square function above.

Code Snippet


foreach (int i in Square(1, 10)) { Response.Write(i.ToString() + …

C# 4.0 - Optional Parameters

With .net framework 4.0 and C# 4.0 also introduces a new feature called optional parameters it was there in VB since visual basic 6.0. But it is now available in the C# with 4.0 version. Optional parameter will be become very handy when you don't want to pass some parameter and just you need the default value. For that Till C# 3.0 we need to create overloaded functions where we creating function with different argument with same function name. Let's see how its worksLet's go through a simple scenario we will create function with simple response.write which will print a string which will be optional argument.

Code Snippet

protectedvoid Page_Load(object sender, EventArgs e) { PrintMessage(); PrintMessage("This is message to print passed as argument"); }privatevoid PrintMessage(string MessageToPrint="This is default message to print") { Response.Write(MessageToPrint); }



See the above example in …

New publish setting dialog in visual studio 2010 beta 2.

Those who are working with asp.net will also know about the publishing site to directly with publish dialog. This was introduced with visual studio 2005 and till now it is one of the best way to publish your asp.net site with only required files. Visual studio 2010 is not a exception but the publish dialog is enhanced with more feature. Now the its directly given in the toolbar of visual studio 2010 like following.Once you select the new the following dialog will appear.In visual studio 2010 publish setting dialog has more options then earlier version. Now you can also save publish profile also and this profile will appear in the publish setting and by choosing that you can directly publish your site from the publish button with same setting you set for this profile.The publish setting dialog also has more options then earlier version of visual studio. It is having four option.MSDeploy Publish Ftp FileSystem FPSE MSDeploy Publish:With this option you can directly deploy your site on R…

Visual Studio 2010 New Features:Zooming and highlighting tags.

As i am using visual studio 2010 more and more i loving it more. While working with new Microsoft New Visual Studio 2010 Express edition I have came across about two more cool features zooming feature and another the highlighting start and end tag of particular selected tag.Let see one by one. First feature is the new zooming feature of visual studio 2010 now you can zoom your html and server side code up to 20% to 400%. This feature is very useful when you are teaching some one some thing with your code. Another thing you can also make visible your lots of code in one screen while you set it less then 100%. There is a dropdown given in both source view of html code from which you can zoom it up to 400% like as following.So you can zoom your code and view in the bigger way like following.Same way you can do it for the server side code also. Another feature i have came across is the highlighting of start and end tag in html source view. So user can have idea where the tag can be finis…

page_load event firing twice in firefox with asp.net page or user control.

Technorati Tags: ,, We are having a very large project in asp.net 3.5. During code review of project we have found that page_load event of one control is firing twice in the firefox browser. After doing some debugging i have found that it's due to one of the link button which is there in the control. I like to share you the same scenario with you all guys.One of the developer from us has specified the link button click event  two times.  The first one in directly in the html with onclick  like following.

Code Snippet


<formid="form1"runat="server"><div><asp:LinkButtonID="lnkButton"runat="server"onclick="lnkButton_Click"></asp:LinkButton></div></form>



And another in the initialize event he has also attached the event handler for the click event like following.

Code Snippet


protectedoverridevoid OnInit(EventArgs e)        {            lnkButton.Click += newEventHandler(lnkButton_Clic…

File upload control is not working with Update panel

We all are using update panel for the Ajax implementation in every site. Nowadays Ajax is almost there in each and every site. Update panel is working fine with the all other controls but when we use the file upload control then its not working fine. I have searched from lots for this and i have found one of simple solution from the Imran's blog. It is so simple and works very well. You just need to add the "multipart/form-data" attribute to the form.Here is the link from which you can have all the information.http://knowledgebaseworld.blogspot.com/2009/02/file-upload-not-working-with-update.htmlThanks Imran for posting nice info like this.Technorati Tags: ,,