简体   繁体   中英

Passing array list parameter into Crystal Report using loop in C#

public List<string>  _DELIVERYNO;
private void get_INVOICE_DATA_RPT()
{ 
  _DELIVERYNO = new List<string>();
   foreach (DataGridViewRow row in grdDNList.Rows)
   {
    DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;
    if (Convert.ToBoolean(chk.Value) == true)
       if (row.Cells.Count >= 2 && row.Cells[2].Value != null) 
          {                          
             _DELIVERYNO.Add(row.Cells[2].Value.ToString());
          }
     }
    foreach (var value in list)
     {
        get_INVOICE_DATA(value);                   
     }               
 }

Get the Delivery No from DataGridView and store to List.

The parameters are passed from frm1 to crystal report is OK

private void frmPrinINVOICE_02_Load(object sender, EventArgs e)
 {         
   var rpt = new ReportDocument();
   rpt.Load("rptSalesInvoice_02.rpt");
   rpt.SetDatabaseLogon(userName2, passWord2, @serverName2, databaseName2);
   rpt.Refresh();
   crystalReportViewer1.RefreshReport();
   foreach (var value in frm1._DELIVERYNO)
   {
      rpt.SetParameterValue("@DELIVERYNO", value);
   }               
   crystalReportViewer1.ReportSource = rpt;       
}

But the report source only keep the last data from the last query return by the last DELIVERYNO Example: DELIVERY No: 6425,6758,6927 were passed from the frm1 . But the report only display last query return by DELIVERY No:6927 .

So my question how to Loop the parameter but it must keep all the data return by all passed parameters (DELIVERY No: 6425,6758,6927)

And here is the SQL store procedure:

PROCEDURE [dbo].[proc_IVHead_Get_INVOICE_DATA]
    @DELIVERYNO nvarchar(50)
AS
    select *
        from [ENVNDIVDB].[dbo].[IVHead] a
        inner join [ENVNDIVDB].[dbo].[IVRecords] b
        on a.DELIVERYNO=b.DELIVERYNO        
     WHERE B.[DELIVERYNO] =@DELIVERYNO
     ORDER BY a.DELIVERYNO 

Can we store loop result into temp table then get data out from them?

Passed Comma Separated Values of DELIVERY NO from frm1 and Add below in SQL Server function that split Separated Values

CREATE FUNCTION dbo.Split(@String nvarchar(4000), @Delimiter char(1))
RETURNS @Results TABLE (colA nvarchar(4000))
AS
BEGIN
DECLARE @INDEX INT
DECLARE @SLICE nvarchar(4000)

SELECT @INDEX = 1
WHILE @INDEX !=0
BEGIN

SELECT @INDEX = CHARINDEX(@Delimiter,@STRING)
IF @INDEX !=0

SELECT @SLICE = LEFT(@STRING,@INDEX - 1)
ELSE
SELECT @SLICE = @STRING

INSERT INTO @Results(colA) VALUES(@SLICE)

SELECT @STRING = RIGHT(@STRING,LEN(@STRING) - @INDEX)

IF LEN(@STRING) = 0 BREAK
END
RETURN
END

Last Alter your Stored Porcedure

PROCEDURE [dbo].[proc_IVHead_Get_INVOICE_DATA]
    @DELIVERYNO nvarchar(50)
AS
    select *
        from [ENVNDIVDB].[dbo].[IVHead] a
        inner join [ENVNDIVDB].[dbo].[IVRecords] b
        on a.DELIVERYNO=b.DELIVERYNO        
        inner join DBO.Split(@DELIVERYNO,',') c 
        on B.[DELIVERYNO] = c.[colA] 
     ORDER BY a.DELIVERYNO 

Your loop is replacing the parameter value with every iteration and leaving with the last value. Please use the following code to achieve the correct results:

Your loop:

foreach (var value in frm1._DELIVERYNO)
{
   rpt.SetParameterValue("@DELIVERYNO", value);
}       

Replace it with:

var value = string.Join(",", frm1._DELIVERYNO);
rpt.SetParameterValue("@DELIVERYNO", value);

It will set the parameter with value: 6425,6758,6927

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM