Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 0:02am
Edited 27-Nov-12 0:06am
v2
Comments
Karthik Harve at 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 at 27-Nov-12 6:53am
   
i tried this still same error
Parameter is not valid.
ooffooooo at 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 at 27-Nov-12 6:49am
   
@Mohd.Mukhtar. still same error ....
Parameter is not valid
ooffooooo at 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 at 28-Nov-12 0:28am
   
Try after updating below line.
 
Report.PrintToPrinter(1, false, 0, 0);
Mohammed Abdul Muqeet at 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
0 OriginalGriff 468
1 Sergey Alexandrovich Kryukov 305
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Uma Shankar Patel 80
0 Sergey Alexandrovich Kryukov 6,557
1 OriginalGriff 5,966
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2014
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