title: How to stop crystal report viewer from asking login credentials when opening subreport
body: <p>I currently have a crystal report embedded in a visual studio website. I deploy the website and install it on an IIS, and provide a link to the user so they can access globally and see this report. This system has been working great. </p>
  
  <p>However, when I embedded a crystal report that has a sub report, the credentials are not being passed to the sub-report automatically. When i debug the solution, the initial report opens fine, when I click the item to open the sub-report, crystal report viewer asks me for database login credentials. </p>
  
  <p>How can i pass those credentials automatically in the code so that users dont have to enter the code when viewing in the crystal report viewer.</p>
  
  <p>Below is the code i use in my default.aspx.cs page. It contains the connection strings. </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.SqlClient;
  
  namespace CFIBInventory
  {
      public partial class _Default : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              SqlConnection con = new SqlConnection("Data Source=HPL-WTS;Initial Catalog=Enterprise32;Persist Security Info=True;User ID=sa;Password=********");
              DataSet1 ds = new DataSet1();
              SqlDataAdapter adapter = new SqlDataAdapter("SELECT     dbo.Material.MaterialCode, dbo.Material.CategoryCode, dbo.Material.Description, dbo.MaterialOnHand.LocationCode, dbo.Material.ValuationMethod,                       dbo.MaterialOnHand.Quantity FROM         dbo.Material INNER JOIN                      dbo.MaterialOnHand ON dbo.Material.MaterialCode = dbo.MaterialOnHand.MaterialCode WHERE     (dbo.Material.CategoryCode = 'CFIB3') AND (dbo.Material.ValuationMethod = 1) AND (dbo.Material.InactiveFlag = 0)", con);
  
              adapter.Fill(ds.cfibInventory);
              CrystalReport1 report = new CrystalReport1();
              report.SetDataSource(ds);
              CrystalReportViewer1.ReportSource = report;
  
              CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
          }
      }
  }
  </code></pre>
  
  <p>The crystal report viewer is embedded into my .aspx page via the code below:</p>
  
  <pre><code>&lt;%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
      CodeBehind="Default.aspx.cs" Inherits="CFIBInventory._Default" %&gt;
  
  &lt;%@ Register assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %&gt;
  
  &lt;asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"&gt;
  &lt;meta http-equiv="X-UA-Compatible" content="IE=9"/&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"&gt;
      &lt;p&gt;
      &lt;/p&gt;
      &lt;CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
          AutoDataBind="true" /&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>The query for the sub report is in the sub-reports' database expert. </p>
  
  <p>By the way, the database is connecting to a windows server 2005 machine. I do not believe it has public access setup, such as its own IIS. The previous reports, that do not have subreports, are installed on a newer 2012 Server as thats where the database is. Not sure if the machine has anything to do with the additional login prompt in crystal report viewer.</p>
  
  <p>Any help will be great. Thank you in advance!</p>
  
  <hr>
  
  <h2>EDIT: After implementing solution from Nimesh</h2>
  
  <p>Well i added the connected connection type however, visual studio is underlining 'report' saying: Cannot use local variable 'report' before it is declared. </p>
  
  <p>foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in <strong>report</strong>.Database.Tables)</p>
  
  <p>and</p>
  
  <p>foreach (ReportDocument subreport in <strong>report</strong>.Subreports)</p>
  
  <p>I noticed i declare report later below as new crystalreport1. So I move that declaration above Nimesh code block and the red underline for 'report' goes away, but then all 6 instances of 'crtableLogoninfo' get underlined in red with the error: The name 'crtableLogoninfo' does not exist in the current context' </p>
  
  <p>Any further help will be greatly appreciated.</p>
  
  <p>Below is how my code looks now:</p>
  
  <pre><code> using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Data.SqlClient;
      using CrystalDecisions.Shared;
      using CrystalDecisions.CrystalReports.Engine;
  
      namespace CFIBInventory
      {
          public partial class _Default : System.Web.UI.Page
          {
  
  
              protected void Page_Load(object sender, EventArgs e)
              {
  
  
  
                  //Nimesh code
                  ConnectionInfo crConnectionInfo = new ConnectionInfo();
                  crConnectionInfo.ServerName = "HPL-WTS";
                  crConnectionInfo.DatabaseName = "Enterprise32";
                  crConnectionInfo.UserID = "sa";
                  crConnectionInfo.Password = "*********";
  
                  foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in report.Database.Tables)
                  {
                      crTableLogoninfo = CrTable.LogOnInfo;
                      crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                      CrTable.ApplyLogOnInfo(crtableLogoninfo);
                  }
                  foreach (ReportDocument subreport in report.Subreports)
                  {
                      foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in subreport.Database.Tables)
                      {
                          crtableLogoninfo = CrTable.LogOnInfo;
                          crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                          CrTable.ApplyLogOnInfo(crtableLogoninfo);
                      }
                  } // nimesh code end
  
                  //Old connection string, i assume this shouldnt be here since nimesh code is for connecting
                  //SqlConnection con = new SqlConnection("Data Source=HPL-WTS;Initial Catalog=Enterprise32;Persist Security Info=True;User ID=sa;Password=123qwerTy987");
  
                  DataSet1 ds = new DataSet1();
                  SqlDataAdapter adapter = new SqlDataAdapter("SELECT     dbo.Material.MaterialCode, dbo.Material.CategoryCode, dbo.Material.Description, dbo.MaterialOnHand.LocationCode, dbo.Material.ValuationMethod,                       dbo.MaterialOnHand.Quantity FROM         dbo.Material INNER JOIN                      dbo.MaterialOnHand ON dbo.Material.MaterialCode = dbo.MaterialOnHand.MaterialCode WHERE     (dbo.Material.CategoryCode = 'CFIB3') AND (dbo.Material.ValuationMethod = 1) AND (dbo.Material.InactiveFlag = 0)", con);
  
                  adapter.Fill(ds.cfibInventory);
  
                  CrystalReport1 report = new CrystalReport1();
  
                   // OLD CODE
                  //report.SetDataSource(ds);
                  CrystalReportViewer1.ReportSource = report;
  
                  CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
              }
          }
      }
  </code></pre>