Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i use this code to show the results of searching into crystalreportviewer.
it filters data between two date and two id .
when i execute it, it doesnt show any result.
what's wrong in this code?????

private void btnSearch_Click(object sender, EventArgs e)
       {
 
            SqlDataAdapter adapt = new SqlDataAdapter();
            adapt.SelectCommand = new SqlCommand();
            adapt.SelectCommand.Connection = cnn;
            adapt.SelectCommand.CommandType = CommandType.Text;
            adapt.SelectCommand.CommandText = "SELECT * FROM  View_Selling WHERE (khdate >='" + txtFromDate.Text.Trim() + "' AND khdate <  '" + txtToDate.Text.Trim() + "') AND (id >='" + txtFromCode.Text.Trim() + "' AND id < '" + txtToCode.Text.Trim() + "')    ";
            DataSet dset = new DataSet();
            cnn.Open();
            adapt.Fill(dset, "View_Selling");
            cnn.Close();
            CrystalReport1 objRPT = new CrystalReport1();
            objRPT.SetDataSource(dset);
            crystalReportViewer1.ReportSource = objRPT;

 
       }


help me
thanks alot

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 19-Jul-11 22:41pm
v2

You are actually doing a compare on the id.
In that case, you will need to probably use numeric types rather than string types.

Remove the quotes in the query <='" + txtFromCode.Text.Trim() + "' AND should probably be <=" + txtFromCode.Text.Trim() + " AND.
 
Share this answer
 
To add to what Abhinav says, don't do it that way at all. Concatenating strings is a very bad idea - it can lead to SQL Injection attacks, were a user can accidentally of deliberately destroy your database. Use parametrised queries instead:
adapt.SelectCommand = new SqlCommand();
adapt.SelectCommand.Connection = cnn;
adapt.SelectCommand.CommandType = CommandType.Text;
adapt.SelectCommand.CommandText = "SELECT * FROM  View_Selling WHERE (khdate >=@FRDATE AND khdate < @TODATE) AND (id >=@FRID AND id < @TOID)";
adapt.SelectCommand.Parameters.AddWithValue("@FRDATE", DateTime.Parse(txtFromDate.Text.Trim()));
adapt.SelectCommand.Parameters.AddWithValue("@TODATE", DateTime.Parse(txtToDate.Text.Trim()));
...
This will probably get rid of your problem anyway, since the user is unlikely to be entering the dates in the correct (i.e. "yyyy-MM-dd") format for SQL server anyway.
 
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