Friday, February 26, 2010

How to send mail asynchronously in

With Microsoft.NET Framework 2.0 everything is asynchronous and we can send mail also asynchronously. This features is very useful when you send lots of bulk mails like news letter. You don’t have to wait for response from mail server and you can do other task .

Let's create a simple example to send mail. For sending mail asynchronously you need to create a event handler that will notify that mail successfully sent or some errors occurred during sending mail. Let’s create a mail object and then we will send it asynchronously. You will require System.Net.Mail space to import in your page to send mail.

//you require following name space
using System.Net.Mail;

//creating mail message object
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("Put From mail address here");
mailMessage.To.Add(new MailAddress("Put To Email Address here"));
mailMessage.CC.Add(new MailAddress("Put CC Email Address here"));
mailMessage.Bcc.Add(new MailAddress("Put BCC Email Address here"));
mailMessage.Subject = "Put Email Subject here";
mailMessage.Body = "Put Email Body here ";
mailMessage.IsBodyHtml = true;//to send mail in html or not

SmtpClient smtpClient = new SmtpClient("Put smtp server host here", 25);//portno here
smtpClient.EnableSsl = false; //True or False depends on SSL Require or not
smtpClient.UseDefaultCredentials = true ; //true or false depends on you want to default credentials or not
Object mailState = mailMessage;

//this code adds event handler to notify that mail is sent or not
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
smtpClient.SendAsync(mailMessage, mailState);
catch (Exception ex)
Following is a code for event handler

void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
MailMessage mailMessage = e.UserState as MailMessage;
if (!e.Cancelled && e.Error != null)
Response.Write("Email sent successfully");
So that’s it you can send mail asynchronously this 30-40 lines of code.

Technorati Tags: ,

Shout it

kick it on
Sunday, February 21, 2010

How to import your tweets into Google Buzz.

Social networking is a important feature for any person for professionally ass well as personally. You can stay in touch with your friends,colleague and other peoples thanks to social networking sites. Google Buzz is a counter part of twitter from Google. you can post anything on that and you don’t have limit of 140 characters just like. But now there are lots of social networking site available  like Face book,Twitter,Orkut,LinkedIn and now Google Buzz and updating your profile and other things on every social networking site is a tedious job.So, I decided to synchronize my twitter account into Google Buzz. Adding your twitter account in Google Buzz is very easy. First open your Gmail Account and then click on Buzz it will redirect you to your Google Buzz screen. Then click on connected sites and following skin will appear.


Click on add it will ask your twitter name just put your twitter name and that’s it you have done with it. Your tweet will appear in Google Buzz depends on traffic from 1 min to 1 hour.

kick it on
Shout it

Delete all cookies and session in

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.
Technorati Tags: ,,,

Shout it
kick it on
Saturday, February 20, 2010

How to remove all the cache object from 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())
Here is the another example of removing cache.
foreach (DictionaryEntry dEntry in HttpContext.Current.Cache)
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
Shout it
Thursday, February 4, 2010

Microsoft Community Techdays at Ahmedabad- A great Event

On 30th January Microsoft,Pass Organization and Ahemdabad .NET User Group organized Microsoft Community Techdays. It was a great event and full of information. There were five session with the full of content. First session was given by Mahesh Dhola on the Windows Azure Platform. In his session he explained all the thing related with windows azure platform from basics like what is windows azure why it is required and what is use of windows azure platform and how its works.

Windows Azure presentation by Mahesh Dhola

Insight of Windows Azure Platform by Mahesh Dhola

The next session was presented by Pinal Dave- A Microsoft Most Valuable Professional and Well know Database Architect for SQL Server. It was about SQL Azure. He has explained what is SQL Azure and all the tools that are needed to connect and maintain database on SQL Azure Server.


SQL Azure: Extending SQL Data Platform to Cloud By Pinal Dave

Then after lunch there was session given by Jadeja Dushyantsinh on Silvelight 4.0. In this session he has explained what is Silvelight, History of Silvelight and what’s new in Silvelight 4.0.The session also include some interesting demo of Silvelight 4.0 Features.


Microsoft Silvelight 4.0- An Overview By Dushyantsinh Jadeja

After this session there was an interesting session by Jacob Sebastian A well know SQL Book writer and Microsoft Most Valuable Professional . He explained SQL Server 2008 R2 data migration features and integration with Visual Studio 2010 by taking an example of love story of Summit- A DBA and Leena-A Developer. It was a great session We have learnt lots of refracting features available in SQL Server 2008 R2 and Visual Studio 2010 Ultimate.


Fall in love with SQL Server 2008 R2 By Jacob Sebastian

Last but not least session given by Prabhjot Bakshi – A well known Microsoft Certified Trainer about Visual Studio 2010 enhancement for Developers. In this session he has explained UML Designer features and lots of new things about Microsoft.NET 4.0 and Microsoft Visual Studio 2010. He has also explained why we need functional programming and some features of F#- A new functional programming language by Microsoft. Indeed it was great i have learnt lot about that in this session.


Visual Studio 2010 enhancement for Developers By Prabhjot Bakshi


Me(First from Left) with Mahesh Dhola,Prabhjot Bakshi and other Ahmedabad Group Members

Overall it was a great event and thank you once again Microsoft for organizing such events.

Shout it
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.


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"
d:DesignHeight="300" d:DesignWidth="400">

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

Here grid will be act as container like as we are using content pane in our 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 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
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 website. After doing searching for sometime the i found following link.

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

Happy New Year 2010- From the writer of DotNetJaps

Technorati Tags: ,

I wish happy new year 2010 to all my readers.I know that this blog has been quite from last 1 or 2 month but i was busy with professional end as well as my computer operation system was break so it was long process to get my all data back and having reinstalled all the software and other thing i need but i promise you that i will post all the new latest thing 4.0,Microsoft SQL Server 2008, Silver light and other latest Microsoft Technologies that I am going explore in this new year so you will going hear at least one post week from this blog.

Once again I am wishing you a very happy and phosphorus new year to all of you.

Shout it
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;

dynamic floatExample = 2.5;

dynamic stringExample = "DotNetJaps";
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,,VB.NET";
var varstring=s;
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,,VB.NET";
dynamic varstring=s;
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
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.


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.


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()));
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())
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";
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;
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);
You can delete the product via passing product id as following.
//Delete product
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

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