Click here to Skip to main content
15,896,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to export multiple files in PDF format through ASP.NET at a time. File format is already updated in Crystal report. My code attached here. this is use to only one PDFfile save at a time.
C#
public void updatestaus(string F)
    {
        sc.Close();
        sc.Open();
        SqlCommand cmd = new SqlCommand("update BOND_REG set status='P' where fliono='"+F+"'", sc);
        cmd.ExecuteNonQuery();
        sc.Close();


    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        sc.Open();
        SqlCommand cmd = new SqlCommand("select Fliono from BOND_REG where status='A'", sc);
        SqlDataReader SDR = cmd.ExecuteReader();
        while (SDR.Read() == true)
        {
            string St = SDR.GetValue(0).ToString();
            //SDR.Close();
          // SDR.Dispose();
            fileexport(St);
            updatestaus(St);

        }
        SDR.Close();
        sc.Close();

    }

    public void fileexport(string st1)
    {
       // SDR.Close(); 
        SqlCommand cmd1 = new SqlCommand("select * from BOND_REG where status='A' and Fliono='" + st1 + "'", 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:\pdfd\ st1.pdf";
        rptFileDestOption.DiskFileName = reportFileName;
        rptExportOption = crystalReport.ExportOptions;
        {
            rptExportOption.ExportDestinationType = ExportDestinationType.DiskFile;
                       rptExportOption.ExportFormatType = ExportFormatType.PortableDocFormat;
            rptExportOption.ExportDestinationOptions = rptFileDestOption;
            rptExportOption.ExportFormatOptions = rptFormatOption;
        }

        crystalReport.Export();
    }
Posted
Updated 31-May-15 18:28pm
v2

1 solution

I think the problem is in each record your application overwrite the same pdf file. try to build unique file name like below

C#
string reportFileName =string.Format(@"C:\pdfd\st1_{0}.pdf",st1) ;


or append timespan to file name.[^]

You can avoid connection related issues by introducing methods to do the database operations and properly handling database connections in those methods. for example,

C#
public List<string> GetFlionoList()
{
    List<string>  flionoList = new List<string>();
    using(SqlConnection connection = new SqlConnection(yourConnectionString) )
	using(SqlCommand cmd = new SqlCommand("select Fliono from BOND_REG where status='A'", connection))
	{
		connection.Open();
		using(SqlDataReader sdr= cmd.ExecuteReader())
		{
			while (sdr.Read())
			{
			  flionoList.Add(sdr.Getstring(0));
			}
		}
	}
	return flionoList;
}

public void UpdateStaus(string fliono)
{
   using(SqlConnection connection = new SqlConnection(yourConnectionString) )
   using(SqlCommand cmd = new SqlCommand("update BOND_REG set status='P' where fliono='"+fliono+"'", connection))
   {
   		connection.Open();
   		cmd.ExecuteNonQuery();
   }
}


Now you can call GetFlionoList() and get the list and iterate each item of the list and call methods like UpdateStaus for each item. make sure that you have dispose the connection and readers using "using blocks"
 
Share this answer
 
v4
Comments
Unni R 1-Jun-15 1:07am    
Showing one error message "There is already an open DataReader associated with this Command which must be closed first"

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