Saturday, September 14, 2013

How to show a Crystal Report in a CrystalReportViewer


1- Be sure to Download Crystal Reports 2010 for Visual Studio

2- Open an existing project or make a new one, build a data set to add the table(s) you want in it, by clicking right click on the project name, Add , New Item..., click on Data, and choose DataSet, then name it as you like and click on the Add button 


3-  In the Data set, right click, choose Add, then choose DataTable



4- Change the name of the Data table if you want and right click in it and add column(s) , and name them, for me i named it Employee and added 4 columns in it as shown : 




5- right click on your project name at the solution and choose Add, then choose New Item... 



6- After the List show's up, choose Reporting and then choose Crystal Reports, and name it as you like and then press on the Add button 



7- The Crystal Report Gallery will show up, i will choose from Using the Report Wizard : Standard and click on OK 




8- know lets add the table to the report, click the + beside Project Data, ADO .NET DataSets, then you will see your project name and your data set name , in my case it's WindowsFormsApplication1.DataSet1, inside you will see the name of the data table(s) defined in the data set, add the one(s) you want to use, in my case ill add Employee and then click on Finish : 




// Note : If you clicked next you will be able to determine specific Fields to Display, and another next you will be able to determine the Report Style  


9- Drag the Sections to make a good spaces to add things you want, and add a group by doing right click, Insert, Group... : 




10- Then choose the column name that the records will be sorted and Grouped by, in my case i will choose EmployeeID, and then click OK button : 




11- From the Toolbox, search for Crystal Reports, I want to add a Title so i will drag a Text Object and put it at the Report Header, you can add inside it by clicked right click, Edit Text Object, you can change also the Font Size, make it Bold, Center, etc...  : 




12- From the Toolbox, Crystal Reports, There is Line Object, i will add lines to the report horizontally and vertically to make the design of the table where i will add the data, put the lines of headers on the Group header, and the place where the data will appear in the Details, to make the lines in the header suit with the lines in the Detail you can click on the line on the header and see his properties and take the Right or Left number for each vertical line and copy it to the line that match it in the Detail, but for the vertical line take both Right and left numbers, you can change also the Top and Bottom so you can have a neat report : 






13-  Press right click on the Details and choose Fit Section, also do that to the Group Header  :



14- know put 4 Text Objects from the Toolbox inside the Headers , and put inside them the names you want : 



15- From the Field Explorer, Database Fields, you will find your table(s), for my case the table is Employee, and there is 4 columns shown in it : 



16- Drag and Drop columns you want in Details each in the place that you want the record(s) to show in : 




17- Go to the Form that you want to show the report in, from Toolbox, Reporting, Drag and Drop a CrystalReportViewer and make it in the size you want and change the properties for it if you like, From Common Controls, Drag and Drop 3 labels, and 3 Textboxes, and 3 buttons, change the Text of the Labels and buttons : 




18- From Data Connections, if you don't have a connection add one, right click, Add connection..., put the name of the Data base file, i will name it Project, press OK, then Yes : 




19 - The name of your database will show under the Data Connections, inside it you will find Tables, right click, Add New Table, put the Column Names and the Data Types for them, i will make the EmployeeID Column an Identity from Column Properties, Identity Specifications, (Is Identity) - Yes, my table shape is : 



// Press on the save button and put the Table name

20- From the Form, i made a button to add, so i will double click on the Add button, the function for this button will be taking the Employee Name, and Employee Phone, and Employee Age from the Text boxes, and add them to the table in the database, The Name and Phone and Age will be required, and i changed the textboxes and the buttons names, the code is : 

private void button1_Click(object sender, EventArgs e)
        {
            if (txtAge.Text != string.Empty && txtName.Text != string.Empty && txtPhone.Text != string.Empty )
            {
                txtAge.ForeColor = Color.Black;
                txtName.ForeColor = Color.Black;
                txtPhone.ForeColor = Color.Black;
                SqlConnection Con = new SqlConnection();
                Con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Saleem\Documents\Project.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                Con.Open();
                String query = "Insert into Employee_T(EmployeeName , EmployeePhone, EmployeeAge)Values('" + txtName.Text + "','" + txtPhone.Text + "','" + txtAge.Text + "')";
                SqlCommand com = new SqlCommand(query, Con);
                com.ExecuteNonQuery();
                Con.Close();
            }
            else if (txtAge.Text == string.Empty && txtName.Text == string.Empty && txtPhone.Text == string.Empty )
            {
                txtAge.ForeColor = Color.Red;
                txtAge.Text = "please Enter the Employee age";
                txtName.ForeColor = Color.Red;
                txtName.Text = "please Enter the Employee name" ;
                txtPhone.ForeColor = Color.Red;
                txtPhone.Text = "please Enter the Employee Phone";
            }
            else if (txtAge.Text == string.Empty && txtName.Text == string.Empty)
            {
                txtAge.ForeColor = Color.Red;
                txtAge.Text = "please Enter the Employee age";
                txtName.ForeColor = Color.Red;
                txtName.Text = "please Enter the Employee name" ;
            }
            else if (txtName.Text == string.Empty && txtPhone.Text == string.Empty)
            {
                txtName.ForeColor = Color.Red;
                txtName.Text = "please Enter the Employee name" ;
                txtPhone.ForeColor = Color.Red;
                txtPhone.Text = "please Enter the Employee Phone";
            }
            else if (txtAge.Text == string.Empty && txtPhone.Text == string.Empty )
            {
                txtAge.ForeColor = Color.Red;
                txtAge.Text = "please Enter the Employee age";
                txtPhone.ForeColor = Color.Red;
                txtPhone.Text = "please Enter the Employee Phone";
            }
            else if (txtAge.Text == string.Empty)
            {
                txtAge.ForeColor = Color.Red;
                txtAge.Text = "please Enter the Employee age";
            }
            else if (txtPhone.Text == string.Empty)
            {
                txtPhone.ForeColor = Color.Red;
                txtPhone.Text = "please Enter the Employee Phone";
            }
             else if (txtName.Text == string.Empty)
            {
                txtName.ForeColor = Color.Red;
                txtName.Text = "please Enter the Employee name" ;
            }
        }

21- The Clear button code : 

 private void btnClear_Click(object sender, EventArgs e)
        {
            txtAge.Text = string.Empty;
            txtName.Text = string.Empty;
            txtPhone.Text = string.Empty;

        }

22- The Show Report button function is to show me the Crystal Report in the CrystalReportViewer with all the data from the columns that is selected : 

 private void btnShow_Click(object sender, EventArgs e)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Saleem\Documents\Project.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            Conn.Open();
            String queryy = "SELECT EmployeeID, EmployeeName, EmployeePhone, EmployeeAge From Employee_T";
            SqlCommand comm = new SqlCommand(queryy, Conn);
            SqlDataAdapter Adapter = new SqlDataAdapter(comm);
            DataSet1 ds = new DataSet1();
            Adapter.Fill(ds, "Employee_T");
            Conn.Close();
            CrystalReport1 crs = new CrystalReport1();
            crs.SetDataSource(ds.Tables[1]);
            crystalReportViewer1.ReportSource = crs;
        }



know you will be able to see the data in the CrystalReportViewer after clicking the Show Report button : 



// The data in the report is only for testing

No comments :

Post a Comment