Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public void Browse_Click(object sender, EventArgs e)
        {
            DialogResult RESULT = this.OFP.ShowDialog();
            if (RESULT == DialogResult.OK)
            {
                string filename = OFP.FileName;
                Locations.Text = filename;
            }
            else
            {
                MessageBox.Show("Dear user Please select Path");
            }
        }
        //**************************************************************        public static DataSet GetDataset(string filename)
        {
            string Connectionstring = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;",
            Path.GetDirectoryName(filename));
            string cmdstring = string.Format(@"SELECT *FROM {0}", Path.GetFileName(filename));
            DataSet dataset = new DataSet();

            using (OleDbConnection olconn = new OleDbConnection(Connectionstring))
            {
                olconn.Open();
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                adapter.SelectCommand = new OleDbCommand(cmdstring, olconn);
              adapter.Fill(dataset, "Test");
                olconn.Close();
            }
          return dataset;
        }
public void Loaded_Click(object sender, EventArgs e)
       {


           try
           {
               DataSet dataset = Form1.GetDataset(Locations.Text);
               Datagridveiw1.DataSource = dataset.Tables[0].DefaultView;
           }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
//********************************************************************
public void save_Click(object sender, EventArgs e)
       {
             DataSet dst = Form1.GetDataset( );
            try
            {
                StringBuilder sbCSV = new StringBuilder();
                 int intColCount = dst.Tables[0].Columns.Count;    
                foreach (DataRowView dr in dst.Tables[0].DefaultView)   
                {                  
                    for (int x = 0; x < intColCount; x++)            
                    {                    
                        sbCSV.Append(dr[x].ToString());      
                 if ((x + 1) != intColCount)                  
                 {                           
                     sbCSV.Append(",");           
                 }                   
                    }                 
                    sbCSV.Append("\n");  
                }           
               using (StreamWriter sw = new StreamWriter("c:\\data\\Log1.csv"))      
                {                
                    sw.Write(sbCSV.ToString());     
                }     
            }

           catch (System.FormatException ex)
           {
               MessageBox.Show(ex.Message);
           }
       }



Hi developer !
I am vishal sharma

my problem is that I'm just confused because I know that I have written a bad code.
I want to declare a dataset Globaly that can be used in any method
My Browse and load code is working good but my save code does not work efficiently.
1. Dataset Object can not be call in the save_click event
2. Invalid string pass to save datagridveiw values .
3. how use save dialog box in this code
4. and after save file in csv format wrap text show in the all file
5. how use trim method in stringbulider code to remove whitespace from save file.
Please solve my problem by correcting and modifying this code
I m thankfull to you
Posted
Updated 15-Nov-10 20:04pm
v2

1 solution

1. as i can see in the comment GetDataSet takes a string arg which is not present when u r calling it on click event.
2. same as 1
3.Try to google it it's not big deal

4. dude u can not control wrap try and open it in xcl it might help

5.why don't you remove the whitespace before saving the file somthing like sbCSV.Append(dr[x].toString().Trim()).

Hope this helps
 
Share this answer
 

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