Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two table one table contain product information and another table contain date information.I want to generate crystal report using these two tables from selected date from date picker. i am able to generate report without date range.
so how to pass those two date parameter to my crystalreportviewer.

What I have tried:

private ProductSell Getdata()
{
DateTime sdt = dateTimePicker1.Value.Date;
DateTime edt = dateTimePicker2.Value.Date;
SqlConnection connection= new SqlConnection(cn);

SqlCommand cmd = new SqlCommand("Select ProductName,sum(ProductCost)as ProductCost,Sum(ProductPrice)as ProductPrice,sum(ProductQuantity)as ProductQuantity from ProductSell Where GROUP BY ProductName", connection);

// SqlCommand cmd = new SqlCommand("SELECT ProductSell.ProductName, sum(ProductSell.ProductCost)as pc,sum(ProductSell.ProductPrice)as pp,sum(ProductSell.ProductQuantity)as pq FROM ProductSell INNER JOIN Project ON ProductSell.ProjectID = Project.ProjectID WHERE (Project.Date >='"+sdt+"') AND (Project.Date <='"+edt+"') GROUP BY ProductSell.ProductName", connection);

SqlDataAdapter sda = new SqlDataAdapter(cmd);
ProductSell dsproductsell = new ProductSell();
sda.Fill(dsproductsell, "ProdSellDT");
return dsproductsell;
}

private void button1_Click(object sender, EventArgs e)
{
ProductSellReport psreport = new ProductSellReport();
ProductSell dsproductsell = Getdata();
psreport.SetDataSource(dsproductsell);
this.ProductSellReportViewer.ReportSource = psreport;
this.ProductSellReportViewer.RefreshReport();
}
Posted
Updated 24-Sep-16 19:49pm

1 solution

try like this
C#
SqlCommand cmd = new SqlCommand("SELECT ProductSell.ProductName, sum(ProductSell.ProductCost)as pc,sum(ProductSell.ProductPrice)as pp,sum(ProductSell.ProductQuantity)as pq FROM ProductSell INNER JOIN Project ON ProductSell.ProjectID = Project.ProjectID WHERE (Project.Date >=@sdt) AND (Project.Date <=@edt) GROUP BY ProductSell.ProductName", connection);
              cmd.Parameters.Add("@sdt", startDateObj);
              cmd.Parameters.Add("@sdt", endDateObj);


always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
 
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