Tuesday, February 26, 2013

Set up ASP.NET web application default start up page in IIS 7.0

It's essential you need to set the starting page of your ASP.net application in IIS 7.0. hear i have provided the solution to do that

  • Go to IIS 7.0
  • Select the Published ASP.NET application
  • In the right side double click the Default document
  • There add your Start up page


(Solved) Error: HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.


When you publish a ASP.NET application in IIS 7.0 you may getting this error.

HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
To Solve this,

  • Add a Start up page for your published application in IIS.





(Solved) Error: Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. An attempt was made to load a program with an incorrect format.

When you publish a ASP.NET application in IIS you may getting this error.

"Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. An attempt was made to load a program with an incorrect format." To Solve this,


  • Go to IIS
  • Double click the Application Pool
  • Right click the application pool you are using for your application
  • Select Advanced Settings
  • There you can see Enable 32 bit Application set to False
  • Change that to True 
  • It will work now

Saturday, February 9, 2013

Encrypt a Password string in C Sharp (C#)

When you develop a secured ASP.NET web application you need to encrypt the password before it getting stored in database. Encryption means converting input data into an unreadable format.

Here is a simple code to encrypt a given string. You can use it in your project.
public string EncryptIt(string inputString)  
{  
    try  
    {  
        byte[] encryptedByteData= new byte[inputString.Length];  
        encryptedByteData= System.Text.Encoding.UTF8.GetBytes(inputString);  
        string encryptedData= Convert.ToBase64String(encryptedByteData);  
        return encryptedData;  
    }  
    catch (Exception ex)  
    {  
        throw ex;  
    }  
}  

Friday, February 8, 2013

Create CSV file from a DataTable in CSharp (C#)

While working as a .net programmer you may getting a requirement to convert a DataTable to CSV file format.

CSV - Comma Separated Value. Tabular format data are converted into comma separated values text file with the extension of csv. See sample below

Name, Age, Eid, Native
Raj, 27, E0025, Chennai 
Kumar, 20, E0032, Nagercoil
Balu, 22, E0024, Mumbai
Raj, 25, E0026, Bangalore

To achieve this in C#

public void CreateCSVFile(DataTable dt, string strFilePath)  
{  
    try  
    {  
        StreamWriter sw = new StreamWriter(strFilePath, false);  
        int columnCount = dt.Columns.Count; 
 
        for (int i = 0; i < columnCount ; i++)  
        {  
            sw.Write(dt.Columns[i]); 
 
            if (i < columnCount - 1)  
            {  
                sw.Write(",");  
            }  
        }  

        sw.Write(sw.NewLine);  
         
        foreach (DataRow dr in dt.Rows)  
        {  
            for (int i = 0; i < columnCount ; i++)  
            {  
                if (!Convert.IsDBNull(dr[i]))  
                {  
                    sw.Write(dr[i].ToString());  
                }  

                if (i < columnCount - 1)  
                {  
                    sw.Write(",");  
                }  
            }  

            sw.Write(sw.NewLine);  
        }  

        sw.Close();  
        }  
        catch (Exception ex)  
        {  
            throw ex;  
        }  
}  
Call the CreateCSVFile function like below by passing the DataTable to convert and the path of the CSV file to be saved.
static void Main(string[] args)
{
    CreateCSVFile(dt,"D://ConvertedFile.csv");
}

Alter a table column that has primary key and foreign key


We may getting error when we try to alter a column in SQLServer table. To handle that situation we need to delete the Primary Key and Foreign Key from the table before alter. Below is a sample code to alter datatype of a column name id from int to bigint in a table PaymentsType.

ALTER TABLE PaymentsType drop CONSTRAINT PK_PaymentsType  
ALTER TABLE PaymentsType ALTER COLUMN id bigint not null  
ALTER TABLE PaymentsType ADD CONSTRAINT PK_PaymentsType PRIMARY KEY (id)