SQL Server I am getting "Invalid mapping type value" this error while calling in c#

epggiuax  于 2023-06-21  发布在  C#
关注(0)|答案(3)|浏览(131)

i created method to show my report then i give connectioninfo at runtime but when i connect to remote server i got error

public bool ShowReport()
{
    try
    {
        //CrystalDecisions.CrystalReports.Engine.ReportDocument oRepDoc;
        //CrystalDecisions.Shared.TableLogOnInfo oLogInfo;
        //CrystalDecisions.CrystalReports.Engine.Table oTable;
        //CrystalDecisions.CrystalReports.Engine.FieldDefinition oField;
        //CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition oParamFieldDef;
        //CrystalDecisions.Shared.ParameterValues oParamValues;
        //CrystalDecisions.Shared.ParameterDiscreteValue oDiscretParamValue;
        //ParameterFields oParamList;
        //ParameterField oParamItem;

        if (!System.IO.File.Exists(ReportPath))
        {
            MessageBox.Show("ملف التقرير غير موجود فى المسار الصحيح", MainClass.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return false;
        }
        else
        {
            oRepDoc = new ReportDocument();
            oLogInfo = new TableLogOnInfo();
            Tables t;
            oRepDoc.Load(ReportPath, OpenReportMethod.OpenReportByTempCopy);
            //***********************************************************************************

            ConnectionInfo connection = new ConnectionInfo();
            if (MainClass.IntegratedSecurity) connection.IntegratedSecurity = true;
            else
            {

                connection.IntegratedSecurity = false;
                connection.UserID = MainClass.UserId;
                connection.Password = MainClass.Password;
            }
            connection.ServerName = MainClass.ServerName;
            connection.DatabaseName = MainClass.DatabaseName;
             t= oRepDoc.Database.Tables;
             foreach (CrystalDecisions.CrystalReports.Engine.Table oTab in t)
            {
                oLogInfo = oTab.LogOnInfo;
                oLogInfo.ConnectionInfo = connection;

                oTab.ApplyLogOnInfo(oLogInfo);

                //oLogInfo = null;
            }

            //***********************************************************************************

            oRepDoc.Refresh();
            oRepDoc.VerifyDatabase(); // Very Important Line
            //***********************************************************************************
            // First Way To Pass Report Parameters Value
            foreach (Range oRange in oRangeList)
            {
                oRepDoc.SetParameterValue(oRange.RangeName, oRange.RangeValue);
            }
            oRangeList.Clear();
            //***********************************************************************************
            // Second Way To Pass Report Parameters Value
            //ParameterValues oCurrentParameterValues;
            //ParameterDiscreteValue oParameterDiscreteValue;
            //CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinitions oParameterFieldDefinitions = oRepDoc.DataDefinition.ParameterFields;
            //foreach (Range oRange in oRangeList)
            //{
            //    oCurrentParameterValues = new ParameterValues();
            //    oParameterDiscreteValue = new ParameterDiscreteValue();
            //    oParameterDiscreteValue.Value = oRange.RangeValue;
            //    oCurrentParameterValues.Add(oParameterDiscreteValue);
            //    ParameterFieldDefinition oParameterFieldDefinition = oParameterFieldDefinitions[oRange.RangeName];
            //    oParameterFieldDefinition.ApplyCurrentValues(oCurrentParameterValues);
            //}
            //oRangeList.Clear();
            //***********************************************************************************
            // Pass "Company Name" And "Telephone" And "Printed By"  To Report
            FormulaFieldDefinition oFormulaFieldDefinition;
            FormulaFieldDefinitions oFormulaFieldDefinitions = oRepDoc.DataDefinition.FormulaFields;

            oFormulaFieldDefinition = oFormulaFieldDefinitions["For_CompanyName"];
            oFormulaFieldDefinition.Text = string.Format("\"{0}\"", MainClass.CompanyName);
            oFormulaFieldDefinition = oFormulaFieldDefinitions["For_CompanyTel"];
            oFormulaFieldDefinition.Text = string.Format("\"{0}\"", MainClass.CompanyTel);
            oFormulaFieldDefinition = oFormulaFieldDefinitions["For_PrintedBy"];
            oFormulaFieldDefinition.Text = string.Format("\"{0}\"", MainClass.GetUserName(MainClass.SystemUserId));
            //***********************************************************************************
        }
        if (Destination == ReportDestination.Printer)
        {
            oRepDoc.PrintOptions.PrinterName = PrinterName;
            oRepDoc.PrintToPrinter(NumberofCopies, Collate, StartPage, EndPage);
            return true;
        }
        return true;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, MainClass.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        return false;
    }
}

when depugging oRepDoc.VerifyDatabase(); i got error invalid mapping by the way connectioninfo works on sql server

ff29svar

ff29svar1#

Tim Vasil, from this thread on MSDN, says he resolved this error by installing Microsoft SQL Server Native Client on the server machine that was being connected to.

http://social.msdn.microsoft.com/forums/en-US/vscrystalreports/thread/3c8db743-f1fb-4021-bf46-f6aee8889932

Here are some links on how to install the Microsoft SQL Server Native Client:

http://msdn.microsoft.com/en-us/library/ms131321.aspx

http://msdn.microsoft.com/en-us/sqlserver/ff658532

xfyts7mz

xfyts7mz2#

Make sure your UserId and password are correct in MainClass, that the database isn't expecting an encrypted password or that you're passing the encrypted value, and that the user has rights to that database.

3z6pesqy

3z6pesqy3#

make sure your crystal report is using microsoft oledb provider for sql server driver in your report. you can change the driver by opening report>database>verify database. then it'll ask you for credentials. press the back button and find the ms oledb thing there. you don't have to set locate it if you don't want to. below code will take care of it even if the sql instance or client pc changes

try
            {
                ConnectionInfo crconnectioninfo = new ConnectionInfo();
                TableLogOnInfo crtablelogoninfo = new TableLogOnInfo();

                crconnectioninfo.IntegratedSecurity = false;
                crconnectioninfo.ServerName = clsConnection.SERVER;
                crconnectioninfo.DatabaseName = clsConnection.DBN;
                crconnectioninfo.UserID = clsConnection.USER;
                crconnectioninfo.Password = clsConnection.PWD;

                foreach (InternalConnectionInfo r in rptDoc.DataSourceConnections)
                {
                    r.SetConnection(clsConnection.SERVER, clsConnection.DBN, clsConnection.USER, clsConnection.PWD);
                }

                foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in rptDoc.Database.Tables)
                {
                    crtablelogoninfo = CrTable.LogOnInfo;
                    crtablelogoninfo.ConnectionInfo = crconnectioninfo;
                    CrTable.ApplyLogOnInfo(crtablelogoninfo);
                    CrTable.Location = CrTable.Name;
                }
                for (int i = 0; i < rptDoc.Subreports.Count; i++)
                {
                    foreach (CrystalDecisions.CrystalReports.Engine.Table subTable in rptDoc.Subreports[i].Database.Tables)
                    {
                        crtablelogoninfo = subTable.LogOnInfo;
                        crtablelogoninfo.ConnectionInfo = crconnectioninfo;
                        subTable.ApplyLogOnInfo(crtablelogoninfo);
                        subTable.Location = subTable.Name;
                    }
                }
                rptDoc.Refresh();
                rptDoc.VerifyDatabase();

                return rptDoc;
            }
            catch
            {
                throw;
            }

相关问题