Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using this code for printing Crystal report with Button Click event its giving me this error while printing

Parameter is not valid.

this my code

C#
protected void Button3_Click(object sender, EventArgs e)
{
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();
    DataTable dt1 = new DataTable();

    string query;
   
        con.Open();
        query = "select * from Transactions where id='" + TextBox1.Text + "'";
        cmd = new SqlCommand(query, con);
        cmd.Parameters.Add("@id", SqlDbType.Decimal).Value = TextBox1.Text;
        da = new SqlDataAdapter(cmd);
        dt.Clear();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
           
            ReportDocument Report = new ReportDocument();
            Report.Load(Server.MapPath("~/CrystalReport.rpt"));
            Report.SetDataSource(dt);
            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables;
            Report.SetDatabaseLogon("sa", "123456789", @"server", "report");
            CrystalReportViewer1.ReportSource = Report;
            CrystalReportViewer1.ReportSource = Report;
            CrystalReportViewer1.DataBind();
            CrTables = Report.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtableLogoninfo = CrTable.LogOnInfo;
                crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtableLogoninfo);
            }

            Report.Refresh();
            Report.PrintToPrinter(1, true, 1, 1); // ERROR : Parameter is not valid 
        }
    }
Posted
Updated 27-Nov-12 0:06am
v2
Comments
Karthik Harve 27-Nov-12 6:06am    
[Edit] pre tags added.

You aren't using the parameter:
C#
query = "select * from Transactions where id='" + TextBox1.Text + "'";
cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@id", SqlDbType.Decimal).Value = TextBox1.Text;
In this case, the parameter is @id which you don't refer to and which desn't contain a Decimal value - it is text. Change it to:
C#
query = "select * from Transactions where id=@id";
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", TextBox1.Text);
And it should solve your problem.
 
Share this answer
 
Comments
Mohammed Abdul Muqeet 27-Nov-12 6:53am    
i tried this still same error
Parameter is not valid.
Mohammed Abdul Muqeet 27-Nov-12 6:55am    
the crystal report is Hidden i just want the print out of the crystal report ..
when i try visible crystal report its giving me this error

Microsoft JScript runtime error: 'Sys.Application' is null or not an object
Hi,

Write your code as below.

C#
protected void Button3_Click(object sender, EventArgs e)
{
    SqlCommand cmd = null
    SqlDataAdapter da = null;
    DataTable dt = new DataTable();
    DataTable dt1 = new DataTable();
 
    string query;
   
        con.Open();
        query = "select * from Transactions where id='" + TextBox1.Text + "'";
        cmd = new SqlCommand(query, con);        
        da = new SqlDataAdapter(cmd);
        dt.Clear();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
           
            ReportDocument Report = new ReportDocument();
            Report.Load(Server.MapPath("~/CrystalReport.rpt"));
            Report.SetDataSource(dt);
            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables;
            Report.SetDatabaseLogon("sa", "123456789", @"server", "report");
            CrystalReportViewer1.ReportSource = Report;
            CrystalReportViewer1.ReportSource = Report;
            CrystalReportViewer1.DataBind();
            CrTables = Report.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtableLogoninfo = CrTable.LogOnInfo;
                crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtableLogoninfo);
            }
 
            Report.Refresh();
            Report.PrintToPrinter(1, true, 0, 0); 
        }
    }
 
Share this answer
 
Comments
Mohammed Abdul Muqeet 27-Nov-12 6:49am    
@Mohd.Mukhtar. still same error ....
Parameter is not valid
Mohammed Abdul Muqeet 27-Nov-12 6:55am    
the crystal report is Hidden i just want the print out of the crystal report ..
when i try visible crystal report its giving me this error

Microsoft JScript runtime error: 'Sys.Application' is null or not an object
Mohd. Mukhtar 28-Nov-12 0:28am    
Try after updating below line.

Report.PrintToPrinter(1, false, 0, 0);
Mohammed Abdul Muqeet 2-Dec-12 6:20am    
The project is deployed when i try from client system its printing on server but when i debug it its giving error Parameter Is Not Valid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900