Thursday, April 19, 2012

Launch many exes from windows service

In a real time scenario, sometimes we have to launch many exes from windows service to do a task and/or utilizing all the cores in a machine.

If the total number of exes are more, there are chances that few exes will not run. It will exit at the initial stage itself without doing task. The event log also will not have any error.

This may happen due to low desktop heap memory. In general interactive applications can use more desktop heap memory and non-interactive applications can use less desktop heap memory. This is defined in system registry.

If we launch more exes (non-interactive) from windows service, few exes may not be able to access desktop heap memory because it is low. We can increase the desktop heap memory by changing the value in registry. So that we can launch many exes without any issue.

Below is the Microsoft link where increasing the desktop heap memory is explained:
http://support.microsoft.com/kb/184802

steps to increase the heap memory in registry:
Go to Start -> Run -> type “regedit”

Go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\SubSystems
(It’s better to take Backup of registry value by right-clicking 'Subsystems' and Export.)

Double click on Windows, the “Edit String” window shows up:

The string on the Value data will look like below:
%SystemRoot%\system32\csrss.exe ObjectDirectory=\Windows
SharedSection=1024,20480,768 Windows=On SubSystemType=Windows
ServerDll=basesrv,1 ServerDll=winsrv:UserServerDllInitialization,3
ServerDll=winsrv:ConServerDllInitialization,2 ServerDll=sxssrv,4
ProfileControl=Off MaxRequestThreads=16

Increase the value in bold (from 768 to 2048), and click OK.
System reboot is required.

Tuesday, November 30, 2010

Read xml file content or node value of xml file from sql server

Using "xp_cmdshell" command we can read the contents of xml file like below. Finally the variable @FileContents will have the contents of xml file.

DECLARE @FilePath varchar(255)
DECLARE @xCmd VARCHAR(255)
DECLARE @FileContents VARCHAR(MAX)

CREATE TABLE #temp(PK INT NOT NULL IDENTITY(1,1), ThisLine VARCHAR(8000))

SET @FilePath = '\\ServerAddress.com\xmlFiles\xmlFile1.xml'
SET @xCmd = 'type ' + @FilePath
SET @FileContents = ''

INSERT INTO #temp EXEC master.dbo.xp_cmdshell @xCmd
DECLARE @x INT
DECLARE @y INT

SET @x = 0
SELECT @y = count(*) from #temp

WHILE @x <> @y
    BEGIN
        SET @x = @x + 1
        SELECT @FileContents = @FileContents + ThisLine from #temp WHERE PK = @x
    END

SELECT @FileContents as FileContents



There are different options available to read particular node value of xml file. I will explain them one by one. My default xml file will be in below format:

<?xml version="1.0" standalone="yes" ?>
<Price xmlns="http://tempuri.org/Price.xsd">
  <Rate>
     <ITEMNMBR>100</ITEMNMBR>
     <ITEMDESC>Audio Cassette</ITEMDESC>
     <QTY>1</QTY>
     <TPRICE>50</TPRICE>
  </Rate>
  <Rate>
     <ITEMNMBR>101</ITEMNMBR>
     <ITEMDESC>Video Cassette</ITEMDESC>
     <QTY>1</QTY>
     <TPRICE>100</TPRICE>
  </Rate>
</Price>

Method 1:

DECLARE @MyXML XML
SET @MyXML = @FileContents
SELECT  fileds.value('ITEMNMBR[1]', 'varchar(30)') AS ITEMNMBR,
        fileds.value('ITEMDESC[1]', 'varchar(30)') AS ITEMDESC,
        fileds.value('QTY[1]', 'varchar(30)') AS QTY,
        fileds.value('TPRICE[1]', 'varchar(30)') AS TPRICE
FROM    @MyXML.nodes('//Rate') as  xmldata(fileds)
WHERE fileds.value('ITEMNMBR[1]', 'varchar(30)') = '101'



Method 2:

CREATE TABLE #docs (pk INT PRIMARY KEY, xCol XML)
INSERT INTO #docs VALUES (1, @FileContents)

SELECT nref.value('ITEMNMBR[1]', 'nvarchar(50)') ITEMNMBR,
       nref.value('ITEMDESC[1]', 'nvarchar(50)') ITEMDESC,
       nref.value('QTY[1]', 'nvarchar(50)') QTY,
       nref.value('TPRICE[1]', 'nvarchar(50)') TPRICE      
FROM   #docs CROSS APPLY xCol.nodes('/Price/Rate') AS R(nref)
WHERE  nref.exist('.[ITEMNMBR = "100"]') = 1



Method 3:

DECLARE @idoc INT
EXEC sp_xml_preparedocument @idoc OUTPUT, @FileContents

   SELECT   *
   FROM   OPENXML (@idoc, 'Price/Rate', 2)
          WITH (ITEMNMBR  varchar(50) 'ITEMNMBR',
                ITEMDESC   varchar(50) 'ITEMDESC',
                QTY varchar(50) 'QTY',
                TPRICE varchar(50) 'TPRICE'                               
               ) R
   WHERE  R.ITEMNMBR = '101'

EXEC sp_xml_removedocument @idoc

SSRS - Clear multi-value cascading dropdown selected value

Most of us are facing problems that multi-value cascading dropdown selected value is not clearing when changing the value in parent dropdown.


For example I am having two dropdowns State and City in SSRS report. Based on State dropdown selected value, City dropdown values will be populated.

In my sample, State dropdown will have two values "Tamilnadu" and "Karnataka".
Cities for Tamilnadu will be "Trichy" and "C.City".
City for Karnataka will be "C.City".

Initially in State dropdown I will select Tamilnadu and in City dropdown I will select C.City.








Then if I select Karnataka in state dropdown city dropdown will be populated with C.City. But the problem is by default it will be selected as shown below.








But we may need that when it is populating newly, no values should be selected by default.


The solution is very simple.


In Report Parameters, city parameter has value field and label field as City. This is the root cause for this problem.
















If I change the value field to state like below, the problem will be solved.
















Because the state value is always changing. So the report will assume it as a different item. We can get the desired output.

Monday, March 1, 2010

Send Reminder mail based on Date field in SharePoint List

Below is the C# console application program to send reminder mail to user, seven days prior to due date.
It is used check WSS 3.0 list item and send mail.

Add Microsoft.SharePoint.dll in your project.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Net.Mail;

namespace MailSending
{
    class Program
    {
        static string strMailFrom = "MailId";
        static string[] strMailTo = new string[] { "MailId", "MailId" };

        static void Main(string[] args)
        {
            SPSite oSite = new SPSite("SiteURLHere");
            SPWeb oWeb = oSite.OpenWeb();
            SPList oList = oWeb.Lists["ListNameHere"];

            for (int i = 0; i < oList.Items.Count; i++)
            {
                SPListItem oListItem = oList.Items[i];
                DateTime dtDate = Convert.ToDateTime(oListItem["Due Date"]);
                DateTime dtToday = DateTime.Today;
                TimeSpan TS = dtDate.Subtract(dtToday);
                if (TS.Days == 7)
                    SendMail(oList.Items[i].ID.ToString(), Convert.ToDateTime(oListItem["Due Date"]).ToShortDateString());
            }           
        }

        static void SendMail(string strListItemID, string strDueDate)
        {
            MailMessage message = new MailMessage();
            message.Subject = "This is Subject";
            message.From = new MailAddress(strMailFrom);

            for (int j = 0; j < strMailTo.Length; j++)
                message.To.Add(strMailTo[j]);

            string strMsgBody = "Please note that Due Date is " + strDueDate + ". Please check the below list for more information.
";
            strMsgBody += "" + strDueDate + ";

            message.Body = strMsgBody;
            message.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient("SMTP_Address_Here");
            smtp.Send(message);
        }
    }
}

After compiling we can take the exe file of this application and add in windows Task Scheduler. So that it will send mail to user.

Friday, February 26, 2010

Move Files between SharePoint document library

There are different options to move files between SharePoint document libraries. We can see them one by one.

Option 1 :

We can go to Explorer view, then copy paste the files to different location.


Here we can move multiple files. But Meta data like created by and created time will be changed.


Option 2 :

We can click the file --> Send To --> Other Location

Here Meta data will be retained. But we have to move one by one.

Option 3 :

We can write a C# application.
Add Microsoft.SharePoint.dll in your project.

    using Microsoft.SharePoint;

    private void button1_Click(object sender, EventArgs e)
    {
        SPSite sourceSite = new SPSite(SourceSiteURL);
        SPWeb sourceWeb = sourceSite.OpenWeb();
        SPSite destSite = new SPSite(DestinationSiteURL);
        SPWeb destWeb = destSite.OpenWeb();

        SPFolder oFolder = sourceWeb.GetFolder(SourceFolderName).SubFolders.Folder;
        SPFolder oDestFolder = destWeb.GetFolder(DestinationFolderName).SubFolders.Folder;

        foreach (SPFile oFile in oFolder.Files)
        {
            string strDestURL = oDestFolder.Url + "/";
            SPFile f = oFile;
            f.MoveTo(strDestURL + f.Name);
        }

        sourceWeb.Dispose();
        sourceSite.Dispose();
        destWeb.Dispose();
        destSite.Dispose();
    }


You can customize the code as you need files inside folder or subfolder.
Here we can move multiple files with meta data.

Monday, February 8, 2010

Expand Infragistics Grid using javascript

 Call the expandGrid function in onclientclick event of button.
  
    function expandGrid()
    {          
        var grid = igtbl_getGridById('<%=UltraWebGrid1.ClientID%>');      
        expand(grid);
    }

    function expand(grid)
    {           
      var rowsLength = grid.Rows.length;
        for (var i = 0; i < rowsLength; i++)
        {
            var rowObj = grid.Rows.getRow(i);
     
            if(rowObj.ChildRowsCount != 0)
            {           
                rowObj.setExpanded(true);
                expand(rowObj);
            }
            else
            {
                break;
            }
        }     
    } 

Format Infragistics Grid column for Currency and Export to Excel with same format

To Format the Hierarchical grid's column from integer to currency, below code can be used.

protected void UltraGrid1_InitializeLayout(object sender, LayoutEventArgs e)
{
      UltraGrid1.DisplayLayout.Bands[0].Columns[10].Format = "$ #####0.00";
}

If we export the grid to Excel using UltraWebGridExcelExporter, that column will be integer only. To get in currency format use below code.

In Page_Load add CellExported event like below.
this.UltraWebGridExcelExporter1.CellExported += new Infragistics.WebUI.UltraWebGrid.ExcelExport.CellExportedEventHandler(UltraWebGridExcelExporter1_CellExported);

Code for CellExported event will be like below.

void UltraWebGridExcelExporter1_CellExported(object sender, Infragistics.WebUI.UltraWebGrid.ExcelExport.CellExportedEventArgs e)
{
    if (e.GridColumn.Format == "$ #####0.00")
   {            e.CurrentWorksheet.Rows[e.CurrentRowIndex].Cells[e.CurrentColumnIndex].CellFormat.FormatString =  "$ #####0.00";
   }
}