Search code examples
asp.netvisual-studiosql-server-2008c#-4.0crystal-reports

crystal reports giving Database logon failed or asking for credentials


i have tried almost everything I could. I have a web app in ASP.net with c#. I am fetching data from the database tables and adding it to dataset. Then I set this dataset as the source to the report. My code is as following.

con.ConnectionString = ConfigurationManager.ConnectionStrings["familyConnectionString"].ConnectionString;

            SqlDataAdapter sda = new SqlDataAdapter("select uid, member_name, gender, dob from family where uid='"+uid+"'", con);
            DataSet1 myds = new DataSet1();
            sda.Fill(myds, "family");


            SqlDataAdapter sda1 = new SqlDataAdapter("select id from birth_certificates where p_id='"+uid+"'", con);
            sda1.Fill(myds, "birth_certificates");

            ReportDocument rpt = new ReportDocument();
            rpt.Load(Server.MapPath("birth_certi_report.rpt"));
            rpt.Refresh();
            rpt.SetDataSource(myds);
            rpt.SetDatabaseLogon("","",@".\sqlexpress","project2");
            CrystalReportViewer1.ReportSource = rpt;
            CrystalReportViewer1.DataBind();
            CrystalReportViewer1.Visible = true;
            CrystalReportViewer1.RefreshReport();

I am using integrated security so I left username and password blank. Please help.

Asking for Credentials

Please Note that the above code is written inside a DropDown_selectedIndexChanged() event. I tried adding it to the page_load but it didn't work.


Solution

  • If you are not worried about authentication ,then do it this way.Create an instance of your crystal report and set its data source.Thats all you need to do and it works. *Make sure the crystal report is within your solution.

            con.ConnectionString = ConfigurationManager.ConnectionStrings["familyConnectionString"].ConnectionString;
          // the magic
            birth_certi_report myreport = new birth_certi_report();
            DataSet1 myds = new DataSet1();
            SqlDataAdapter sda1 = new SqlDataAdapter("select id from birth_certificates where p_id='"+uid+"'", con);
            sda1.Fill(myds, "birth_certificates");
            myreport.SetDataSource(myds);
            crystalReportViewer1.ReportSource = myreport;