Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have been developing an artefact(Windows form application) which will take take inputs as database name,sql query,sql ip,sql id and password and will fetch the record and display in the grid view as well as make a CSV file for the same. The issue I am facing is regarding the file path. I have a function for csv file creation wherein I have passed the file path as string argument. When I call the function,it should not be hardcoded instead it should save the file in the location specified through savefiledialogue. please find the code and tell me the solution. In the Code,the file path is hardcoded. I want to call the savefiledialogue as string from the function. Thanks

C#
private void button1_Click(object sender, EventArgs e)
        {
            try
 
            {

                String str = "Server="+IPfield.Text +";"+ "database="+DBfield.Text +";"+ "UID="+IDfield.Text +";"+ "password="+pwdfield.Text;
                /*String tbl = "Table=" + textBox6.Text;*/
                //System.Console.WriteLine(str);

               
                SqlConnection con = new SqlConnection(str);

                

                String query = Queryfield.Text;
                SqlCommand cmd = new SqlCommand(query, con);
                SqlDataAdapter dataadapter = new SqlDataAdapter(query, con);
                DataSet ds = new DataSet();
                con.Open();
                /*dataadapter.Fill(ds, "VMS");*/
                dataadapter.Fill(ds, query);

                MessageBox.Show("connect with SQL server");
                MessageBox.Show("Connected");

                DataTable dt = ds.Tables[query];
               
                
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "CSV|*.csv";
                saveFileDialog1.Title = "Save a CSV File";
                //saveFileDialog1.ShowDialog();
                DialogResult dr = saveFileDialog1.ShowDialog();
                
                if (dr == DialogResult.OK)
                {
                    string strFilePath = saveFileDialog1.FileName;
                    //save file using stream.
                    StreamWriter sw = new StreamWriter(strFilePath, true);
                }
               
                //CreateCSVFile(dt, "");
                CreateCSVFile(dt ,"C:\\CSVDATA.csv");
                
                MessageBox.Show("CSV generated"); 
                                                                   
                                          
                con.Close();
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = query;
                
               

 
            }
 
            catch(Exception es)
 
            {
 
               // MessageBox.Show(es.Message);
                MessageBox.Show("Unable to Connect \nPlease Enter Correct Credentials");
               
 
 
 
            }
 
        }
        public void CreateCSVFile(DataTable dt, string strFilePath)
        {
            
            #region Export Grid to CSV
           
            // Create the CSV file to which grid data will be exported.
            StreamWriter sw = new StreamWriter(strFilePath,false);
            // First we will write the headers.
            //DataTable dt = m_dsProducts.Tables[0];
            int iColCount = dt.Columns.Count;
            for (int i = 0; i < iColCount; i++)
            {
                sw.Write(dt.Columns[i]);
                if (i < iColCount - 1)
                {
                    sw.Write(",");
                }
            }
            sw.Write(sw.NewLine);
            // Now write all the rows.
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    if (!Convert.IsDBNull(dr[i]))
                    {
                        sw.Write(dr[i].ToString());
                    }
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);
            }
            sw.Close();

            #endregion
        }
Posted
Updated 11-Mar-15 22:29pm
v2
Comments
[no name] 12-Mar-15 4:39am    
How about reading the documentation?
Richard MacCutchan 12-Mar-15 4:41am    
What is the problem, it looks like you are already doing it?
Tejas Vaishnav 13-Mar-15 6:04am    
and can you rate my answer..

1 solution

can you correct your below line of code in this way...


C#
saveFileDialog1.Title = "Save a CSV File";
//saveFileDialog1.ShowDialog();
DialogResult dr = saveFileDialog1.ShowDialog();

if (dr == DialogResult.OK)
{
    string strFilePath = saveFileDialog1.FileName;
    //save file using stream.
    CreateCSVFile(dt ,strFilePath);
    //StreamWriter sw = new StreamWriter(strFilePath, true);
}
 
Share this answer
 
Comments
Member 11518617 13-Mar-15 6:17am    
Thanks for your suggestion. It worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900