Blog Posts

Thursday, November 10, 2011

ASP.Net chatting using WCF Services and JSon


This post is upgrade to my previous article 'Chat Application in ASP.NET Using AJAX (Pop-up Windows)', which has some disadvantages like storing chat messages in DB and little complex in adding to a website.  I implemented WCF services in place of DB to make it easy for adding to a web site.  JSon is used for communication between server and client.

Please read complete article at http://www.codeproject.com/KB/aspnet/WCFChat_JSon_Popups.aspx.

Sunday, March 27, 2011

Scheduling SQL Server backup to remote computer using SSIS

Step 1 Create an SSIS package from SQL Server Business Intelligence development studio:
In maintenance for our database backups, We had to schedule backups and copy the backup files to a remote computer for our System Administrator to take the files to a tape.  I thought SSIS could be helpful here. The key point in my post is to run the SQL Server Agent with a domain user account and give permissions on the destination folder at remote system for backup.  And make sure that the user account password never expires.


To create a SSIS Package open SQL Server Business Intelligence Development Studio  -
Create project from template - Business Intelligence Projects > Integration Services Project
From tool box, drag the task ‘Backup Database Task’ (under Maintenance Plan Tasks) to create a plan for backup of the database to a path.
From tool box drag – ‘File System Task’, to copy the backup file from the source (which contains the backup file) to destination select operation from one of the ‘Copy directory’ or ‘Copy File’.  And you can also delete the contents of the destination if you don't want the old backups with a another 'File System Task' as Clear Directory.

Check for Permissions on destination if it is on remote system:  
Make sure that the account on which the SQL Server Agent is running should have the permissions on the destination folder in the remote computer (Check the sharing permissions and security tabs in the properties window of the destination folder from windows explorer).  A domain user will work for giving permissions on the network shared drive.  
You can check the user account on which the SQL Server Agent is working from the Services under Computer Management (right click on My Computer and click Manage).  

Build Package: 
Build the project to create the package to schedule in SQL Server Agent.

Step 2 Scheduling the SSIS Package:
Open SQL Server Management Studio, in the object explorer window find SQL Server Agent (it should be running) and expand.  Right click on Jobs and click on ‘New Job’ to open the wizard. 


Enter name and other details in ‘General’ tab.  In steps click ‘New...’, it will open the window to add our package.  Select ‘SQL Server Integration Services Package’ from Type and File System from ‘Package source’.  And browse for the package path from the below.  Click ‘OK’.
You can schedule the package from Schedules tab on left to run the package at a scheduled time.


Monday, March 21, 2011

Tweaking Infragistics WebHtmlEditor

One of the requirements in my project was to do some changes to the WebHtmlEditor in ASP.Net page.  I thought it could be useful for others if I shared the code. 


To disable the toolbar buttons of WebHtmlEditor:
Here I am disabling the Open button of toolbar.  We can identify the buttons using the method FindByKeyOrAction() of the control.  Every button on toolbar is identified by the Action Name.  For open page it is "Open".


infraEditor.FindByKeyOrAction("Open").Enabled = false;




Similarly some actions - InsertImage, InsertFlash and InsertWindowsMedia.


Adding fonts to the fonts dropdown:
We can add the fonts to the fonts dropdown in addition to the existing basic fonts. To add 'Times New Roman' to the font names drop down-


ToolbarDropDown ddlFonts = (ToolbarDropDown)infraEditor.FindByKeyOrAction("FontName");
ddlFonts.Items.Add(new ToolbarDropDownItem("Times New Roman","Times New Roman"));

Similarly to add all the windows registered fonts-

ToolbarDropDown ddlFonts = (ToolbarDropDown)infraEditor.FindByKeyOrAction("FontName");
InstalledFontCollection fonts = new InstalledFontCollection();
foreach (FontFamily family in fonts.Families)
{
    ddlFonts.Items.Add(new ToolbarDropDownItem(family.Name,family.Name));
}
Inserting Image:
For inserting images, first we need to set the UploadedFilesDirectory property to tell the control to save the uploaded files by user.  So that we the control can refer these files by a virtual path.



<ighedit:webhtmleditor height="450" id="infraEditor" runat="server" uploadedfilesdirectory=".\UploadedFiles" width="850">
When you run the application and click on the insert image, it will show a dialog box and can upload images and insert through it.


Similarly we can insert the flash and windows media also.


Monday, January 31, 2011

Overflow issue in Infragistics WebHtmlEditor

I have a web page with WebHtmlEditor (of the Infragistics with version 10.3.20103). It work fine with browser IE7 but was not showing the scrollbar when the content is more than the window height.  


To fix the issue we can set the CSS style of the TextWindow ( the content editor of the control).  Select the WebHtmlEditor control and in properties window under the 'TextWindow' set the 'CssClass' to your own css class.  In this example I wrote one class - 'infrWebEditor'.  


Identified that when you use the properties window, Visual Studio is generating all the other property tags and was taking time while opening in design mode, instead of setting in the properties window you can copy the below tag in between the <ighedit:WebHtmlEditor> tags -
<TextWindow CssClass="infrWebEditor"/>


And my CSS Class contains the property 'overflow' set to 'auto'. (you can write your own properties to set the control to look as you want).

.infrWebEditor{
    overflow:auto;
}


And the end result is - 


Happy coding.... :-)

Thursday, January 13, 2011

Working around for CA2000 (Microsoft.Reliability) while returning Dataset

While working on a web page I had to get a Dataset from the data access layer and bind the Dataset to my grid in web page.  
I wrote the code in data access layer method - 

   Public Shared Function GetDetails(ByVal strEmpName As String) As DataSet
        Dim dsResult As DataSet = New DataSet()
        Try
           
 ' my code for constructing the command and dataadapter 'da'
 da.Fill(dsResult)
 
        Catch ex As Exception
 
        Finally
 ' some cleanup
        End Try
        Return dsResult
    End Function

Simple but when I compile was getting the warning message - 'CA2000 : Microsoft.Reliability : In method 'GetDetails(String)', call System.IDisposable.Dispose on object 'dsResult' before all references to it are out of scope. xyz.vb'
Without clearing this warning I can not check in. refer 'http://msdn.microsoft.com/en-us/library/ms182289.aspx'. Cause - 'A local object of a IDisposable type is created but the object is not disposed before all references to the object are out of scope.'  The idea behind the CA2000 rule is to free memory when scope is ended.  But what about the object which we need to return to the calling method.  While writing code make sure not to initialize the IDisposable objects; and create/allocate only when returning, so that the method returns the object with proper data only when executed successfully.  In other cases (like exception or no data) return Nothing (null in C#) so that the memory is used effectivily.
For the solution I modified to initialize the DataSet to 'Nothing'
   Public Shared Function GetDetails(ByVal strEmpName As String) As DataSet
        Dim dsResult As DataSet = Nothing
        Try
           
 ' my code for constructing the command and dataadapter 'da'
 da.Fill(dsResult)
 
        Catch ex As Exception
 
        Finally
 ' some cleanup
        End Try
        Return dsResult
    End Function
Later I can check for the Nothing and bind to grid.
   If Not dsResult Is Nothing Then
        'bind to grid
   End If

Wednesday, December 22, 2010

WaveMaker for Cloud Applications in Java

My friend who was a Java Developer was very much interested in Cloud Development, while searching for the Cloud Application development tools/API.  Thought of forwarding to him - 


More on -
http://www.wavemaker.com/


http://en.wikipedia.org/wiki/Wavemaker


Few lines from wikipedia - 

WaveMaker (formerly known as ActiveGrid) is an open source software development platform that automates much of the process for creating Java web and cloud applications. WaveMaker provides a visual rapid application development platform and is available as a free open source software download or as a hosted cloud development environment (akaPlatform as a Service) running on Amazon EC2.[1]


From WaveMaker -



WaveMaker is the only open and easy-to-use development platform for web and cloud applications. With WaveMaker's visual, drag and drop tools, any developer can start building enterprise Java applications with minimal training. WaveMaker creates standard Java applications, boosting developer productivity and quality without compromising flexibility.
WaveMaker applications are cloud-ready and include built-in support for multi-tenancy and elastic scaling. WaveMaker is a rapidly-growing company backed by a 15,000-strong developer community. WaveMaker customers like the Center For Disease Control, Macy's and KANA have built rich internet applications with minimal learning curve and up to 98% less code.

Sunday, December 19, 2010

Grouping in LINQ

Grouping in LINQ provides flexibility and reliability over for loops while manipulating DataTable data.  As the LINQ uses the power of Generics, we can write code that avoid issues related to type conversion.  

I am taking a simple example explain the grouping LINQ, I am loading data from xml file (it is similar to getting data from SQL server).  I have to display the data by monthly and yearly.  My xml file has the content refer end of the post.

If I need to show the data by monthly in a grid (grdSales) on my form - 



DataSet ds = new DataSet();
ds.ReadXml("Sales.xml");


var sales = from r in ds.Tables[0].AsEnumerable()
group r by new { month = r["Month"].ToString(), year = r["Year"].ToString() } into grp                


orderby grp.Key.year, grp.Key.month
     select new SalesDetails
     {
        Year = grp.Key.year,
        Month = grp.Key.month,
        Sales = grp.Sum(s => Convert.ToInt32(s["Price"].ToString()))
     };


List<SalesDetails> lstSales = sales.ToList();

//adding total row
lstSales.Add(
sales       
      group r
(from r i n by sales into grpTotal
ails
      {
         Year =
      select new SalesDe t"Total",          Month = "",
=> s.Sales)
      }).ToList()[0]
     );
 
         Sales = grpTotal.Sum(
sgrdSales.DataSource = lstSales;



Sum() is the method from Enumerable class which is implements the the IEnumerable interface. refer the http://msdn.microsoft.com/en-us/library/system.linq.enumerable.aspx for other method of the class like Min, Max and Average.  These methods are overloaded to support datatypes like Int, Decimal etc.  And grp is the object of class which implements IGrouping.


Similarly to show data by year - 
DataSet ds = new DataSet();
ds.ReadXml("Sales.xml");
var sales = from r in ds.Tables[0].AsEnumerable()
    group r by r["Year"].ToString()into grp
    orderby grp.Key
    select new SalesDetails
    {
     Year = grp.Key,
     Month = "1",
     Sales = grp.Sum(s => Convert.ToInt32(s["Price"].ToString()))
    };


List lstSales = sales.ToList();
lstSales.Add(
      (from r in sales
       group r by sales into grpTotal
       select new SalesDetails
       {
        Year = "Total",
        Month = "",
        Sales = grpTotal.Sum(s => s.Sales)
      }).ToList()[0]
   );
grdSales.DataSource = lstSales;








One of the advantages of LINQ is, you can also have a List as one of the columns of your rows. As the following code populates a column with a List of month sales for the particular year.  






public struct MonthlySales
        {
           public string MonthName;
           public int MonthSale;
        }



var sales = from r in ds.Tables[0].AsEnumerable()
           group r by r["Year"].ToString() into grp
           orderby grp.Key
           select new SalesDetails
           {
              Year = grp.Key,
              monthSales =
                 (from rm in ds.Tables[0].AsEnumerable()
                   where rm["Year"].ToString() == grp.Key
                   select new MonthlySales
                   {
                     MonthName = rm["Month"].ToString(),
                     MonthSale = Convert.ToInt32(rm["Price"].ToString())
                   }).ToList(),
             Sales = grp.Sum(s => Convert.ToInt32(s["Price"].ToString()))
            };

The data populates like - 








My xml data from Sales.xml, and loaded data from database also should work.


<?xml version="1.0" encoding="utf-8" ?> <Sales>   <Sale><Item>item1</Item> <Price>2000</Price>  <Month>1</Month> <Year>2010</Year></Sale>   <Sale><Item>item2</Item><Price>3400</Price><Month>5</Month><Year>2010</Year></Sale>   <Sale><Item>item1</Item><Price>2200</Price><Month>7</Month><Year>2010</Year></Sale>   <Sale><Item>item1</Item><Price>1600</Price><Month>10</Month><Year>2010</Year></Sale>   <Sale><Item>item1</Item>  <Price>1200</Price> <Month>11</Month><Year>2010</Year></Sale>   <Sale><Item>item1</Item> <Price>500</Price><Month>12</Month>  <Year>2010</Year></Sale>   <Sale><Item>item1</Item><Price>2500</Price><Month>1</Month><Year>2010</Year></Sale>   <Sale><Item>item2</Item><Price>3400</Price><Month>5</Month><Year>2010</Year> </Sale>   <Sale><Item>item1</Item> <Price>2400</Price><Month>7</Month><Year>2010</Year></Sale>   <Sale><Item>item1</Item><Price>1600</Price><Month>10</Month> <Year>2010</Year></Sale>   <Sale><Item>item1</Item><Price>1000</Price> <Month>11</Month><Year>2010</Year></Sale>   <Sale><Item>item1</Item><Price>200</Price><Month>12</Month><Year>2010</Year></Sale>   <Sale><Item>item1</Item><Price>500</Price><Month>1</Month> <Year>2011</Year></Sale>   <Sale><Item>item2</Item><Price>3100</Price><Month>3</Month><Year>2011</Year></Sale>   <Sale><Item>item1</Item> <Price>2300</Price><Month>8</Month><Year>2011</Year></Sale>   <Sale><Item>item1</Item><Price>3400</Price><Month>11</Month><Year>2011</Year></Sale>   <Sale><Item>item1</Item><Price>2200</Price><Month>8</Month><Year>2011</Year></Sale>   <Sale><Item>item1</Item><Price>1200</Price> <Month>11</Month><Year>2011</Year></Sale>   <Sale> <Item>item1</Item><Price>2990</Price><Month>8</Month> <Year>2011</Year></Sale>   <Sale><Item>item1</Item><Price>1840</Price> <Month>11</Month><Year>2011</Year></Sale>   <Sale><Item>item1</Item><Price>1200</Price><Month>11</Month><Year>2011</Year></Sale>   <Sale><Item>item1</Item> <Price>6340</Price>  <Month>12</Month> <Year>2012</Year></Sale>   <Sale> <Item>item1</Item> <Price>9000</Price><Month>8</Month><Year>2012</Year></Sale>   <Sale> <Item>item1</Item><Price>1600</Price> <Month>9</Month><Year>2012</Year> </Sale>   <Sale> <Item>item1</Item><Price>1100</Price> <Month>7</Month> <Year>2012</Year></Sale>   <Sale><Item>item1</Item><Price>2300</Price><Month>8</Month><Year>2012</Year> </Sale>   <Sale><Item>item1</Item><Price>1800</Price> <Month>9</Month><Year>2012</Year></Sale>   <Sale><Item>item1</Item> <Price>1200</Price><Month>7</Month><Year>2012</Year> </Sale> </Sales>