Click here to Skip to main content
12,621,477 members (30,491 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP ASP.NET ADO.NET C#4.0
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

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 27-Nov-12 1:02am
Updated 27-Nov-12 1:06am
v2
Comments
Karthik Harve 27-Nov-12 6:06am
   
[Edit] pre tags added.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You aren't using the parameter:
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:
query = "select * from Transactions where id=@id";
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", TextBox1.Text);
And it should solve your problem.
  Permalink  
Comments
ooffooooo 27-Nov-12 6:53am
   
i tried this still same error
Parameter is not valid.
ooffooooo 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,

Write your code as below.

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); 
        }
    }
  Permalink  
Comments
ooffooooo 27-Nov-12 6:49am
   
@Mohd.Mukhtar. still same error ....
Parameter is not valid
ooffooooo 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


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100