Search code examples
c#sql-servercrystal-reports

Search By Invoice ID in crystal reports


i want to see the data only with the invoice ID i enter so i tried this code to do that

        Crystal_Bill cr = new Crystal_Bill();
        SqlConnection conect = new SqlConnection("Data Source=DESKTOP-R34C6VV\\SQL;Initial Catalog=Restaurant;Integrated Security=True");
        string sql = "Select * from Orders where InvoiceID='"+PrepareBill_txt1.Text+"'";
        DataSet dt = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter(sql,conect);
        adapter.Fill(dt,"Orders");
        cr.SetDataSource(dt.Tables["Orders"]);
        open.crystalReportViewer1.ReportSource = cr;
Print open = new Print();
open.SHow();

but it did not work i still get all the data in the database is there is a problem in these codes ? can anyone fix it ? thanks this is my data base

CREATE TABLE [dbo].[Orders] (
[InvoiceID] INT          NOT NULL,
[ItemNO]    INT          NOT NULL,
[Category]  VARCHAR (50) NULL,
[ItemName]  VARCHAR (50) NULL,
[Price]     FLOAT (53)   NULL,
[Qty]       INT          NOT NULL,
[SubTotal]  FLOAT (53)   NULL,
CONSTRAINT [Orders_FK1] FOREIGN KEY ([InvoiceID]) REFERENCES [dbo].[Invoice] ([InvoiceID])

);


Solution

  • As per your code you are passing invoice ID as a parameter value in your SQl Query, Your invoice ID has INT datatypes and you are trying to pass it with a single quote in your query so that consider invoice id as a varchar value. You can remove a single quote and try once again. that may help you.

    i.g:

    string sql = "Select * from Orders where InvoiceID="+ PrepareBill_txt1.Text +"";