Tuesday, March 29, 2011

New Learning : Microsoft WebMatrix

Microsoft WebMatrix

Microsoft WebMatrix is a web development tool from Microsoft. It is providing everything you need to develop website applications like built in templates, website from web gallery etc. Moreover in WebMatrix it is really easy to customize your website with the tools provided by the WebMatrix application. There is no need to switch between different application customize your website. WebMatrix provide an easy way to publish your website, it will even help to find the hosting provider within the WebMatrix application to make your application live.

 

Creating website in WebMatrix

To create website in WebMatrix first you need to install it in your machine. You can download WebMatrix from following location in Microsoft website. Its free to download.

http://www.microsoft.com/web/gallery/install.aspx?appid=webmatrix

Once you launched the WebMatrix application you can see four options as following figure:

1

1. My Sites
    Open the existing website you have created recently in WebMatrix
2. Site From Web Gallery, where you can create website from variety of application gallery such as WordPress, Joomla, Drupal, phpBB etc.

2

3. Site from Template, where you can create website based on different templates provides by WebMatrix as shown in figure below:

3

4. Site from Folder, which will allow you to create a website from an existing folder.

4

Getting started tutorials are available in Microsoft website. See the following links to get more idea about Microsoft WebMatrix.

Web Development 101 using WebMatrix
http://www.microsoft.com/web/post/web-development-101-using-webmatrix

Installing and publishing a WordPress blog with WebMatrix
http://www.microsoft.com/web/category/wordpress

Watch videos from the WebMatrix launch at CodeMash 2011
http://www.microsoft.com/web/enter/

Thanks!!!!

Friday, March 25, 2011

How To: Create Add-In to modify the source code in Visual Studio

Create Add-In in visual studio

We can extend the existing Visual Studio environment by automation techniques provided by Visual Studio and DotNet framework. We can add our own features to the Visual Studio by creating Add-Ins. Add-In is a complied DLL which run in visual studio IDE.

Here in this blog post we are going to create an Visual Studio Add-In which will;

1. Find and change the name of the Namespace defined in c# source code
2. Find and change the name of the Class defined in c# source code
3. Find and change the name of the Property defined in the c# source code
4. Find and change the name of the function defined in the c# source code
5. Find the attributes defined in a class and if required we can change the values defined inside the attribute
6. Find the attributes of properties defined in a c# source code file.
7. And more over we will be able to iterate through the C# source code and find out the Namespace, Class, Functions, Properties, Delegates, Events and Struct, we can do any kind of manipulation with in the code.

How to create Visual studio Ad-In:

Open VS 2008 got to File Menu -> New -> Project -> Other Project Type -> Extensibility -> Select Visual Studio Add-In

My project name is "VSAddIn".  Click on "Ok" you will get the following screen click on next

1

Select the programming language you use as shown in figure below

2

Select the application host as shown in figure below

3

Enter the name and description as shown in below image

4

You have certain option to configure the way Add-In shows in VS IDE.

Check the first option to show the Add-In in Tools Menu Items
Check the second option to start the Add-In when host application startup
Third one, keep it unchecked

5

I don't have About information

6

Finally, Click on finish

7

Following is my code to implement the above described.

   1: public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
   2:         {
   3:             handled = false;
   4:             if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
   5:             {
   6:                 if(commandName == "VSAddIn.Connect.VSAddIn")
   7:                 {
   8:                     handled = true;
   9:                     // Call the function which will do the code analysis and 
  10:                     // modification to your c# code 
  11:                     AnalyseAndModifyCode();
  12:                     return;
  13:                 }
  14:             }
  15:         }

 




   1: private void AnalyseAndModifyCode()
   2:         {
   3:             foreach (Project project in this._applicationObject.Solution.Projects)
   4:             {
   5:                 foreach (ProjectItem pItem in project.ProjectItems)
   6:                 {
   7:                     AnanlyseAndModifyRecursive(pItem);
   8:                 }
   9:             }
  10:         }




   1: private void AnanlyseAndModifyRecursive(ProjectItem pItem)
   2:         {
   3:             if (pItem.Name.EndsWith(".cs"))
   4:             {
   5:                 FileCodeModel2 model = (FileCodeModel2)pItem.FileCodeModel;
   6:                 if (model != null)
   7:                 {
   8:                     foreach (CodeElement codeElement in model.CodeElements)
   9:                     {
  10:                         ExamineCodeElement(codeElement);
  11:                     }
  12:                 }
  13:             }
  14:             foreach (ProjectItem pChileItem in pItem.ProjectItems)
  15:             {
  16:                 AnanlyseAndModifyRecursive(pChileItem);
  17:             }
  18:         }






   1: private void ExamineCodeElement(CodeElement codeElement)
   2:         {
   3:             try
   4:             {
   5:                 if (codeElement.Kind == vsCMElement.vsCMElementClass)
   6:                 {
   7:                     CodeClass cClass = (CodeClass)codeElement;
   8:                     MessageBox.Show("Class name is " + codeElement.Name);
   9:                     
  10:                     foreach (CodeAttribute attr in cClass.Attributes)
  11:                     {
  12:                         // Name of the attribute
  13:                         if (!string.IsNullOrEmpty(attr.Name))
  14:                         {
  15:                             MessageBox.Show("Attributes Defined for class " + codeElement.Name + " is " + attr.Name);
  16:                         }
  17:                         // Values Defied in attribute
  18:                         MessageBox.Show("Value of attribute is " + attr.Value);
  19:                     }
  20:  
  21:                     // To change the name of the class defined in the c# source code file 
  22:                     cClass.Name = "NewClassName";
  23:                 }
  24:                 if (codeElement.Kind == vsCMElement.vsCMElementProperty)
  25:                 {
  26:                     CodeProperty cProperty = (CodeProperty)codeElement;
  27:                     MessageBox.Show("Property name is " + codeElement.Name);
  28:                     foreach (CodeAttribute attr in cProperty.Attributes)
  29:                     {
  30:                         if (!string.IsNullOrEmpty(attr.Name))
  31:                         {
  32:                             MessageBox.Show("Attributes Defined for property " + codeElement.Name + " is " + attr.Name);
  33:                         }
  34:                     }
  35:  
  36:                     // To change the name of the propert defined in the c# source code file 
  37:                     cProperty.Name = "NewClassName";
  38:                 }
  39:  
  40:                 if (codeElement.Kind == vsCMElement.vsCMElementFunction)
  41:                 {
  42:                     CodeFunction cFucntion = (CodeFunction)codeElement;
  43:                     MessageBox.Show("Function name is " + codeElement.Name);
  44:  
  45:                     foreach (CodeAttribute attr in cFucntion.Attributes)
  46:                     {
  47:                         if (!string.IsNullOrEmpty(attr.Name))
  48:                         {
  49:                             MessageBox.Show("Attributes Defined for function " + codeElement.Name + " is " + attr.Name);
  50:                         }
  51:                     }
  52:  
  53:                     // To change the name of the property defined in the c# source code file 
  54:                     cFucntion.Name = "NewcFucntionName";
  55:                     
  56:                 }
  57:  
  58:                 if (codeElement.Kind == vsCMElement.vsCMElementNamespace)
  59:                 {
  60:                     CodeNamespace cNamespace = (CodeNamespace)codeElement;
  61:                     MessageBox.Show("Namespace name is " + codeElement.Name);
  62:  
  63:                     // To change the name of the namespace defined in the c# source code file 
  64:                     cNamespace.Name = "NewcNameSpaceName";
  65:                 }
  66:  
  67:                 if (codeElement.Kind == vsCMElement.vsCMElementStruct)
  68:                 {
  69:                     CodeStruct CodeStruct = (CodeStruct)codeElement;
  70:                     MessageBox.Show("Struct name is " + codeElement.Name);
  71:  
  72:                     // To change the name of the Struct defined in the c# source code file 
  73:                     CodeStruct.Name = "NewcStructName";
  74:                 }
  75:  
  76:                 if (codeElement.Kind == vsCMElement.vsCMElementEnum)
  77:                 {
  78:                     CodeEnum CodeEnum = (CodeEnum)codeElement;
  79:                     MessageBox.Show("Enum name is " + codeElement.Name);
  80:  
  81:                     // To change the name of the Enum defined in the c# source code file 
  82:                     CodeEnum.Name = "NewcEnumName";
  83:                 }
  84:  
  85:                 if (codeElement.Kind == vsCMElement.vsCMElementDelegate)
  86:                 {
  87:                     CodeDelegate CodeDelegate = (CodeDelegate)codeElement;
  88:                     MessageBox.Show("Delegate name is " + codeElement.Name);
  89:  
  90:                     // To change the name of the delegate defined in the c# source code file 
  91:                     CodeDelegate.Name = "NewcDelegateName";
  92:                 }
  93:  
  94:                 if (codeElement.Kind == vsCMElement.vsCMElementEvent)
  95:                 {
  96:                     CodeEvent CodeEvent = (CodeEvent)codeElement;
  97:                     MessageBox.Show("Event name is " + codeElement.Name);
  98:  
  99:                     // To change the name of the Event defined in the c# source code file 
 100:                     CodeEvent.Name = "NewcEventName";
 101:                 }
 102:  
 103:                 foreach (CodeElement childElement in codeElement.Children)
 104:                 {
 105:                     ExamineCodeElement(childElement);
 106:                 }
 107:             }
 108:             catch
 109:             {
 110:                 //
 111:             }
 112:         }

You can find the complete source code here. The .Zip file contains two projects one is the Add-In application and the other one is a sample project to test the Add-In functionalities.


To test the project first follow the below steps:


1. Build the “VSAddIn” project.


2. Once you build the solution you will get a Add-In File called “VSAddIn.AddIn” in your project folder. Copy the file and got to My Documents –> Visual Studio 2008 –> Add-Ins and paste it there. 


3. Edit that Add-In file in notepad. You can find the xml tag <Assembly>Project1.dll</Assembly>. Change the path to the Project1.dll file in you project debug folder. Now open the sample project application go to Tools menu You can find the Add-In “VSAddIn” .


4. Again go back to the Add-In Application project got to Tools Menu Click on Attach process. A number of processes will be listed over there, select the sample project application. Now the Add-In application is ready to debug.


5. Now got to the sample project application and select the “VSAddIn”.


If you need further assistance on how to extend this article according to your need please let me know via comments.


Thanks!!!

Sunday, March 13, 2011

How To: Silverlight application for searching and playing YouTube videos

Recently I have created an application to search and view the YouTube videos in silverlight.  For

this we need to get the key to access the YouTube APIs. If you have Google email address you can

get the key from here.

http://code.google.com/apis/youtube/dashboard/

Here you have to enter the name, description and website to host the application. Once you get

the key you add to the web.config file.

Once you have the key to YouTube APIs you will be able to make calls to the YouTube API service.

You can find the .NET library for the Google Data API here

http://code.google.com/p/google-gdata/downloads/list

Before getting video information's from YouTube we need do the authentication by calling the

function YouTubeRequestSettings with the application name, key, user name and password.

In this post am searching the YouTube based on keywords entered in the search textbox. You guys can do further research over here.

http://code.google.com/apis/youtube/overview.html

For playing the YouTube video we are using the web browser control in silverlight 4.0. But the limitation for web browser is it will work only in out of browser mode.  I have set the application to run in out of browser mode.  To run the application in out of browser mode we need to change the setting in project solution properties. Under the Silverlight tab, check “Enable running application out of the browser” and click the “Out-of-Browser settings" button as circled below in the screenshot.

image_thumb_3C972566

You can find the source code here

Thanks guys!!!

Thursday, March 10, 2011

Post 2 - How to: Speedup the heavy data loading from WCF service to datagridview in c# improvements on the client side application

In my previous post on speed up the heavy data loading in datagridview, even though the data is coming to the client in separate chunks the grid is loading only after the data is loaded completely. In this article I am solving that issue using a separate thread to refresh the grid as and when data is pushed to the Data Table.

Following is the code change in the client side application:

Initialize the following variables.

   1: public DataTable dtCustomers { get; set; }
   2:         delegate void Bind_DataTable_to_GridView_Delegate(DataTable dt);
   3:         Thread tBindGrid;


Following is the code change in client side



   1: public Form1()
   2:         {
   3:             InitializeComponent();
   4:  
   5:             // Initilaize the customer data table
   6:             dtCustomers = new DataTable();
   7:             dtCustomers.Columns.Clear();
   8:             dtCustomers.Columns.Add("FirstName");
   9:             dtCustomers.Columns.Add("LastName");
  10:             dtCustomers.Columns.Add("Address");
  11:             // set datasource of the datagridview
  12:             dataGridView1.DataSource = dtCustomers;
  13:             // Initialize the thread with the thread function
  14:             tBindGrid = new Thread(GetAllCustomers);
  15:  
  16:             tBindGrid.SetApartmentState(ApartmentState.STA);
  17:             // Start the thread 
  18:             tBindGrid.Start();
  19:         }
  20:  
  21:         // Thread function
  22:         private void GetAllCustomers()
  23:         {
  24:             DataTable dt = new DataTable();
  25:             dt.Columns.Clear();
  26:  
  27:             dt.Columns.Add("FirstName");
  28:             dt.Columns.Add("LastName");
  29:             dt.Columns.Add("Address");
  30:  
  31:             ChannelFactory<IService> factory = new ChannelFactory<IService>("localhost");
  32:             IService service = factory.CreateChannel();
  33:  
  34:             System.ServiceModel.Channels.Message message = service.GetCustomers();
  35:  
  36:             foreach (List<Customer> customers in GetAllCustomers(message))
  37:             {
  38:                 foreach (Customer item in customers)
  39:                 {
  40:                     DataRow dr = dt.NewRow();
  41:                     dr["FirstName"] = item.FirstName;
  42:                     dr["LastName"] = item.LastName;
  43:                     dr["Address"] = item.Address;
  44:                     
  45:                     dt.Rows.Add(dr);
  46:                 }
  47:                 bind_DataTable_to_GridView(dt);
  48:                 dt.Clear();
  49:             }
  50:         }
  51:  
  52:         // function for binding the datatable datagridview in seperate thread
  53:         private void bind_DataTable_to_GridView(DataTable dt)
  54:         {
  55:  
  56:             if (dataGridView1.InvokeRequired)
  57:             {
  58:                 Bind_DataTable_to_GridView_Delegate del = new Bind_DataTable_to_GridView_Delegate(bind_DataTable_to_GridView);
  59:                 dataGridView1.Invoke(del, new object[] { dt });
  60:             }
  61:             else
  62:             {
  63:                 dtCustomers.Merge(dt);
  64:                 dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;               //Autosizes the gridview 
  65:                 foreach (DataGridViewColumn dgvcol in dataGridView1.Columns)
  66:                 {
  67:                     dgvcol.SortMode = DataGridViewColumnSortMode.NotSortable;
  68:                 }
  69:             }
  70:         }
  71:  
  72:         static IEnumerable<List<Customer>> GetAllCustomers(System.ServiceModel.Channels.Message message)
  73:         {
  74:             XmlReader reader = message.GetReaderAtBodyContents();
  75:  
  76:             XmlSerializer serializer = new XmlSerializer(typeof(Customer[]));
  77:             reader.ReadStartElement("Customer");
  78:  
  79:             while (!reader.EOF && reader.LocalName == "ArrayOfCustomer")
  80:             {
  81:                 Customer[] customer = (Customer[])serializer.Deserialize(reader);
  82:                 yield return customer.ToList<Customer>();
  83:             }
  84:  
  85:             reader.ReadEndElement();
  86:         }

For the time being I have not uploaded the updated source code. You can change the existing source code which is uploaded here. If you need any help please lete me know.


Thanks guys!!!


Anilal Sambasivan