Medicine Sell Receipt in Crystal Reports using ParameterFields

Dear Friends, after searching long on the internet for manually showing static data in Crystal Reports at runtime, I have written the following program. Though I am new to Crystal Reports, I have tried my best. Suggestions are welcome for improving the program.

So, let's start. We will create a Medicine Selling Receipt using C#.NET and Windows Forms. First create a windows Forms project. Then add a blank Crystal Report to the project. Now while Crystal Report open, go to field explorer and create a few parameter fields. I have created 17. I found to manage them programmatically easy in code if I named them ending with contiguous numbers. I named them Param1, Param2 ....., Pr1, Pr2..., and so on.

The Data type of all Parameter fields are string. Now format the blank Crystal Report to your liking. Add Pictures, textfileds and decorate the report. Now drag and drop each parameter field on to the Crystal Reports and place them strategically. See the accompanying program. Now, on the Form Dred and drop two textboxes, 2 buttons, and a CrystalReportViewer. Now write the code on Button click events. The whole program is given below.

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.ComponentModel;    
  4. using System.Data;    
  5. using System.Drawing;    
  6. using System.Linq;    
  7. using System.Text;    
  8. using System.Windows.Forms;    
  9. using CrystalDecisions.CrystalReports.Engine;    
  10. using CrystalDecisions.Shared;    
  11.     
  12. namespace CrystalReport_MedicineSellSlip    
  13. {    
  14.     public partial class Form1 : Form    
  15.     {    
  16.         int i = 0, k = 0, pr1 = 10; double qnt1 = 0; double[] qntarr = new double[4];    
  17.         CrystalReport1 crt1 = new CrystalReport1();    
  18.     
  19.     
  20.         public Form1()    
  21.         {    
  22.             InitializeComponent();    
  23.         }    
  24.     
  25.         private void button1_Click(object sender, EventArgs e)    
  26.         {    
  27.                 
  28.             if (i < 4) i++;    
  29.     
  30.             int j = i;    
  31.     
  32.             if (j <= 4)    
  33.             {    
  34.                 crt1.SetParameterValue("Param" + j.ToString(), textBox1.Text);    
  35.                 crt1.SetParameterValue("Pr" + j.ToString(), pr1.ToString());    
  36.                 crt1.SetParameterValue("Qn" + j.ToString(), textBox2.Text);    
  37.                 crt1.SetParameterValue("Amt" + j.ToString(), (pr1 * Convert.ToDouble(textBox2.Text)).ToString());    
  38.                     
  39.             }    
  40.     
  41.     
  42.     
  43.                
  44.             qnt1 +=(pr1 * Convert.ToDouble(textBox2.Text));    
  45.     
  46.             crt1.SetParameterValue("TTL1", qnt1.ToString());    
  47.     
  48.             qntarr[i - 1] = (pr1 * Convert.ToDouble(textBox2.Text)); ;    
  49.     
  50.             for (j = i + 1; j <= 4; j++)    
  51.             {    
  52.                 crt1.SetParameterValue("Param" + j.ToString(), "");    
  53.                 crt1.SetParameterValue("Pr" + j.ToString(), "");    
  54.                 crt1.SetParameterValue("Qn" + j.ToString(), "");    
  55.                 crt1.SetParameterValue("Amt" + j.ToString(), "");    
  56.             }    
  57.     
  58.             crystalReportViewer1.ReportSource = crt1;    
  59.             crystalReportViewer1.Refresh();    
  60.         }    
  61.     
  62.         private void button3_Click(object sender, EventArgs e)    
  63.         {    
  64.             // double qntmins;    
  65.     
  66.             qnt1 -= qntarr[i - 1];    
  67.     
  68.             if (i > 0)    
  69.             {    
  70.                 crt1.SetParameterValue("Param" + i.ToString(), "");    
  71.                 crt1.SetParameterValue("Pr" + i.ToString(), "");    
  72.                 crt1.SetParameterValue("Qn" + i.ToString(), "");    
  73.                 crt1.SetParameterValue("Amt" + i.ToString(), "");    
  74.                 crt1.SetParameterValue("TTL1", qnt1.ToString());    
  75.             }    
  76.     
  77.             crystalReportViewer1.ReportSource = crt1;    
  78.             crystalReportViewer1.Refresh();    
  79.             if (i > 0) i--;    
  80.     
  81.     
  82.         }    
  83.     
  84.             
  85.     }    
  86. }   
This was my short article. Hope it helps.

Madhukar Krishna 
Ebook Download
View all
Learn
View all