am se limit view image max 4 image or less
when i ipload 4 image work perfect
when i upload less then 4 getitng error
An error occurred during local report processing.
protected void btnprint_Click(object sender, EventArgs e)
{
try
{
// Retrieve session data
string name = Session["name"] as string;
string civilid = Session["civilid"] as string;
string fileid = Session["fileid"] as string;
// Validate session data
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(civilid))
{
ScriptManager.RegisterStartupScript(this, GetType(), "sweetAlert", "Swal.fire({ icon: 'error', title: 'خطأ', text: 'يرجى إدخال البيانات المطلوبة.' });", true);
return;
}
// Retrieve values from TextBoxes and Dropdowns
string dateValue = TextBox17.Text.Trim();
string finalMessage = FinalMessageTextBox.Text.Trim();
string finalMessage2 = FinalMessageTextBox2.Text.Trim();
string selectedState = ddlState.SelectedItem?.Text;
string selectedCity = ddlCity.SelectedItem?.Text;
// Validate input fields
if (string.IsNullOrEmpty(dateValue) || string.IsNullOrEmpty(selectedState) || string.IsNullOrEmpty(selectedCity))
{
ScriptManager.RegisterStartupScript(this, GetType(), "sweetAlert", "Swal.fire({ icon: 'error', title: 'خطأ', text: 'يرجى ملء جميع الحقول المطلوبة.' });", true);
return;
}
// Get the URLs of the images uploaded
string[] imageUrls = HiddenImageUrlsLiteral.Text.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
// Dynamically adjust parameters based on how many images are uploaded
int imageCount = imageUrls.Length;
// Setup the report viewer and local report path
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/gsd/ReportENG2.rdlc");
ReportViewer1.LocalReport.EnableExternalImages = true;
// Prepare the DataTable to handle the images in the RDLC report
DataTable dtImages = new DataTable();
dtImages.Columns.Add("uploadedFiles", typeof(string)); // Ensure this matches the dataset field in RDLC
// Add each image URL to the DataTable
for (int i = 0; i < imageCount; i++)
{
DataRow row = dtImages.NewRow();
row["uploadedFiles"] = imageUrls[i]; // Pass the image URL
dtImages.Rows.Add(row);
}
// Fill the rest with empty strings if less than 4 images were uploaded
for (int i = imageCount; i < 4; i++)
{
DataRow row = dtImages.NewRow();
row["uploadedFiles"] = ""; // Add empty string for missing images
dtImages.Rows.Add(row);
}
// Add the image data source to the report
ReportDataSource imageDataSource = new ReportDataSource("DataSet1", dtImages);
ReportViewer1.LocalReport.DataSources.Clear(); // Clear any previous data sources
ReportViewer1.LocalReport.DataSources.Add(imageDataSource); // Add the new image data source
// Create the array for parameters (8 static + 4 images)
ReportParameter[] param = new ReportParameter[8 + 4];
// Assign static parameters for text fields and dropdowns
param[0] = new ReportParameter("locationParameter1", selectedState ?? string.Empty);
param[1] = new ReportParameter("locationParameter2", selectedCity ?? string.Empty);
param[2] = new ReportParameter("nameParameter", name ?? string.Empty);
param[3] = new ReportParameter("fileidParameter", fileid ?? string.Empty);
param[4] = new ReportParameter("dateParameter", dateValue ?? string.Empty);
param[5] = new ReportParameter("DropdownList", DropdownList.SelectedValue ?? string.Empty);
param[6] = new ReportParameter("msg1Parameter", string.IsNullOrEmpty(finalMessage) ? string.Empty : finalMessage);
param[7] = new ReportParameter("msg2Parameter", string.IsNullOrEmpty(finalMessage2) ? string.Empty : finalMessage2);
// Add image parameters; dynamically adjust based on how many images are available
for (int i = 0; i < imageCount; i++)
{
param[8 + i] = new ReportParameter($"uploadedFiles{i + 1}", imageUrls[i]);
}
// For missing images, pass empty strings
for (int i = imageCount; i < 4; i++)
{
param[8 + i] = new ReportParameter($"uploadedFiles{i + 1}", string.Empty);
}
// Set the parameters in the report viewer
ReportViewer1.LocalReport.SetParameters(param);
// Refresh the report viewer to apply the parameters and data source
ReportViewer1.LocalReport.Refresh();
// Export the report to a PDF
string mimeType, encoding, fileNameExtension;
string[] streams;
Warning[] warnings;
byte[] pdfBytes = ReportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
// Send the PDF file to the client for download
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", $"attachment; filename=Report-{DateTime.Now:dd-MM-yyyy}.pdf");
Response.BinaryWrite(pdfBytes);
Response.End();
}
catch (Exception ex)
{
// Log the error message for debugging purposes
System.Diagnostics.Debug.WriteLine($"Error: {ex.Message}");
// Show a SweetAlert error message to the user
ScriptManager.RegisterStartupScript(this, GetType(), "sweetAlert", $"Swal.fire({{ icon: 'error', title: 'خطأ', text: '{ex.Message}' }});", true);
}
}
on RDLC exp
=IIf(Parameters!uploadedFiles1.Value = "", Nothing, Parameters!uploadedFiles1.Value)