Click here to Skip to main content
15,867,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Error is:
error "An expression of non-boolean type specified in a context where a condition is expected, near '


What I have tried:

C#
public partial class ProductWiseReportViewer : Form
{
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=StockSF;Integrated Security=True");
    ReportDocument crypt = new ReportDocument();
    public ProductWiseReportViewer()
    {
        InitializeComponent();
    }

    public DateTime date1 { get; set; }

    public DateTime date2 { get; set; }

    public object prname { get; set; }

    private void crystalReportViewer1_Load(object sender, EventArgs e)
    {
        //this.reportViewer1.RefreshReport();
        SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tblPurchase WHERE Pur_Date between '" + date1 + "'and '" + date2 + "'and'"+prname+"'",con);
        DataTable dt = new DataTable();
        DataSet dst = new DataSet();
        sda.Fill(dst, "tblPurchase");
        crypt.Load(@"F:\Project C#\StockSF\StockSF\RptPurchase.rpt");
        crypt.SetDataSource(dst);
        crystalReportViewer1.ReportSource = crypt;
    }
}
Posted
Updated 4-Mar-17 0:51am
v2
Comments
Graeme_Grant 4-Mar-17 6:29am    
Which line is the error being thrown on? Is it one of these and which one?
crypt.SetDataSource(dst);
crystalReportViewer1.ReportSource = crypt;
[no name] 4-Mar-17 6:44am    
You should use a proper parameterized query, then the spacing problem in your query string would not be giving you that error.

1 solution

Um.
First off, don't do that. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
But look at what text your query as written generates:
SQL
SELECT * FROM tblPurchase WHERE Pur_Date between '2016-03-01'and '2017-02-28'and'prname'

lets add a few brackets, and you can see what SQL is looking at:
SQL
SELECT * FROM tblPurchase WHERE (Pur_Date BETWEEN '2016-03-01' AND '2017-02-28') AND 'prname'
So SQL expects prname to be a boolean expression.
Probably, you mean something like:
SQL
... AND 'prname' = 'myusername'
But that's up to you.

And I'm serious about the string concatenation : fix it everywhere in your app, or your best mate will delete your DB just to see the look on your face...
 
Share this answer
 

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



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