Breaking News

Editors Picks

Tuesday, November 8, 2011

Writing a DataTable into a text file in asp.net c#


    public void Writewowfileintxt(DataTable dt, string filePath)
    {

        int i = 0;
        StreamWriter sw = null;
        try
        {
            TextInfo UsaTextInfo = new CultureInfo("en-US", false).TextInfo;
            sw = new StreamWriter(filePath, false);
            DataTable dtcolunm = clsd.getTablecolumn(tablename);
            for(i = 0; i < dt.Columns.Count - 1; i++)

                {

                    sw.Write(dt.Columns[i].ColumnName + ” | ”);

                }

                sw.Write(dt.Columns[i].ColumnName);

                sw.WriteLine();
            char[] charsToTrim = { ' ' };
            foreach (DataRow row in dt.Rows)
            {
                object[] array = row.ItemArray;
                for (i = 0; i < array.Length - 1; i++)
                {
       sw.Write(array[i].ToString);                       
                }
                sw.Write(array[i].ToString);
                sw.WriteLine();
            }
            sw.Close();
        }
        catch (Exception ex)
        {
        }
    }

No comments :

Post a Comment

Contact Us

Name

Email *

Message *