0

The code below is to bind a dataset to an Excel file in Vb.net
One core record also binds in a .csv file
I need this code to be translated to C#

 Public Function Write2CSV(ByVal ExlDs As Data.DataSet) As String
            Dim strfilename As String, i As Integer = 0, p As Integer = 0
            Dim dRandomNo As Integer = Rnd(1) * 10000
            Dim sAppPath As String = System.AppDomain.CurrentDomain.BaseDirectory
            Dim sFile As String = "RepFile\Dispatch" & Format(Now, "ddMMyyyyHHmmss") & dRandomNo & ".csv"
            Dim sOpnURL As String
            TextBox1.Text = sFile
            strfilename = sAppPath & "Reports\" & sFile
            Dim swObj As StreamWriter

            Try
                swObj = File.AppendText(strfilename)
                For Each colObj As DataColumn In ExlDs.Tables(0).Columns
                    swObj.Write(colObj.ColumnName & ",")
                Next
                swObj.WriteLine()

                For intRow As Integer = 0 To ExlDs.Tables(0).Rows.Count - 1
                    For intCol As Integer = 0 To ExlDs.Tables(0).Columns.Count - 1
                        swObj.Write(ExlDs.Tables(0).Rows(intRow)(intCol) & ",")
                    Next
                    swObj.WriteLine()
                Next

                Dim strFileURL() As String = Split(sFile, "\")
                sOpnURL = "RepFile/" & strFileURL(UBound(strFileURL))
                'sbObj.Save(strfilename)

            Catch ex As Exception
                'bError = True
                Dim ErrContext As HttpContext = HttpContext.Current
                ErrContext.Items.Add("ErrDesc", ex.Message)
                ErrContext.Items.Add("ErrSrc", ex.Source)
                ErrContext.Items.Add("ErrInfo", ex.StackTrace)
                ErrContext.Items.Add("ErrFile", "T")
            Finally
                'sbObj = Nothing
                swObj.Close()
                swObj = Nothing
            End Try
            Return sOpnURL
        End Function

Help is highly appreciated

3vts
  • 778
  • 1
  • 12
  • 25
Manohar A
  • 27
  • 2

1 Answers1

0

Hello @Manohar_A I think what you need is to translate your code to C#.
If that is the case here you have the C# version of your function.

    public string Write2CSV(System.Data.DataSet ExlDs)
    {
        using (ExlDs)
        {
            string strfilename;
            var Rnd = new Random();
            var dRandomNo = Rnd.Next(1, 10000);
            var sAppPath = System.AppDomain.CurrentDomain.BaseDirectory;
            var sFile = @"RepFile\Dispatch" + DateTime.Now.ToString("ddMMyyyyHHmmss") + dRandomNo + ".csv";
            var sOpnURL = string.Empty;
            TextBox1.Text = sFile
            strfilename = sAppPath + @"Reports\" + sFile;
            StreamWriter swObj = null;

            try
            {
                swObj = File.AppendText(strfilename);
                foreach (System.Data.DataColumn colObj in ExlDs.Tables[0].Columns)
                {
                    swObj.Write(colObj.ColumnName + ",");
                }
                swObj.WriteLine();

                for (int intRow = 0; intRow < ExlDs.Tables[0].Rows.Count - 1; intRow++)
                {
                    for (int intCol = 0; intCol < ExlDs.Tables[0].Columns.Count - 1; intCol++)
                    {
                        swObj.Write(ExlDs.Tables[0].Rows[intRow][intCol] + ",");
                    }
                }
                var strFileUrl = Strings.Split(sFile, "\\");
                sOpnURL = "RepFile/" + strFileUrl[strFileUrl.Length];
                //sbObj.Save(strfilename);
            }
            catch (Exception ex)
            {
                //bError = true;
                var ErrContext = HttpContext.Current;
                ErrContext.Items.Add("ErrDesc", ex.Message);
                ErrContext.Items.Add("ErrSrc", ex.Source);
                ErrContext.Items.Add("ErrInfo", ex.StackTrace);
                ErrContext.Items.Add("ErrFile", "T");
            }
            finally
            {
                //sbObj = null;
                swObj.Close();
                swObj = null;
            }
            return sOpnURL;
        }
    }

Please note that you are going to need the following imports:

using Microsoft.VisualBasic;
using System;
using System.IO;
using System.Web;

Please give it a try and let me know your comments

3vts
  • 778
  • 1
  • 12
  • 25