Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to export bulk data from database,my code is working.but data saved in server system only .i need to save bulkdata in local system also,pls help any one
C#
public void SaveWorkbook(){

    String folderPath = C:\\My Files\\+ this.folder ;
My path attached here


My code attached here.
C#
My path(string reportFileName = @"C:\Bondreg\" + l + ".pdf";) while exporting time data saving to server system only . If possible to save data in local system .

protected void Button2_Click(object sender, EventArgs e)
{
List l = new List();
sc.Open();
SqlCommand cmd = new SqlCommand("select Fliono from BOND_REG where status='A'", sc);
SqlDataReader SDR = cmd.ExecuteReader();


while (SDR.Read() == true)
{
var item1 = SDR.GetValue(0).ToString();
l.Add(item1);
}
SDR.Close();
sc.Close();

for (int i = 0; i < l.Count; i++)
{
fileexport(l[i].ToString());
updatestaus(l[i].ToString());

}

SDR.Close();
sc.Close();
Response.Write("<Script> alert('Data Exported Succesfully')</Script>");
DropDownList1.SelectedIndex = 0;

}
public void updatestaus(string s)
{
sc.Open();
SqlCommand cmd = new SqlCommand("update BOND_REG set status='P' where fliono ='" + s.ToString() + "'", sc);
cmd.ExecuteNonQuery();
sc.Close();
}
public void fileexport(string l)
{

SqlCommand cmd1 = new SqlCommand("select * from BOND_REG where status='A' and Fliono ='" + l + "'", sc);

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd1;
DataTable datatable = new DataTable();
da.Fill(datatable);
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/bond.rpt"));
crystalReport.SetDataSource(datatable);
ExportOptions rptExportOption;
DiskFileDestinationOptions rptFileDestOption = new DiskFileDestinationOptions();
PdfRtfWordFormatOptions rptFormatOption = new PdfRtfWordFormatOptions();
string reportFileName = @"C:\Bondreg\" + l + ".pdf";
rptFileDestOption.DiskFileName = reportFileName;
rptExportOption = crystalReport.ExportOptions;
{
rptExportOption.ExportDestinationType = ExportDestinationType.DiskFile;
rptExportOption.ExportFormatType = ExportFormatType.PortableDocFormat;
rptExportOption.ExportDestinationOptions = rptFileDestOption;
rptExportOption.ExportFormatOptions = rptFormatOption;
}

crystalReport.Export();
crystalReport.Close();
crystalReport.Dispose();
Posted
Updated 9-Jun-15 22:55pm
v2
Comments
The Doer 9-Jun-15 10:08am    
What type of data are you pulling from database?
Unni R 9-Jun-15 12:04pm    
like registration form..in this form some date and text fields
njammy 10-Jun-15 5:00am    
Well it won't save the file to Client PC because the web application is running on the server and the export code is executing on that end.

You need to push the file content to the client browser using the HttpResponse object.

In the link I provided previously, have you tried the HttpResponse (add header, flush) approach? Only that needs adding to your code at the end, set the content type to pdf and see if that works.

This example is more suited to PDF file saving.
http://stackoverflow.com/questions/5586772/asp-net-sending-a-pdf-to-the-user

1 solution

AsI'm making a big assumption here that you are performing a SELECT database command and wanting to save a file containing the data.

Try this example:
http://www.aspsnippets.com/Articles/Export-data-from-SQL-Server-to-CSV-file-in-ASPNet-using-C-and-VBNet.aspx[^]

If this example does not suit your problem, please describe more detail specific to your scenario.

Also try this
This example is more suited to PDF file saving.
http://stackoverflow.com/questions/5586772/asp-net-sending-a-pdf-to-the-use
 
Share this answer
 
v2
Comments
Unni R 10-Jun-15 1:48am    
My code attached here . My path(string reportFileName = @"C:\Bondreg\" + l + ".pdf";) while exporting time data saving to server system only . If possible to save data in local system .

protected void Button2_Click(object sender, EventArgs e)
{
List<string> l = new List<string>();
sc.Open();
SqlCommand cmd = new SqlCommand("select Fliono from BOND_REG where status='A'", sc);
SqlDataReader SDR = cmd.ExecuteReader();


while (SDR.Read() == true)
{
var item1 = SDR.GetValue(0).ToString();
l.Add(item1);
}
SDR.Close();
sc.Close();

for (int i = 0; i < l.Count; i++)
{
fileexport(l[i].ToString());
updatestaus(l[i].ToString());

}

SDR.Close();
sc.Close();
Response.Write("<Script> alert('Data Exported Succesfully')</Script>");
DropDownList1.SelectedIndex = 0;

}
public void updatestaus(string s)
{
sc.Open();
SqlCommand cmd = new SqlCommand("update BOND_REG set status='P' where fliono ='" + s.ToString() + "'", sc);
cmd.ExecuteNonQuery();
sc.Close();
}
public void fileexport(string l)
{

SqlCommand cmd1 = new SqlCommand("select * from BOND_REG where status='A' and Fliono ='" + l + "'", sc);

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd1;
DataTable datatable = new DataTable();
da.Fill(datatable);
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/bond.rpt"));
crystalReport.SetDataSource(datatable);
ExportOptions rptExportOption;
DiskFileDestinationOptions rptFileDestOption = new DiskFileDestinationOptions();
PdfRtfWordFormatOptions rptFormatOption = new PdfRtfWordFormatOptions();
string reportFileName = @"C:\Bondreg\" + l + ".pdf";
rptFileDestOption.DiskFileName = reportFileName;
rptExportOption = crystalReport.ExportOptions;
{
rptExportOption.ExportDestinationType = ExportDestinationType.DiskFile;
rptExportOption.ExportFormatType = ExportFormatType.PortableDocFormat;
rptExportOption.ExportDestinationOptions = rptFileDestOption;
rptExportOption.ExportFormatOptions = rptFormatOption;
}

crystalReport.Export();
crystalReport.Close();
crystalReport.Dispose();

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