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");
}
}
{
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