dyanmic crystal report

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pupilstuff
    New Member
    • Jun 2009
    • 18

    dyanmic crystal report

    hi guys i wan to make dyanmic crystal report according to values which i checked from check box
    thats all i did

    1. I made data set having data table name "Customer"
    2 i put four columm id,name
    ,age ,sex
    3. then i made crystal report and drag those columm in crystal report
    4 thn i drag crystal report viwer
    5 thn i write tht code at button click

    Code:
    protected void Button1_Click(object sender, EventArgs e)
        {
         
            ////CrystalReport1 objRpt;
            ////objRpt = new CrystalReport1();
            ReportDocument report = new ReportDocument();
            string reportPath = Server.MapPath("CrystalReport1.rpt");
    
            report.Load(reportPath); 
    
    
            string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=D:\\mayank/db2.mdb";
    
            //Get Select query String and add parameters to the 
            //Crystal report.
            string query = CreateSelectQueryAndParameters();
            
            //if there is no item select, then exit from the method.
            if (!query.Contains("Column"))
            {
                Response.Write("No selection to display!");
                return;
            }
    
            try
            {
                OleDbConnection Conn = new OleDbConnection(connString);
    
                OleDbDataAdapter adepter =
                new OleDbDataAdapter(query, connString);
                DataSet1 Ds = new DataSet1();
    
                adepter.Fill(Ds, "Customer");
                Response.Write(Ds);
                report.SetDataSource(Ds);
                crystalReportViewer1.ReportSource = report;
    
            }
            catch (OleDbException oleEx)
            {
              //  MessageBox.Show(oleEx.Message);
            }
            catch (Exception Ex)
            {
                //MessageBox.Show(Ex.Message);
            }
    
        }
        private string CreateSelectQueryAndParameters()
        {
            ReportDocument reportDocument;
            ParameterFields paramFields;
    
            ParameterField paramField;
            ParameterDiscreteValue paramDiscreteValue;
    
            reportDocument = new ReportDocument();
            paramFields = new ParameterFields();
    
            string query = "SELECT ";
            int columnNo = 0;
    
            if (CheckBox1.Checked)
            {
                columnNo++;
                query = query.Insert(query.Length, "Code as Column" +
                columnNo.ToString());
    
                paramField = new ParameterField();
                paramField.Name = "col" + columnNo.ToString();
                paramDiscreteValue = new ParameterDiscreteValue();
                paramDiscreteValue.Value = "Customer Code";
                paramField.CurrentValues.Add(paramDiscreteValue);
                //Add the paramField to paramFields
                paramFields.Add(paramField);
            }
            if (CheckBox2.Checked)
            {
                columnNo++;
                if (query.Contains("Column"))
                {
                    query = query.Insert(query.Length, ", ");
                }
                query = query.Insert(query.Length, "FirstName as Column" +
                columnNo.ToString());
    
                paramField = new ParameterField();
                paramField.Name = "col" + columnNo.ToString();
                paramDiscreteValue = new ParameterDiscreteValue();
                paramDiscreteValue.Value = "First Name";
                paramField.CurrentValues.Add(paramDiscreteValue);
                //Add the paramField to paramFields
                paramFields.Add(paramField);
            }
            if (CheckBox3.Checked)
            {
                columnNo++; //To determine Column number
                if (query.Contains("Column"))
                {
                    query = query.Insert(query.Length, ", ");
                }
                query = query.Insert(query.Length, "LastName as Column" +
                columnNo.ToString());
    
                paramField = new ParameterField();
                paramField.Name = "col" + columnNo.ToString();
                paramDiscreteValue = new ParameterDiscreteValue();
                paramDiscreteValue.Value = "Last Name";
                paramField.CurrentValues.Add(paramDiscreteValue);
                //Add the paramField to paramFields
                paramFields.Add(paramField);
            }
            if (CheckBox4.Checked)
            {
                columnNo++;
                if (query.Contains("Column"))
                {
                    query = query.Insert(query.Length, ", ");
                }
                query = query.Insert(query.Length, "Address as Column" +
                columnNo.ToString());
    
                paramField = new ParameterField();
                paramField.Name = "col" + columnNo.ToString();
                paramDiscreteValue = new ParameterDiscreteValue();
                paramDiscreteValue.Value = "Address";
                paramField.CurrentValues.Add(paramDiscreteValue);
                //Add the paramField to paramFields
                paramFields.Add(paramField);
            }
           
    
            //if there is any remaining parameter, assign empty value for that 
            //parameter.
            for (int i = columnNo; i < 5; i++)
            {
                columnNo++;
                paramField = new ParameterField();
                paramField.Name = "col" + columnNo.ToString();
                paramDiscreteValue = new ParameterDiscreteValue();
                paramDiscreteValue.Value = "";
                paramField.CurrentValues.Add(paramDiscreteValue);
                //Add the paramField to paramFields
                paramFields.Add(paramField);
            }
    
            crystalReportViewer1.ParameterFieldInfo = paramFields;
    
            query += " FROM Customer";
          
            return query;
        }
    it showing no error but crstal report is not generating

    plz tel me the solution its very urgent
    Last edited by tlhintoq; Jun 16 '09, 02:33 PM. Reason: [CODE] ... your code here ... [/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Have you done the basic debugging?
    Put a breakpoint at line 6 and walk through the code one line at a time to see
    • where it is not behaving as you expect.
    • Confirm that *every* value is valid along the way (paths, assumptions etc.)
    • Are you sure you have permission to write to the destination servers/volumes?
    • Maybe you're getting exceptions and not seeing them since your messageboxes are commented out.
    • Confirmed that each query going out is right, and each response back is right and has content?

    Comment

    Working...