Thursday 27 February 2014

Crystal Report With DataSet in Web Application .aspx Page(With Database)


1. Open Project solution

2. Create a Table and Insert Data's into a Table.

3. Example



4. Coding added to place of Web.config

<appSettings>
    <add key="ConnString" value="Data Source=SAMITECHPC1\SQL2008R2;Initial Catalog=test;Integrated Security=True" />
    <add key="CrystalImageCleaner-AutoStart" value="true" />
    <add key="CrystalImageCleaner-Sleep" value="60000" />
    <add key="CrystalImageCleaner-Age" value="120000" />
  </appSettings>

<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=SAMITECHPC1\SQL2008R2;Initial Catalog=test;Integrated Security=True"/>
</connectionStrings>

5. Example:




6.Solution Name To Right Click --> Add New Item --> Select DataSet and Add Button is Clicked



7. Now Open DataSet1.xsd page and Right Click --> Add --> DataTable



and now open DataTable again Add a column in DataTable



and Click a Properties to change a Datatype( same as Database column name and datatype) and click a
Save to DataSet1.xsd


8. Now Add a Crystal Report into Solution.



9. Now You Add a New Class into Solution



Coding:

public class Class1
{
    //Connection Strings
    string strConnection = System.Configuration.ConfigurationSettings.AppSettings["ConnString"];

    //DataSet1 is .xsd file name
    public DataSet1 GetValues()
{
        try
        {
            DataSet1 alldatas = null;
            using (SqlConnection connection = new SqlConnection(strConnection))
            {
                using (SqlCommand command = connection.CreateCommand())
                {
                    alldatas = new DataSet1();
                    // best practies ingore inline query
                    command.CommandText = "SELECT * FROM empdetails;";
                    command.CommandType = System.Data.CommandType.Text;
                    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                    {
                        //DataTable1 is Datatable name of DataSet1
                        adapter.Fill(alldatas.DataTable1);
                    }
                }
            }
            return alldatas;
        }
        catch (Exception ex)
        {

            throw ex;
        }
}
}

10. Now You add a New Form into Solution



11. Now You Add a Crystal Report Viewer From Tool Box



12. To Open a PrintScreen.cs File and type a Following Coding

In Page Load Event

       //Class1 is a file name of .cs
        Class1 ps = new Class1();

        ReportDocument report = new ReportDocument();
        report.Load(Server.MapPath("CrystalReport.rpt"));

        //DataSet is a .xsd File Name
        DataSet1 Result = ps.GetAll();
        if (Result != null)
        {
            report.SetDataSource(Result);
            CrystalReportViewer1.ReportSource = report;
        }

13. Now to Open a Crystal Report

   Click Field Explorer of DataBase Fields --> right click --> DataBase Expert clicked



14. DataBase Expert Window is Opened and Now Click Project Data --> ADO.NET DataSets -->  Select DataTable1 to Moved To another Box.



15. Now Click Field Explorer of Database Fields all DataTable1 Fileds are here.


16. And open a Page Print Click Event



17. To click Print to Type a Coding

    protected void butPrint_Click(object sender, EventArgs e)
    {
        Response.Redirect("PrintScreen.aspx");
    }


18.Output:


No comments:

Post a Comment