I have develop an application in which i am taking the data from one table which contain only 1 column which contain the table name.
Motive of the application is if this table contain 2 records(fetch 2 table name)
then i have to fetch the records from these table then insert into csv file
here i will be creating csv file
My Application is running smooth
And it is creating empty csv files
for Debugging purpose i have written messagebox.show();
In this it is showing data properly
But when i use sw.writeline();
it writes empty data

following is my code

namespace Contract_db_to_csv
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection connection_string = new SqlConnection("Data Source= 192.168.25.12; Initial Catalog = aceweb; user id = dbaadmin; password = Mumb@!@123");
            DataTable dt = new DataTable();
            DataTable dt1 = new DataTable();
            string query = "select SYMBOL from Contract_Mastar";

            
            try
            {
                //connection_string = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["database"]);    
                
                connection_string.Open();
                using (SqlCommand cmd = new SqlCommand(query, connection_string))
                {
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        dt = new DataTable();
                        dt.Load(dr);
                    }
                }

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    saveFileDialog1.ShowDialog();
                    string filename = saveFileDialog1.FileName;
                    StreamWriter sw = new StreamWriter(filename);
                    string table_name = Convert.ToString(dt.Rows[i]["Symbol"]);

                    string querytoexport = "Select * from " + table_name;

                    using (SqlCommand cmd = new SqlCommand(querytoexport, connection_string))
                    {
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            dt1 = new DataTable();
                            dt1.Load(dr);
                        }
                    }
                    bool writeheader = true;
                    string[] arr = new String[dt1.Columns.Count];
                    if (writeheader)
                    {
                        for (int a = 0; a < dt1.Columns.Count; a++)
                        {
                            arr[a] = dt1.Columns[a].ColumnName;
                            arr[a] = GetWriteableValue(arr[a]);
                            
                        }
                        sw.WriteLine(string.Join(",", arr));
                        MessageBox.Show(string.Join(",", arr));
                    }

                    for (int j = 0; j < dt1.Rows.Count; j++)
                    { 
                        string[] dataArr = new String[dt1.Columns.Count]; 
                        for (int k = 0; k < dt1.Columns.Count; k++) 
                        {   object o = dt1.Rows[j][k];
                            dataArr[k] = GetWriteableValue(o);
                            //sw.WriteLine(string.Join(",", dataArr));
                            
                        }
                        sw.WriteLine(string.Join(",", dataArr));
                        MessageBox.Show(string.Join(",", dataArr));

                    }//for (int j = 0; j < dt.Rows.Count; j++)

                } //main for loop
                MessageBox.Show("Done");
            }//try
            catch (Exception er)
            {
                MessageBox.Show("Error : " + er.Message);
            }//catch
            finally
            {
                //connection_string.Close();
            }//finally

        } //private void button1_Click(object sender, EventArgs e)

        public static string GetWriteableValue(object o)
        {
            if (o == null || o == Convert.DBNull)
            {
                return "";
            }
            else
            {
                if (o.ToString().IndexOf(",") == -1)
                {
                    return o.ToString();
                }
                else
                {
                    return "\"" + o.ToString() + "\"";
                }
            }
        }
    }
}

I am not getting what is wrong

This article has been dead for over six months. Start a new discussion instead.