Surendra Sharma

Surendra Sharma

Search This Blog

Tuesday, July 22, 2014

column Id is constrained to be unique. value is already present

If you are working with dataset in .NET and facing error as "column Id is constrained to be unique. value is already present."
Then simply set

datasset.EnforceConstraints = false;

It does not check the constraints at .net side. When you are going to update dataset records into database, all constraints fire at database level.

Please leave your comments or share this tip if it’s useful for you.

Tuesday, June 24, 2014

Quick guide to install and use NCache in .NET application

NCache is a distributed in-memory object cache for mission .NET applications which are hosted on web farm environment.

Here are quick steps to install and use NCache in .NET application.

·         Download and install Ncache express from http://www.alachisoft.com/download.html#ncache

·         Open C:\Program Files\NCache Express\config and change cache.config name="mytestreplicatedcache" in config.ncconf file.

·         In client.ncconf, change cache id ="mytestreplicatedcache".

·         Open Perfmon from Run window and test the Ncache counter for object mytestreplicatedcache.

·         Start cache on all nodes, one for Local Cache & two for Replicated Cache. Startcache.exe is located in “%InstallDir%\NCache Express\bin\tools” folder. Do this once for each cache server:
Startcache  cache-id /s serverName

C:\Program Files\NCache Express\bin\tools > Startcache  mytestreplicatedcache /s SERVERNAME

·         Run “AddTestData” command-line program provided with NCache to add some test data to the cache. It is located in “%InstallDir%\NCache Express\bin\tools” folder. Following command adds 100 items of size 1024 bytes each:

AddTestData  cache-id /c 1000 /s 1024

C:\Program Files\NCache Express\bin\tools > AddTestData  mytestreplicatedcache /c 1000 /s 1024

·         Check Green marked Additions/sec counter in performance counter that, it is showing value for addition of 1000s items.

·         After 5 min., check blue marked Expirations/sec counter in performance counter that, it is showing items going to expire.

·         If the above operations succeed, then you can rest assured that you’ve configured the cache correctly. You can either let the “test data” expire in 5 minutes of manually clear the cache with the following command (again in “%InstallDir%\NCache Express\bin\tools”)

ClearCache cache-id

C:\Program Files\NCache Express\bin\tools > ClearCache mytestreplicatedcache

·         Open web application in visual studio add reference of "Alachisoft.NCacheExpress.Web.dll" from C:\Program Files\NCache Express\bin\assembly\clr20

·         Mark all entities that need to be cached as [Serializable].

Please leave your comments or share this guide if it’s useful for you.

Wednesday, April 16, 2014

15 Apr 2014 6:00 PM | Magical number 10,000

15 Apr 2014 6:00 PM is remarkable day for my blog as it crossed magical number 10,000+ visitors with 150+ technical articles.

Hope it will grow to 1,00,000+ in near future.

Tuesday, April 8, 2014

How to use SQL Server Profiler in easy steps

Most of the .NET developers have to deal with SQL server. Some may be good in database while most of the developers are great in front end but know only the basic knowledge of SQL Server database.

During development, these developers have to face the database performance issues. For most of the developers this database query performance kind of tasks are bouncers. 

Here I am trying to explain how you can use SQL Server profiler for identifying query performance.

Suppose I have to check performance of below queries

SELECT * FROM TableA
GO
SELECT * FROM TableB
GO
SELECT * FROM TableC
GO

1.    First open SQL Server Profiler from Tools -> SQL Server Profiler







2.    Login to SQL Server Profiler and click on Events Selection tab of Trace Properties window.

 

3.    Uncheck all events in Events Selection tab except SQL:BatchCompleted. Select Show All Columns radiobutton and click on Column Filters… button.

 

4.    Select HostName filter from Edit Filter screen and click on Like option. Enter here filter value as ‘%ComputerName%’. Click Ok.

 

5.    Click Run.
6.    Now run queries in SQL Server.
7.    Check status of all queries in SQL Server profiler as below

 

8.    The most important data are CPU, Reads, Writes and Duration. Here higher figures of these columns show the action required for query optimization.


Please leave your comments or share this article if it’s useful for you.

Wednesday, April 2, 2014

No server is available to process the request in NCache

If you are getting error "No server is available to process the request" for NCache then follow below steps to fix it.

·         Restart NCache Express from windows service
·         Go to command prompt and start cache as
Startcache  cache-id /s serverName


Please leave your comments or share this tip if it’s useful for you.

Monday, March 31, 2014

How to download file via FTP in ASP.NET using C#

In almost all ASP.NET web application, we have to write a code to download file from FTP server.

Here is simple and full code to download file via FTP.

It uses FTP web request and response stream for connecting and downloading file from FTP server by using credentials.

public static bool FTPDownloadFile(string sAddress, string sUserName, string sPassword, bool isUsePassive, string sDownloadFileName, string sServerFileName)
{
    Stream ftpStream = null;
    int bufferSize = 2048;
    bool bReturnValue = false;

    FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(sAddress + @"/" + sServerFileName);

    ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
    ftpRequest.Proxy = null;
    ftpRequest.UseBinary = true;
    ftpRequest.KeepAlive = false;
    ftpRequest.EnableSsl = isSSL;
    ftpRequest.Credentials = new NetworkCredential(sUserName, sPassword);
    ftpRequest.UsePassive = isUsePassive;

    FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
    ftpStream = ftpResponse.GetResponseStream();
    FileStream localFileStream = new FileStream(sDownloadFileName, FileMode.Create);
    byte[] byteBuffer = new byte[bufferSize];
    int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);

    try
    {
        while (bytesRead > 0)
        {
            localFileStream.Write(byteBuffer, 0, bytesRead);
            bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
        }
        bReturnValue = true;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        if (localFileStream != null)
        {
            localFileStream.Close();
        }
        if (ftpStream != null)
        {
            ftpStream.Close();
        }
        if (ftpResponse != null)
        {
            ftpResponse.Close();
        }
        ftpRequest = null;
    }

    return bReturnValue;
}


Please leave your comments or share this code if it’s useful for you.

Friday, March 28, 2014

How to download file via SFTP in ASP.NET using C#

In almost all ASP.NET web application, we have to write a code to download file from secure SFTP server.

Here is simple and full code to download file via SFTP.

First you need to download SshNet DLL from http://sshnet.codeplex.com/.

You can connect SFTP by two ways
1.    By using credentials
2.    By using Username and private key. Private Key may be bind with Pass phrase.

This code is specific to connect to SFTP by using credentials.

Note:-
·         To connect by using private key refer my another article on the blog.
·         Your private key must be compatible with SshNet. To convert any private key to SshNet compatible private key, refer my other article How to convert Private key to OpenSSH Key to connect to SFTP server.

It uses SftpClient for creating SFTP connection to server by providing SFTP URL with credentials and private key to connect to specific folder or root folder. Code read and download the file from SFTP server.

using Renci.SshNet;

private static object threadLock = new object();

public static bool SFTPDownloadFile(string Address, string UserName, int Port, string Password, string DownloadFileName, string ServerFileName)
{
    SftpClient client = null;
    bool bReturnValue = false;

    try
    {

        client = new SftpClient(Address, Port, UserName, Password);

        client.Connect();

        lock (threadLock)
        {
            using (var file = File.OpenWrite(DownloadFileName))
            {
                client.DownloadFile(ServerFileName, file);
                bReturnValue = true;
            }
        }

    }
    catch (Exception ex)
    {
        throw ex;
    }

    finally
    {
        if (client != null)
        {
            client.Disconnect();
            client.Dispose();
        }
    }
    return bReturnValue;
}


Please leave your comments or share this code if it’s useful for you.