locked
ViewBag is always null after deployment while creating report. RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    Why my view bag is always gets null after deployment for the application. 

    I put breakpoint  for debugging and make connection to the live server and checked the view bag is not null and it is generation the report perfectly.

    @if (ViewBag.ReportViewer != null)
    {
        @Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer, new { @style = "width:896px;height:642px" })
    }

    Even when I connect it to local database it is also generating report perfectly. But when I deploy it on server then it makes viewbag null.

    It always shows the blank page because viewbag is null.

    Please suggest.
     

    Tuesday, October 22, 2019 8:46 AM

Answers

  • User753101303 posted

    I checked it is not null when I check by putting a breakpoint.

    "it" being? The error message clearly shows ViewBag.ReportViewer is null (ViewBag is not).

    Anyway as pointed already it tuns out you hide any exception that could happen earlier. Rather than trying to guess by moving your app around and testing distinct situations just get rid of that to get at the root cause exception.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 23, 2019 11:08 AM
  • User475983607 posted

    Please stop sharing images of the exception and just share the text.  Anyway, the error is very specific.  The dll is not found in the application's bin folder.  Move the dll from the repo to the application's bin folder.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 23, 2019 11:42 AM

All replies

  • User753101303 posted

    Hi,

    If ViewBag.ReportViewer is null here, the problem should be likely where ViewBag.ReportViewe is defined.

    I dislike using "as" when not needed (if ViewBag.ReportViewer is not null but is not a Microsoft.Reporting.WebForms.ReportViewer then   the ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer expression returns null).

    Tuesday, October 22, 2019 9:03 AM
  • User1052662409 posted

    PatriceSc

    If ViewBag.ReportViewer is null here, the problem should be likely where ViewBag.ReportViewe is defined.

    I checked it is not null when I check by putting a breakpoint.

    PatriceSc

    if ViewBag.ReportViewer is not null but is not a Microsoft.Reporting.WebForms.ReportViewer then   the ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer expression returns null

    Yes this may be the possibility. Then how should I use it without as?

    Tuesday, October 22, 2019 9:14 AM
  • User753101303 posted

    The usual cast expression ie :

    (Microsoft.Reporting.WebForms.ReportViewer)ViewBag.ReportViewer


    See https://blogs.msdn.microsoft.com/prakasht/2013/04/23/difference-between-direct-casting-is-and-as-operator-in-c/

    A direct cast is preferred if the type is expected to be allways this one (or at least compatible)

    "as" is rather when the type could not match and if null you want to do something else (rather than having your app failing later because of a NullReferenceException).

    Tuesday, October 22, 2019 9:31 AM
  • User1052662409 posted

    The usual cast expression ie :

    (Microsoft.Reporting.WebForms.ReportViewer)ViewBag.ReportViewer

    @using ReportViewerForMvc
    @{
        ViewBag.Title = @Session["WebsiteName"];
    }
    
    @Html.ReportViewer((Microsoft.Reporting.WebForms.ReportViewer)ViewBag.ReportViewer)

    I did the same as suggested. But still same result.

    saaaaa

    asasa

    And my ViewBag is not null from both side niether from local database not from server database. I have already checked.

    Tuesday, October 22, 2019 9:55 AM
  • User475983607 posted

    The null argument exception is very specific.  The argument is null which is related to the ViewBag.ReportViewer.  Can you share the code that populates the ViewBag?  Have you tried running your code through the Visual Studio debugger to verify the ReportView is populated?

    Tuesday, October 22, 2019 11:37 AM
  • User1052662409 posted

    mgebhard

    an you share the code that populates the ViewBag?

    The below Action Result gives me the ViewBag. The action result doesn't have any issue I am sure with proof.

    public ActionResult Index()
            {
                try {
                    DataAccessLayer objDB = new DataAccessLayer();
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                    ReportViewer ReportViewer1 = new ReportViewer();
                    if (Request.QueryString["summary"] + "" != "")
                    {
                        if ((Session["AdminType"] + "" == "m") || (Session["_ROLE"] + "" == "HR"))
                        {
                        }
                        else
                        {
                            if (Request.QueryString["summary"] + "" != Session["adminId"] + "")
                            {
                                Response.Redirect("Dashboard/Dashboard");
                            }
                        }
                        if ((Request.QueryString["usertype"] + "" != "S") && (Request.QueryString["usertype"] + "" != "V") && (Request.QueryString["usertype"] + "" != "A"))
                        {
                            Response.Redirect("Dashboard/Dashboard");
                        }
                        ReportDataSource dt;
                        DataTable ds;
                        dt = new ReportDataSource("DataSet1");
                        //ds = GetLeaveSupervisor(Request.QueryString["supervisor"] + "", Request.QueryString["year1"] + "", Session["USER-Country"] + "");
                        ds = objDB.Getsummary_all(Request.QueryString["summary"] + "", Request.QueryString["year1"] + "", Session["USER-Country"] + "", Request.QueryString["leavetype"] + "", Request.QueryString["usertype"] + "", Request.QueryString["projectno"] + "");
                        dt.Value = ds;
    
    
                        ReportViewer1.ProcessingMode = ProcessingMode.Local;
                        // ReportViewer1.LocalReport.ReportPath = Server.MapPath("Reports/leavereport_individual_new.rdlc");
                        ReportViewer1.LocalReport.ReportPath = Server.MapPath("../Reports/leave_summary.rdlc");
    
                        if (Request.QueryString["user"] + "" == "1")
                        {
                            DisableUnwantedExportFormat(ReportViewer1, "Excel");
                            DisableUnwantedExportFormat(ReportViewer1, "WORD");
                        }
    
                        ReportViewer1.LocalReport.DataSources.Clear();
                        ReportViewer1.LocalReport.DataSources.Add(dt);
    
                        ReportParameter rptPrm = new ReportParameter("FromDate", Request.QueryString["year1"] + "");
                        ReportViewer1.LocalReport.SetParameters(rptPrm);
                        DataTable dtemp = objDB.Leave_Report_Viewer_AdminName(Request.QueryString["summary"] + "");// obj.gettable("select vcfirstname+' '+vclastname as AdminFullName from AdminMaster_GEN where bintId_Pk='" + Request.QueryString["summary"] + "" + "'");
                        if (dtemp.Rows.Count > 0)
                        {
                            string _supname = "" + dtemp.Rows[0]["AdminFullName"] + "" + "";
                            ReportParameter rptPrm1 = new ReportParameter("employeename", _supname);
                            ReportViewer1.LocalReport.SetParameters(rptPrm1);
                        }
                        else
                        {
                            ReportParameter rptPrm1 = new ReportParameter("employeename", "ALL");
                            ReportViewer1.LocalReport.SetParameters(rptPrm1);
                        }
                        DataTable dtleave = objDB.Leave_Report_Viewer_LeaveID(Request.QueryString["leavetype"] + ""); //obj.gettable("select * from Leave_Type_Master where bigid='" + Request.QueryString["leavetype"] + "" + "'");
                        if (dtleave.Rows.Count > 0)
                        {
                            string leavetype = "" + dtleave.Rows[0]["vctype"] + "" + "";
                            ReportParameter rptPrm1 = new ReportParameter("leavetype", leavetype);
                            ReportViewer1.LocalReport.SetParameters(rptPrm1);
                        }
                        else
                        {
                            ReportParameter rptPrm1 = new ReportParameter("leavetype", "ALL");
                            ReportViewer1.LocalReport.SetParameters(rptPrm1);
                        }
    
                        ReportViewer1.AsyncRendering = false;
                        ReportViewer1.SizeToReportContent = true;
                        ReportViewer1.ShowFindControls = false;
                        ReportViewer1.ServerReport.Refresh();
    
                    }
    
                    if (Request.QueryString["supervisor"] + "" != "")
                    {
                        if ((Session["AdminType"] + "" == "m") || (Session["_ROLE"] + "" == "HR"))
                        {
                        }
                        else
                        {
                            if (Request.QueryString["supervisor"] + "" != Session["adminId"] + "")
                            {
                                Response.Redirect("Dashboard/Dashboard");
                            }
                        }
    
                        ReportDataSource dt;
                        DataTable ds;
                        dt = new ReportDataSource("DataSet1");
                        //ds = GetLeaveSupervisor(Request.QueryString["supervisor"] + "", Request.QueryString["year1"] + "", Session["USER-Country"] + "");
                        ds = objDB.GetLeaveSupervisor_all(Request.QueryString["supervisor"] + "", Request.QueryString["year1"] + "", Session["USER-Country"] + "");
                        dt.Value = ds;
    
    
                        ReportViewer1.ProcessingMode = ProcessingMode.Local;
                        // ReportViewer1.LocalReport.ReportPath = Server.MapPath("Reports/leavereport_individual_new.rdlc");
                        ReportViewer1.LocalReport.ReportPath = Server.MapPath("../Reports/leavereport_individual_new_sum.rdlc");
    
                        if (Request.QueryString["user"] + "" == "1")
                        {
                            DisableUnwantedExportFormat(ReportViewer1, "Excel");
                            DisableUnwantedExportFormat(ReportViewer1, "WORD");
                        }
    
                        ReportViewer1.LocalReport.DataSources.Clear();
                        ReportViewer1.LocalReport.DataSources.Add(dt);
    
                        ReportParameter rptPrm = new ReportParameter("FromDate", Request.QueryString["year1"] + "");
                        ReportViewer1.LocalReport.SetParameters(rptPrm);
                        DataTable dtemp = objDB.Leave_Report_Viewer_AdminName(Request.QueryString["supervisor"] + ""); //obj.gettable("select vcfirstname+' '+vclastname as AdminFullName from AdminMaster_GEN where bintId_Pk='" + Request.QueryString["supervisor"] + "" + "'");
                        if (dtemp.Rows.Count > 0)
                        {
                            string _supname = "Supervisor: " + dtemp.Rows[0]["AdminFullName"] + "" + "";
                            ReportParameter rptPrm1 = new ReportParameter("extravalue", _supname);
                            ReportViewer1.LocalReport.SetParameters(rptPrm1);
                        }
    
                        ReportViewer1.AsyncRendering = false;
                        ReportViewer1.SizeToReportContent = true;
                        ReportViewer1.ShowFindControls = false;
                        ReportViewer1.ServerReport.Refresh();
                    }
                    else if (Request.QueryString["leader"] + "" != "")
                    {
                        if ((Session["AdminType"] + "" == "m") || (Session["_ROLE"] + "" == "HR"))
                        {
                        }
                        else
                        {
                            if (Request.QueryString["leader"] + "" != Session["adminId"] + "")
                            {
                                Response.Redirect("Dashboard/Dashboard");
                            }
                        }
    
                        ReportDataSource dt;
                        DataTable ds;
                        dt = new ReportDataSource("DataSet1");
                        ds = objDB.GetLeaveLeader(Request.QueryString["leader"] + "", Request.QueryString["year1"] + "", Session["USER-Country"] + "");
                        dt.Value = ds;
    
    
                        ReportViewer1.ProcessingMode = ProcessingMode.Local;
                        ReportViewer1.LocalReport.ReportPath = Server.MapPath("../Reports/leavereport_individual_new.rdlc");
    
                        if (Request.QueryString["user"] + "" == "1")
                        {
                            DisableUnwantedExportFormat(ReportViewer1, "Excel");
                            DisableUnwantedExportFormat(ReportViewer1, "WORD");
                        }
    
                        ReportViewer1.LocalReport.DataSources.Clear();
                        ReportViewer1.LocalReport.DataSources.Add(dt);
    
                        ReportParameter rptPrm = new ReportParameter("FromDate", Request.QueryString["year1"] + "");
                        ReportViewer1.LocalReport.SetParameters(rptPrm);
                        DataTable dtemp = objDB.Leave_Report_Viewer_AdminName(Request.QueryString["leader"] + "");//obj.gettable("select vcfirstname+' '+vclastname as AdminFullName from AdminMaster_GEN where bintId_Pk='" + Request.QueryString["leader"] + "" + "'");
                        if (dtemp.Rows.Count > 0)
                        {
                            string _supname = "Team Leader: " + dtemp.Rows[0]["AdminFullName"] + "" + "";
    
                            ReportParameter rptPrm1 = new ReportParameter("extravalue", _supname);
                            ReportViewer1.LocalReport.SetParameters(rptPrm1);
                        }
                        ReportViewer1.AsyncRendering = false;
                        ReportViewer1.SizeToReportContent = true;
                        ReportViewer1.ShowFindControls = false;
                        ReportViewer1.ServerReport.Refresh();
                    }
                    else if (Request.QueryString["dateval"] + "" != "")
                    {
                        if ((Session["AdminType"] + "" == "m") || (Session["_ROLE"] + "" == "HR"))
                        {
                        }
                        else
                        {
                            if (Request.QueryString["empid"] + "" != Session["adminId"] + "")
                            {
                                Response.Redirect("Dashboard/Dashboard");
                            }
                        }
    
                        string startdate = "", enddate = "", projectno = "", empid = "", type = "";
                        if (Request.QueryString["projectno"] + "" != "")
                            projectno = Request.QueryString["projectno"] + "";
                        else
                            projectno = null;
    
                        if (Request.QueryString["type"] + "" != "")
                            type = Request.QueryString["type"] + "";
                        else
                        {
                            type = "";
                            DataTable dttype = objDB.Leave_Report_Viewer_CountryID(Session["USER-Country"] + "");//obj.gettable("select * from Leave_type_master where CountryId='" + Session["USER-Country"] + "" + "'");
                            if (dttype.Rows.Count > 0)
                            {
                                for (int i = 0; i < dttype.Rows.Count; i++)
                                {
                                    type += dttype.Rows[i]["bigid"] + ",";
                                }
                                if (type.Length > 0)
                                {
                                    type = type.Substring(0, type.Length - 1);
                                }
                            }
                        }
    
                        if (Request.QueryString["empid"] + "" != "")
                            empid = Request.QueryString["empid"] + "";
                        else
                            empid = null;
                        if (Request.QueryString["startdate"] + "" != "")
                            startdate = Request.QueryString["startdate"] + "";
                        else
                            startdate = null;
                        if (Request.QueryString["enddate"] + "" != "")
                            enddate = Request.QueryString["enddate"] + "";
                        else
                            enddate = null;
                        ReportDataSource dt;
                        DataTable ds;
                        dt = new ReportDataSource("DataSet1");
                        ds = objDB.GetLeavereport_filter(projectno, empid, startdate, enddate, type);
                        dt.Value = ds;
    
    
                        ReportViewer1.ProcessingMode = ProcessingMode.Local;
                        ReportViewer1.LocalReport.ReportPath = Server.MapPath("../Reports/leavereport_filter.rdlc");
    
                        if (Request.QueryString["user"] + "" == "1")
                        {
                            DisableUnwantedExportFormat(ReportViewer1, "Excel");
                            DisableUnwantedExportFormat(ReportViewer1, "WORD");
                        }
    
                        ReportViewer1.LocalReport.DataSources.Clear();
                        ReportViewer1.LocalReport.DataSources.Add(dt);
    
                        ReportParameter rptPrm = new ReportParameter("FromDate", (Convert.ToDateTime(startdate)).ToString("dd/MM/yyyy"));
                        ReportViewer1.LocalReport.SetParameters(rptPrm);
                        ReportParameter rptPrm1 = new ReportParameter("ToDate", (Convert.ToDateTime(enddate)).ToString("dd/MM/yyyy"));
                        ReportViewer1.LocalReport.SetParameters(rptPrm1);
    
                        ReportViewer1.AsyncRendering = false;
                        ReportViewer1.SizeToReportContent = true;
                        ReportViewer1.ShowFindControls = false;
                        ReportViewer1.ServerReport.Refresh();
                    }                
                    else if (Request.QueryString["leader1"] + "" != "")
                    {
    
                        if (Request.QueryString["leader1"] + "" == "All")
                        {
                            //------------Comprehensive team report For All Employee----------//
    
                            ReportDataSource dt;
                            DataTable ds;
                            dt = new ReportDataSource("DataSet1");
                            ds = objDB.GetLeaveLeader_summary_All("0", Request.QueryString["year1"] + "", Session["USER-Country"] + "");
                            dt.Value = ds;
    
    
                            ReportViewer1.ProcessingMode = ProcessingMode.Local;
                            ReportViewer1.LocalReport.ReportPath = Server.MapPath("../Reports/leavereport_leader_summary_All.rdlc");
    
                            if (Request.QueryString["user"] + "" == "1")
                            {
                                DisableUnwantedExportFormat(ReportViewer1, "Excel");
                                DisableUnwantedExportFormat(ReportViewer1, "WORD");
                            }
    
                            ReportViewer1.LocalReport.DataSources.Clear();
                            ReportViewer1.LocalReport.DataSources.Add(dt);
    
                            ReportParameter rptPrm = new ReportParameter("FromDate", Request.QueryString["year1"] + "");
                            ReportViewer1.LocalReport.SetParameters(rptPrm);
                            //DataTable dtemp = obj.gettable("select vcfirstname+' '+vclastname as AdminFullName from AdminMaster_GEN"); // where bintId_Pk='" + Request.QueryString["leader1"] + "" + "'");
                            //if (dtemp.Rows.Count > 0)
                            //{
                            //    string _supname = "Team Leader: " + dtemp.Rows[0]["AdminFullName"] + "" + "";
    
                            //    ReportParameter rptPrm1 = new ReportParameter("extravalue", _supname);
                            //    ReportViewer1.LocalReport.SetParameters(rptPrm1);
                            //}
                            ReportViewer1.AsyncRendering = false;
                            ReportViewer1.SizeToReportContent = true;
                            ReportViewer1.ShowFindControls = false;
                            ReportViewer1.ServerReport.Refresh();
                        }
                        else
                        {
                            if ((Session["AdminType"] + "" == "m") || (Session["_ROLE"] + "" == "HR"))
                            {
                            }
                            else
                            {
                                if (Request.QueryString["leader1"] + "" != Session["adminId"] + "")
                                {
                                    Response.Redirect("Dashboard/Dashboard");
                                }
                            }
    
    
                            //------------Comprehensive team report For Single Employee----------// 
    
                            ReportDataSource dt;
                            DataTable ds;
                            dt = new ReportDataSource("DataSet1");
                            ds = objDB.GetLeaveLeader_summary(Request.QueryString["leader1"] + "", Request.QueryString["year1"] + "", Session["USER-Country"] + "");
                            dt.Value = ds;
    
    
                            ReportViewer1.ProcessingMode = ProcessingMode.Local;
                            ReportViewer1.LocalReport.ReportPath = Server.MapPath("../Reports/leavereport_leader_summary.rdlc");
    
                            if (Request.QueryString["user"] + "" == "1")
                            {
                                DisableUnwantedExportFormat(ReportViewer1, "Excel");
                                DisableUnwantedExportFormat(ReportViewer1, "WORD");
                            }
    
                            ReportViewer1.LocalReport.DataSources.Clear();
                            ReportViewer1.LocalReport.DataSources.Add(dt);
    
                            ReportParameter rptPrm = new ReportParameter("FromDate", Request.QueryString["year1"] + "");
                            ReportViewer1.LocalReport.SetParameters(rptPrm);
                            DataTable dtemp = objDB.Leave_Report_Viewer_AdminName(Request.QueryString["leader1"] + "");//obj.gettable("select vcfirstname+' '+vclastname as AdminFullName from AdminMaster_GEN where bintId_Pk='" + Request.QueryString["leader1"] + "" + "'");
                            if (dtemp.Rows.Count > 0)
                            {
                                string _supname = "Team Leader: " + dtemp.Rows[0]["AdminFullName"] + "" + "";
    
                                ReportParameter rptPrm1 = new ReportParameter("extravalue", _supname);
                                ReportViewer1.LocalReport.SetParameters(rptPrm1);
                            }
                            ReportViewer1.AsyncRendering = false;
                            ReportViewer1.SizeToReportContent = true;
                            ReportViewer1.ShowFindControls = false;
                            ReportViewer1.ServerReport.Refresh();
                        }
                    }
    
    
                    if (Request.QueryString["val"] + "" == "1")
                    {
                        if (Request.QueryString["year"] + "" != "")
                        {
                            string fromdate = "";
                            string Todate = "";
                            string _uname = "";
                            string _sname = "";
    
                            string year = "", projectno = "", empid = "", type = "";
                            if (Request.QueryString["year"] + "" != "")
                            {
                                year = Request.QueryString["year"] + "";
                                fromdate = year;
                                Todate = year;
                            }
                            else
                                year = null;
    
                            if (Request.QueryString["projectno"] + "" != "")
                                projectno = Request.QueryString["projectno"] + "";
                            else
                                projectno = null;
    
                            if (Request.QueryString["type"] + "" != "")
                                type = Request.QueryString["type"] + "";
                            else
                            {
                                type = "";
                                DataTable dttype = objDB.Leave_Report_Viewer_CountryID(Session["USER-Country"] + "");//obj.gettable("select * from Leave_type_master where CountryId='" + Session["USER-Country"] + "" + "'");
                                if (dttype.Rows.Count > 0)
                                {
                                    for (int i = 0; i < dttype.Rows.Count; i++)
                                    {
                                        type += dttype.Rows[i]["bigid"] + ",";
                                    }
                                    if (type.Length > 0)
                                    {
                                        type = type.Substring(0, type.Length - 1);
                                    }
                                }
                            }
    
                            if (Request.QueryString["empid"] + "" != "")
                            {
                                if ((Session["AdminType"] + "" == "m") || (Session["_ROLE"] + "" == "HR"))
                                {
                                }
                                else
                                {
                                    if (Request.QueryString["empid"] + "" != Session["adminId"] + "")
                                    {
                                        Response.Redirect("Dashboard/Dashboard");
                                    }
                                }
    
                                empid = Request.QueryString["empid"] + "";
                                DataTable dts = objDB.Leave_Report_Viewer_AdminID(empid); //obj.gettable("select * from AdminMaster_GEN where bintId_Pk='" + empid + "'");
                                if (dts.Rows.Count > 0)
                                {
                                    _uname = dts.Rows[0]["vcfirstname"] + " " + dts.Rows[0]["vclastname"] + " (" + dts.Rows[0]["vcusername"] + ")";
                                    _sname = dts.Rows[0]["sapno"] + "";
                                    if (dts.Rows[0]["_joinyear"] + "" == year)
                                    {
                                        fromdate = Convert.ToDateTime(dts.Rows[0]["dateofjoining"] + "") + "";
                                    }
                                    else
                                    {
                                        fromdate = "01/01/" + year;
                                    }
                                    if (dts.Rows[0]["_endyear"] + "" == year)
                                    {
                                        Todate = Convert.ToDateTime(dts.Rows[0]["dateofend"] + "") + "";
                                    }
                                    else
                                    {
                                        Todate = "12/31/" + year;
                                    }
                                }
                            }
                            else
                                empid = null;
    
                            ReportDataSource dt;
                            ReportDataSource dt1;
                            DataTable ds;
                            DataTable ds1;
                            if (empid != null)
                            {
                                dt = new ReportDataSource("DataSet1");
                                dt1 = new ReportDataSource("DataSet2");
                                ds = objDB.GetLeaveReport(projectno, empid, year, type);
                                dt.Value = ds;
                                ds1 = objDB.GetLeaveReport_USER(empid, year, Session["USER-Country"] + "");
                                dt1.Value = ds1;
                            }
                            else
                            {
                                dt = new ReportDataSource("DataSet1");
                                ds = objDB.GetLeaveReport(projectno, empid, year, type);
                                dt.Value = ds;
                                dt1 = null;
                            }
    
    
                            ReportViewer1.ProcessingMode = ProcessingMode.Local;
                            if (empid != null)
                            {
                                ReportViewer1.LocalReport.ReportPath = Server.MapPath("../Reports/leavereport_USER.rdlc");
                            }
                            else
                            {
                                if (Request.QueryString["ind"] + "" == "1")
                                {
                                    dt = new ReportDataSource("DataSet1");
                                    //ds = GetLeaveReport_USER_Summary(year, Session["USER-Country"] + "");
                                    ds = objDB.GetLeaveReport_USER_all(year, Session["USER-Country"] + "");
                                    dt.Value = ds;
                                    dt1 = null;
                                    // ReportViewer1.LocalReport.ReportPath = Server.MapPath("Reports/leavereport_individual_new.rdlc");
                                    ReportViewer1.LocalReport.ReportPath = Server.MapPath("../Reports/leavereport_individual_new_sum.rdlc");
    
                                }
                                else
                                {
                                    ReportViewer1.LocalReport.ReportPath = Server.MapPath("../Reports/leavereport.rdlc");
                                }
                            }
    
                            if (Request.QueryString["user"] + "" == "1")
                            {
                                DisableUnwantedExportFormat(ReportViewer1, "Excel");
                                DisableUnwantedExportFormat(ReportViewer1, "WORD");
                            }
    
                            ReportViewer1.LocalReport.DataSources.Clear();
                            if (empid != null)
                            {
                                ReportViewer1.LocalReport.DataSources.Add(dt);
                                ReportViewer1.LocalReport.DataSources.Add(dt1);
                            }
                            else
                            {
                                ReportViewer1.LocalReport.DataSources.Add(dt);
                            }
                            if (empid != null)
                            {
                                ReportParameter rptPrm = new ReportParameter("FromDate", fromdate);
                                ReportParameter rptPrm1 = new ReportParameter("ToDate", Todate);
                                ReportParameter rptPrm2 = new ReportParameter("Uname", _uname);
                                ReportParameter rptPrm3 = new ReportParameter("Sname", _sname);
                                ReportViewer1.LocalReport.SetParameters(rptPrm);
                                ReportViewer1.LocalReport.SetParameters(rptPrm1);
                                ReportViewer1.LocalReport.SetParameters(rptPrm2);
                                ReportViewer1.LocalReport.SetParameters(rptPrm3);
                            }
                            else
                            {
                                ReportParameter rptPrm = new ReportParameter("FromDate", year);
                                ReportViewer1.LocalReport.SetParameters(rptPrm);
                            }
    
                            ReportViewer1.AsyncRendering = false;
                            ReportViewer1.SizeToReportContent = true;
                            ReportViewer1.ShowFindControls = false;
                            ReportViewer1.ServerReport.Refresh();
                        }
                    }
                    else
                    {
                        if (Request.QueryString["year"] + "" != "")
                        {
                            string fromdate = "";
                            string Todate = "";
                            string year = "", projectno = "", empid = "";
                            if (Request.QueryString["year"] + "" != "")
                            {
                                year = Request.QueryString["year"] + "";
                                fromdate = year;
                                Todate = year;
                            }
                            else
                                year = null;
    
                            if (Request.QueryString["projectno"] + "" != "")
                                projectno = Request.QueryString["projectno"] + "";
                            else
                                projectno = null;
    
                            if (Request.QueryString["empid"] + "" != "")
                            {
                                if ((Session["AdminType"] + "" == "m") || (Session["_ROLE"] + "" == "HR"))
                                {
                                }
                                else
                                {
                                    if (Request.QueryString["empid"] + "" != Session["adminId"] + "")
                                    {
                                        Response.Redirect("Dashboard/Dashboard");
                                    }
                                }
    
                                empid = Request.QueryString["empid"] + "";
                                DataTable dts = objDB.Leave_Report_Viewer_AdminID(empid); //obj.gettable("select * from AdminMaster_GEN where bintId_Pk='" + empid + "'");
                                if (dts.Rows.Count > 0)
                                {
                                    if (dts.Rows[0]["_joinyear"] + "" == year)
                                    {
                                        fromdate = Convert.ToDateTime(dts.Rows[0]["dateofjoining"] + "") + "";
                                    }
                                    else
                                    {
                                        fromdate = "01/01/" + year;
                                    }
                                    if (dts.Rows[0]["_endyear"] + "" == year)
                                    {
                                        Todate = Convert.ToDateTime(dts.Rows[0]["dateofend"] + "") + "";
                                    }
                                    else
                                    {
                                        Todate = "12/31/" + year;
                                    }
                                }
    
                            }
                            else
                                empid = null;
    
                            ReportDataSource dt;
                            DataTable ds;
                            dt = new ReportDataSource("DataSet1");
                            ds = objDB.GetLeaveReportAdjust(projectno, empid, year, Session["USER-Country"] + "");
                            dt.Value = ds;
    
    
                            ReportViewer1.ProcessingMode = ProcessingMode.Local;
                            ReportViewer1.LocalReport.ReportPath = Server.MapPath("../Reports/leavereport_Adjust.rdlc");
    
                            if (Request.QueryString["user"] + "" == "1")
                            {
                                DisableUnwantedExportFormat(ReportViewer1, "Excel");
                                DisableUnwantedExportFormat(ReportViewer1, "WORD");
                            }
    
                            ReportViewer1.LocalReport.DataSources.Clear();
                            ReportViewer1.LocalReport.DataSources.Add(dt);
    
                            ReportParameter rptPrm = new ReportParameter("FromDate", fromdate);
                            ReportParameter rptPrm1 = new ReportParameter("ToDate", Todate);
                            ReportViewer1.LocalReport.SetParameters(rptPrm);
                            ReportViewer1.LocalReport.SetParameters(rptPrm1);
    
                            ReportViewer1.AsyncRendering = false;
                            ReportViewer1.SizeToReportContent = true;
                            ReportViewer1.ShowFindControls = false;
                            ReportViewer1.ServerReport.Refresh();
                        }
                    }
                    ViewBag.ReportViewer = ReportViewer1;
                    return View();
                }
                catch (Exception ex)
                {
                    return View();
                }
            }

    Now I again change the razor code to 

    @using ReportViewerForMvc
    @{
        ViewBag.Title = @Session["WebsiteName"];
    }
    
    
        @Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer, new { @style = "font-size:9pt;font-family:Verdana", @Height = "642px", @ProcessingMode = "Local", @Width = "896px" })
    

    And when I run it from my local machine it works fine (USING SAME DATABASE)

    error2

    And when I deploy same code on server (Azure)  it shows error (USING SAME DATABASE).

    error2

    Although the database is same but running on local visual studio same code gives result, but after deployment same code gives null exception with a common database.

    Now lets assume if ViewBag doesn't have data then it should show at least blank report with header, which is showing when I run it on local machine , after removing data from table (USING SAME DATABASE) . See below

    111

    Please suggest.

    Wednesday, October 23, 2019 4:00 AM
  • User475983607 posted

    The code fails silently with this catch block.

                catch (Exception ex)
                {
                    return View();
                }

    Most likely the code is throwing an exception which is causes the null exception.   Catch blocks are intended to handle exceptions and provide indication of what happened.  Ignoring bugs makes your code very difficult to troubleshoot.  I recommend removing the try...catch to get the actual error.  Then design a proper exception handler.

    https://docs.microsoft.com/en-us/dotnet/standard/exceptions/how-to-use-the-try-catch-block-to-catch-exceptions

    Wednesday, October 23, 2019 10:51 AM
  • User753101303 posted

    I checked it is not null when I check by putting a breakpoint.

    "it" being? The error message clearly shows ViewBag.ReportViewer is null (ViewBag is not).

    Anyway as pointed already it tuns out you hide any exception that could happen earlier. Rather than trying to guess by moving your app around and testing distinct situations just get rid of that to get at the root cause exception.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 23, 2019 11:08 AM
  • User1052662409 posted

    mgebhard

      I recommend removing the try...catch to get the actual error. 

    PatriceSc

      Rather than trying to guess by moving your app around and testing distinct situations just get rid of that to get at the root cause exception.

    Now it is showing 

    Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.

    But I checked in my repos on azure they are available in bin folder

    Wednesday, October 23, 2019 11:25 AM
  • User475983607 posted

    Please stop sharing images of the exception and just share the text.  Anyway, the error is very specific.  The dll is not found in the application's bin folder.  Move the dll from the repo to the application's bin folder.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 23, 2019 11:42 AM
  • User753101303 posted

    Check rather then bin folder of the deployed application. It could likely be in the repo but still skipped when published if "copy local" is false.

    See  https://www.hostbuddy.com/support/kb/a307/how-to-set-the-copy-local-property-of-a-reference.aspx

    Wednesday, October 23, 2019 11:46 AM