Showing posts with label .NET4.0. Show all posts
Showing posts with label .NET4.0. Show all posts
Saturday, March 20, 2010

Microsoft.NET 4.0/VB.NET 10 Automatic Properties

In C# we are having automatic properties since C# 3.5 framework but now with Microsoft.NET 4.0 Framework VB.NET 10.0 version we are also having automatic properties for VB.NET Also.

Like in C# we can define automatic like following.

Public string TestProperty
{
get;
set;
}
Same way we can define in automatic Property in VB.NET as follows.
Public Property TestProperty As String 
You can use this properties any where in class same as C# automatic property.

Shout it
kick it on DotNetKicks.com
Share:
Monday, March 15, 2010

Rename feature in Visual Web Developer 2010/Visual Studio 2010

Visual web developer is great tool and I am playing more and more with it and every time I am discovering some new features of it. Recently I have discovered a very cool feature of it. I want to rename a variable in visual studio 2010 Web Developer express edition and I found a great refractor tool for that which will rename that variable in all the instance and all the methods. First you need to select variable which you want to rename and then you need to Right Click ->Refractor->Rename. You can also invoke that via its shortcut Ctrl + R,Ctrl +R and it will be available For reference see the below screenshot.

Rename variable featuers in visual studio 2010

Once you click the rename which will have a dialog box open which will ask for new name or variable like following.

RenameDilaog option in visual web developer 2010

It is also having options for search in comments and search in string. Search in comment will search and replace in comment and search in string will replace word for any string which contains that variable name. Once you put new name in dialog box it will display a preview of replaced strings like following. Here also you can select where you need to rename and where you don’t want to rename and this is really cool. :)

Preview dialog for visual web developer 2010

Shout it
Share:
Sunday, February 21, 2010

Delete all cookies and session in asp.net

Cookies are one of most important features of web application. From where we store little information on client side in one of my project we need to delete all cookies and as you can not delete cookies from server side so we have to expire them manually. Here is the code for that.

 string[] myCookies = Request.Cookies.AllKeys;
foreach (string cookie in myCookies)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
Same way we can delete all session for following code.
Session.Abandon();
Technorati Tags: ,,,

Shout it
kick it on DotNetKicks.com
Share:
Saturday, February 20, 2010

How to remove all the cache object from asp.net application

Cache object in .NET technologies very power full feature and we all use it for increasing out application performance. In one of my project i need to remove all of the my cache once some task was completed. So i have done some digging and i have found some beautiful way of removing all the cache object. As you know cache it self is dictionary entry object so we can always remove them via loop here are some of code examples from which we can remove the all the cache object.

//Following namespace you need.
using System.Collections;
using System;
using System.Collections.Generic;
using System.Web;

IDictionaryEnumerator cacheEnumerator = HttpContext.Current.Cache.GetEnumerator();

while (cacheEnumerator.MoveNext())
{
HttpContext.Current.Cache.Remove(cacheEnumerator.Key.ToString());
}
Here is the another example of removing cache.
foreach (DictionaryEntry dEntry in HttpContext.Current.Cache)
{
HttpContext.Current.Cache.Remove(dEntry.Key.ToString());
}
Please make sure this is way only for the cache object it will not going to remove other cache like output cache etc.



kick it on DotNetKicks.com
Shout it
Share:
Tuesday, January 5, 2010

Creating Hello World application with Silverlight 3.0 with Visual Studio 2010

Silvelight is latest buzz in Microsoft.NET Technologies. Silverlight enable us to create rich user interface for the web based application. Let create a simple application to understand how Silverlight application works.

We will start creating application with file menu –> New Project –>Silverlight application a dialog for silverlight application will open like following.

Hello Word application by silver light 3.0

Once you create a Silverlight application a new dialog will open to create a website which will host Silvelight application like following.

SilverLight

This website are used for hosting Silvelight application. Please note that Silverlight application can also run with simple html file also. After clicking on Ok it will create a simple project like following in solution explorer.

Solution Explorer,Silver Light 3.0,ASP.NET 4.0

Now let's Create a simple button and on clicking on that button will print “Hello World” on screen.With Silvelight used Microsoft new UI Language called XAML which used to define the object in the SilverLight.

Once we create a silvelight application which will create a MainPage.xaml and MainPage.xaml.cs file for the same. It will create by default XAML like following.

<UserControl x:Class="MySilverLight.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">

</Grid>
</UserControl>
Here grid will be act as container like as we are using content pane in our asp.net application. Which can host the another objects like button text and other objects. Now I am creating a simple hello world button via adding following xml between <Grid> and </Grid> tag.
<Button x:Name="HelloWorldButton" Content="Push to print Hello World on Screen " Height="25" Width="250"></Button> 

Here x:name is name of button and content is text that will display on button,width will specify width of button and height will specify height of button. It will create a simple button like following in the design layout of silverlight application as following.

SilverLight 3.0,

Now let add a text block object which will print the Hello world text on the screen after clicking button with following XAML.
<TextBlock x:Name="Mytextbox" Text="Print Text" Height="25" Margin="75,197,92,78"></TextBlock>
Which will create a text block just below the button like following. Here margin will define the margin from four side just like html. Text block will behave just like label in asp.net application

TextBlock, SilverLight Button, Silverlight 3.0

After that Let’s add a add a click event to button via modifying existing XAML like following .
<Button x:Name="HelloWorldButton" Content="Push to print Hello World on Screen " Height="25px" Width="250px" Click="HelloWorldButton_Click" ></Button> 
It will create a click handler for the button we have added after then we can add code to print Hello World!! to event handler we have just create like following.
private void HelloWorldButton_Click(object sender, RoutedEventArgs e)
{
Mytextbox.Text = "Hello World!!!";
}
Now compile and run the application it will open a browser and once you click the button it will print a hello world!! text like following.

Silverlight in Browser,SilverLigh Helloworld


Technorati Tags: ,
Shout it
kick it on DotNetKicks.com
Share:
Saturday, January 2, 2010

The operation could not be completed. Class not registered- Visual Studio 2010 Express edition beta2.

My operating system was crashed so i have again downloaded and installed Microsoft Visual Studio 2010 Express edition beat 2. But when i was trying to create a web application then i got the error message that “The operation could not be completed. Class not registered.” Other projects were working fine and I am able to create asp.net website. After doing searching for sometime the i found following link.

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=498787&wa=wsignin1.0

From that i found that it was Microsoft SQL Server Express edition that is creating problem. I had installed Microsoft Visual Studio IDE not the SQL Server Express 2008 edition. So i have installed SQL Server Express Edition 2008 and now its working fine. But it is very strange that i can not create a web application project with out installing it.

Technorati Tags: ,
Shout it
Share:
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:
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:
Saturday, November 14, 2009

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



  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. foreach (int i in Square( max:10,min:1))
  4. {
  5. Response.Write(i.ToString() + " ");
  6. }
  7. }

  8. public static IEnumerable<int> Square(int min, int max)
  9. {
  10. for (int i = min; i < max; i++)
  11. {
  12. yield return i*i;
  13. }
  14. }





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 have named parameters and another thing you will notice that i have changed the sequence of parameter but still its working fine. So this is a very cool new feature from the C# 4.0.

Happy Programming...

Technorati Tags: ,

kick it on DotNetKicks.com
Shout it
Share:

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.

Text

Shout it
Share:
Tuesday, November 10, 2009

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



  1. public static IEnumerable<int> Square(int min, int max)
  2. {
  3. for (int i = min; i < max; i++)
  4. {
  5. yield return i*i;
  6. }
  7. }





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



  1. foreach (int i in Square(1, 10))
  2. {
  3. Response.Write(i.ToString() + " ");
  4. }





And here will be the output in the browser.

image

So it will create IEnumerable without implementing any interface and lesser code.

Happy Programming..

Technorati Tags: ,,

kick it on DotNetKicks.com
Shout it
Share:
Saturday, November 7, 2009

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.

Zoom1

So you can zoom your code and view in the bigger way like following.zoom2

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 finish. As you can see in following picture I have selected link button and its highlighting both start and end tag of link button.

Highlight

Technorati Tags: ,
Shout it
Share:
Tuesday, October 27, 2009

Review of Microsoft Visual Web Developer 2010 beta 2 express edition.

Microsoft visual studio express editions are light weight IDE provided free by Microsoft and also having almost all the functionality of its big brother Microsoft Visual Studio Professional edition except few. My pc is bit old now Its P4 and its having only 1gigs of ram so the express edition are the best suited for pc like mine. Microsoft has recently provided the beta 2 version of Microsoft new generation IDE called Visual Studio Twenty Ten(2010).

There are lots of changes from visual studio 2008 express edition and Microsoft Visual Studio 2010 Express edition. The first changes you will notice is the brand new splash screen and brand new colored logo for visual studio.

LogoVisual Splash

Another thing you will notice is the look and fill of IDE. It all looks like blue every where. Its is best suited for the Microsoft forthcoming operating system Microsoft Windows 7.

VWD

Another the difference is the create new project window which is bit stylish and having more options compare the to the visual studio 2008. There are two new things in new project dialog the first one the windows azure tools which is Microsoft's new steps towards the could computing(I will blog about this later one). and another thing is the Silver light application option. Now you can create three kind of application with the Microsoft SilverLight Application

  1. Silverlight Application
  2. Silverlight Navigation Application
  3. Silverlight class library

OpenDialog

Another difference is the default font for the IDE earlier it was Courier New and now its Consolas which is the next generation programming font. Which is more visual than the other fonts.

Another major difference is when you create a asp.net web application then its now having more default folders compare to visual studio 2008. There are site.master is also there and some default style sheet just like asp.net mvc applications. You will have default login control like asp.net mvc application. There are two more folder scripts and styles. In style you will have default style sheet for the application. Another folder is for the scripts which are having default JQuery as per earlier announcement Microsoft and JQuery will go together.

SolutionsExplorer

Another new things in Microsoft Visual Studio 2010 Visual Web Developer Expression edition is the Tool Setting menu which are having three setting by default.

  1. Basic Settings
  2. Code Optimized
  3. Expert Settings.

All the three option will have different setting for solution explorer and all other stuff. Another new thing is visual web developer 2010 express edition having multi monitor support which is not there in earlier express editions.Visual web developer express also having windows presentation foundation code editor.

Another cool feature is the maximized windows in design view now you will have whole window is maximized in design view hiding other windows. So you visually look all the control more better. Another cool thing is the percentage dropdown through which you can set all screen by percentage as per your requirement.

There are lots of more features like Improved css compatibility,HTML and Javascript snippets,support for asp.net mvc application and support for multi target etc. I will blog about each feature in much more detail. T

If you still not downloaded the future version of visual studio express edition 2010 then you can download it from the following link.

http://www.microsoft.com/express/future/default.aspx

Shout it
Share:
Saturday, July 25, 2009

Sending mail with secure service layer in asp.net 3.5/2.0

In earlier post i have mentioned that how to send the email using new System.Net.Smtpmail with asp.net 2.0 or higher version. Now lets learn how to send the mail with the secure service layer in the asp.net.

Security is the one of the most important requirement in today’s world as you are doing your business online you have keep secure from other wrong elements. Secure service layer add an

extra layer of security to your web application and sending mail with the SSL with tighten your security for mails. Your emails are more secure then ever and your valuable information will not going to wrong hands.

Here is the code from which we can send the email with the secure service layer. As i have mentioned in earlier post you need to to send email with authentication to use SSL. So you need to write following code. First you need to create message like following..

MailAddress fromAddress = new MailAddress("[email protected]","Nameofsendingperson");
message.From = @”fromAddress”;//here you can set address
message.To.Add("mailto:[email protected]%22%29;//");//
message.Subject = "Sending email with ssl";
message.CC.Add("[email protected]");//ccing the same email to other email address
message.Bcc.Add(new MailAddress("[email protected]"));//here you can add bcc address
message.IsBodyHtml = true;//To determine email body is html or not
message.Body =@"Plain or HTML Text";
Then you have to sent message with following code.
System.Net.Mail.SmtpClient mailClient =
 new System.Net.Mail.SmtpClient("your smptp","ssl port");
//This object stores the authentication values
System.Net.NetworkCredential basicCrenntial = 
new System.Net.NetworkCredential("username", "password");
mailClient.Host = "Host";
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicCrenntial;
// You have to add one other properties for SSL
mailClient.EnableSSL=true;
mailClient.Send(message);
Share:
Friday, July 24, 2009

Client Id of a control in ASP.NET 4.0.

Client Id is used to store the id of a server control in client side. ASP.NET Engine creates a very long client id to unique identify each element on the same page. If you are having so many inner controls hierarchy then asp.net will generate very long client id like “ctl00_Ctl001_ctl02_BM_Login_btnSave” here i i have used control name like ctrl00,ctr001,bm etc. If you are having long control name then it will more longer then this. This will increase html kilobytes for the pages and if you are having so many server controls in the page then it will take time to load in browser and will decrease performance of application . To overcome this problem we have to override existing asp.net controls like textbox,label etc and then you have override client id from server side with server side id. Now with asp.net 4.0 you can control client id generated by .net framework. In asp.net 4.0 there is one another property called ClientIdMode which will control the behavior of client id generated by .net framework. Like following.

<asp:Button ID="btnSave" runat="server" ClientIDMode="[Mode]" />

There are four types of different mode for ClientIdMode property.

  1. Legacy- This is the same as in old version. It will generate client like like “ctl00_Ctl001_ctl02_BM_Login_btnSave” in old fashion.

  2. Inherit-This is the default mode for every control . It will refer parent control to have its clientidmode. If you want to change it then you have to specify your client id mode for that control.

  3. Static –This mode will put client id as static if you set server id like “btnSave” then it will generate the same ld like “btnSave”. So if you want to use this mode then you have to make sure that you are having all unique name in one pages. Otherwise it will be a mess for ids.

  4. Predictable- This mode will used when the framework needs to ensure uniqueness of each control in particular way. The framework traverse from the control to parent controls and put hierarchy of parent control id with control id like ‘”ctrl0_btnsave” and for another btnsave its like “ctrl0_ctrl1_btnsave” etc.

  5. Technorati Tags: ,
Share:
Saturday, July 18, 2009

Partial Types, Class and Method in C#.NET 3.0/2.0

With C# 2.0 Microsoft has added partial keyword in C#. So what is partial keyword used for. Lets go through the partial keyword in greater detail.

First and foremost use of the partial keyword is the you can declare one class or method with more then one declaration and at the compilation time it will have one complete class.So now you can declare a class more then one file at the compile time it will be treated as one class.

For example this is one class

//first file Patial Class
public class partial PartialClass
{
   public void First()
   {
     //First function
   }
}
 Here is the another class
public class partial PartialClass
{
   public void Second()
   {
      // Logic...
   }
}
Now at the compile time it will be treat as single class like following.
public class PartialClass
{
   public void First()
   {
      // Logic...
   }
   public void Second()
   {
      // Logic...
   }
}
Same way you can use partial method it will treated as one method as compile time. Partial method will only available with C# 3.0. It will not be available to C# 2.0.

Practical Usage Of Partial Class:

There are lots of way where partial class is useful like many developer can work on same class via creating different files. There are lost of code generators available in market so you can extend the class functionality via marking them partial. Partial class can also be used for the manage code in separate files instead of creating large files. With C# 3.5 linq to sql designer uses this concept to split custom behaviors outside mapping class.

Share:
Wednesday, June 10, 2009

What’s new in asp.net 4.0 and .net Framework 4.0?

Microsoft.NET Framework 4.0 beta version is out now. Let’s see the what’s new in asp.net 4.0 for the developer purpose. There are lots of features are there. Some of them are following.

1) Extensible Output Caching:

ASP.NET 4.0 adds an extensibility to output caching now you can create your cache provider and will can use this provider for the caching. So now you can use your own data source for you caching.

2) AutoStart Webapplication

Before asp.net 4.0 you have to use application_load or application_start event for the extensive data processing before application pages loads. But now you can use auto start features of asp.net 4.0. A new scalability feature named auto-start that directly addresses this scenario is available when ASP.NET 4.0 runs on IIS 7.5 on Windows Server 2008 R2. The auto-start feature provides a controlled approach for starting up an application pool, initializing an ASP.NET application, and then accepting HTTP requests.

3) Permanently Redirecting a page

Before asp.net 4.0 you need to use response.redirect method to go to another page from new page. ASP.NET, developers have traditionally handled requests to old URLs by using by using the Response.Redirect method to forward a request to the new URL. However, the Redirect method issues an HTTP 302 Found (temporary redirect) response, which results in an extra HTTP round trip when users attempt to access the old URLs. Now permanent redirect will solve this. Its will also benefit for the Search Engine Optimization.

4) Compressed Session State:

ASP.NET 4.0 will have new option for session stored in sql server or in state service. You can compress your serialized session using GZip Compression

5) Page.Keywords and Page.Description Properties

In asp.net 4.0 now you can create dynamic keywords and description for a page using Page.Keywords and Page.Description Properties

6) Enabling View state for Individual Controls

In earlier version of the asp.net if you disable your parent control view state it will disable your child control view state but now with asp.net view state is not dependent on parent child control view state.

7) Routing in asp.net 4.0

Now you can have build in routing for your pages. You don’t have to write your url rewriting modules. It introduces a WebFormRouteHandler routing class which will take care of routing.

8) ClientID

In earlier asp.net version clientId is system generated and if you want to change you have to override it . ASP.NET by default generated a big string for the clientid which will increase your html kb. With asp.net you have small client id using different options Legacy,Static,Predictable,Inherit

There are several other features are also there like following

Live Data Binding

Observer pattern using JavaScript and JavaScript objects

ADONetServiceProxy Class

Formview Control Ehancements

Ajax Improvements in asp.net 4.0

I have just list some of the features you can have all the features available in asp.net 4.0 white paper from the following link.

http://www.asp.net/learn/whitepapers/aspnet40/

Cheers… Happy Programming…

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