Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
create procedure sp_summary
@SP_FROMDATE DATE,
@SP_TODATE DATE
AS
SELECT STYLE,PROCESS,SUM(QUANTITY) AS QUANTITY 
FROM TBL_PRODUCTION_INFO 
WHERE STYLE IN (SELECT STYLE FROM TBL_STYLE_INFO WHERE STATUS = 1)
AND PROD_DATE BETWEEN CONVERT(DATE,@SP_FROMDATE) AND CONVERT(DATE,@SP_TODATE)
GROUP BY STYLE, PROCESS


C#
ReportDocument rptDoc = new ReportDocument();
                string workingDirectory = Environment.CurrentDirectory;
                //string workingDirectory = Application.StartupPath;

                string rptPath = Directory.GetParent(workingDirectory).Parent.FullName + "\\sewing\\sewingReports\\StyleSummaryDateWise.rpt";
                //string rptPath = @"D:\NewProject\SkyTree Cost Management System\reports\CustomerWiseReport.rpt";
                //string rptPath = @"reports\\CustomerWiseReport.rpt";
                rptDoc.Load(rptPath);
                rptDoc.SetParameterValue("@SP_FROMDATE", Convert.ToDateTime(fromDateTxt.Text));
                rptDoc.SetParameterValue("@SP_TODATE", Convert.ToDateTime(todateTxt.Text));
                crystalReportViewer1.ReportSource = rptDoc;
                crystalReportViewer1.Refresh();


What I have tried:

When I open crystal report it shows error message "database connection error". I make this report with store procedure. What's the problem with this code?
Posted
Updated 19-Feb-22 6:32am

I do NOT see the part of code responsible for connection.

Check this out:
VB.Net crystal report connection string - Stack Overflow[^]
Setting the Connection String Programmatically on a Crystal Reports ReportDocument[^]

Here is an example in VB.NET:
VB.NET
Private Sub CrystalReportViewer1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Load
        Dim cryRpt As New ReportDocument
        Dim crtableLogoninfos As New TableLogOnInfos
        Dim crtableLogoninfo As New TableLogOnInfo
        Dim crConnectionInfo As New ConnectionInfo
        Dim CrTables As Tables
        Dim CrTable As Table
        cryRpt.Load("E:\ColorLab1\colorlab\colorlab\rpt_bill.rpt")

        With crConnectionInfo
            .ServerName = "MAHESH\SQLEXPRESS"
            .DatabaseName = "cc"
            .UserID = "erp"
            .Password = "123"
        End With

        CrTables = cryRpt.Database.Tables
        For Each CrTable In CrTables
            crtableLogoninfo = CrTable.LogOnInfo
            crtableLogoninfo.ConnectionInfo = crConnectionInfo
            CrTable.ApplyLogOnInfo(crtableLogoninfo)
        Next
        CrystalReportViewer1.RefreshReport()
    End Sub


Good luck!
 
Share this answer
 
Please check your database connection. It may be closed.
Sometimes runtimes .NET framework has changed the way that it binds to older mixed-mode assemblies and getting stuck. Use the following attribute in your App.config which is useful if your application uses legacy activation paths.
<startup useLegacyV2RuntimeActivationPolicy="true">
	<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
 
Share this answer
 
This works for me. Thanks for your answer.

ReportDocument rptDoc = new ReportDocument();
                string workingDirectory = Environment.CurrentDirectory;
                //string workingDirectory = Application.StartupPath;

                string rptPath = Directory.GetParent(workingDirectory).Parent.FullName + "\\sewing\\sewingReports\\StyleSummaryDateWise.rpt";                
                rptDoc.Load(rptPath);
                rptDoc.SetParameterValue("@SP_FROMDATE", fromDateTxt.Value.ToString("yyyy-MM-dd"));
                rptDoc.SetParameterValue("@SP_TODATE", todateTxt.Value.ToString("yyyy-MM-dd"));
                crystalReportViewer1.ReportSource = rptDoc;
                crystalReportViewer1.Refresh();
 
Share this answer
 
Comments
Maciej Los 20-Feb-22 10:06am    
This code is exactly the same as the code provided in your question. Why did you change the code in your question?

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