Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.40/5 (4 votes)
See more:
I have a crystal report in my program that displays all staffs and their salary.
I want the report to display only the salary of a particular staff
so I created a form (frmMysalary) which only takes the ID of a particular staff.
How do I code and link this form to the crystal report so that the report is generated base on the ID entered into frmMysalary

please help me out

OP's additional information moved from non-solution below
created the report then I created a new Integer parameter in the Parameter Fields of Field Explorer (Crystal Reports).
I then created the selection formula for the Crystal Reports by Right clicking on Crystal Reports designer window , select REPORT -> SELECTION FORMULA -> RECORD .

first i selected the table field that is studentid from Student table and then I selected the comparison operator (=) and finally i selected the Parameter i created earlier.
The code

C#
 private void btnShowreport_Click(object sender, EventArgs e)
 {
 ReportDocument cryRpt = new ReportDocument();
 cryRpt.Load("F:\\STUDENT\\STUDENT\\CrystalReport2.rpt");
  
 
ParameterFieldDefinitions crParameterFieldDefinitions;
 ParameterFieldDefinition crParameterFieldDefinition;
 ParameterValues crParameterValues = new ParameterValues();
 ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
  
 //crParameterDiscreteValue.Value = Convert.ToInt32(txtStudentrid.Text);
 crParameterDiscreteValue.Value = txtStudentrid.Text;
 crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
 crParameterFieldDefinition = crParameterFieldDefinitions["studentid"];
 crParameterValues = crParameterFieldDefinition.CurrentValues;
  
 crParameterValues.Clear();
 crParameterValues.Add(crParameterDiscreteValue);
 crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
  
 crystalReportViewer2.ReportSource = cryRpt;
 crystalReportViewer2.Refresh();
 }
Posted
Updated 12-Nov-12 5:55am
v2
Comments
R. Giskard Reventlov 7-Nov-12 11:51am    
What have you already tried for yourself? How did that work out?
bbirajdar 7-Nov-12 12:22pm    
Start with writing the sql query that fetches the required details from the database from the ID. When you are done , let us know. We will tell you the next step

1 solution

Form frmMysalary
C#
private void print_Click(object sender, EventArgs e)
        {
           datatable tbl= new datatable();
           SqlDataAdapter adp = new SqlDataAdapter("select * from staffdetails where staffID='"+textbox1.text+"'",conn);
           adp.fill(tbl);
           if(dt>0)
           {
                Reports.StaffReport sr = new Reports.StaffReport ();
                sr.dt = tbl;
                sr.Show();
           }
           else
           {
                   MessageBox.Show("No Records found");
           }
         }


Form StaffReport
C#
public DataTable dt = new DataTable();
        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {
            Reports.StaffReport crt = new Reports.StaffReport();
            crt.Database.Tables[0].SetDataSource(dt);
            crystalReportViewer1.ReportSource = crt;
        }
 
Share this answer
 

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