Friday, January 31, 2014

How to write SSRS report into server folder as Excel File

Public Sub RenderReportToExcel(ByVal _ReportPath As String)

    ReportViewerCostReport.Visible = True
    ReportViewerCostReport.Reset()
    ReportViewerCostReport.ServerReport.ReportPath = "/myReportFolder/reports/" + _ReportPath
    ReportViewerCostReport.ServerReport.ReportServerUrl = New System.Uri("http://localhost/ReportServer")
    ReportViewerCostReport.ServerReport.ReportServerCredentials = New MyReportServerCredentials2()

    Dim myparamProjectID As ReportParameter
    Dim myparamsProjectID As New List(Of ReportParameter)
    myparamProjectID = New ReportParameter("ProjectID", DropDownListPrj.SelectedValue)
    myparamsProjectID.Add(myparamProjectID)
    ReportViewerCostReport.ServerReport.SetParameters(myparamsProjectID)

    Dim warnings As Warning
    Dim streamIds As String
    Dim mimeType As String = String.Empty
    Dim encoding As String = String.Empty
    Dim extension As String = "xls"

    Dim bytes As Byte() = ReportViewerCostReport.ServerReport.Render("Excel", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)

    Dim oFileStream As System.IO.FileStream
    Dim strFilePath As String = String.Empty
    strFilePath = Server.MapPath("~/YourFolderOnRoot/FileName.xls")

    oFileStream = New System.IO.FileStream(strFilePath, System.IO.FileMode.Create)
    oFileStream.Write(bytes, 0, bytes.Length)
    oFileStream.Close()

  End Sub
Reference url:http://forums.asp.net/t/1746293.aspx?How+to+write+SSRS+report+into+server+folder+as+Excel+File

Exporting to Word/PDF using Microsoft Report (RDLC) without using Report Viewer

protected void Page_Load(object sender, EventArgs e)
{
    LocalReport report = new LocalReport();
    report.ReportPath = "Report1.rdlc";
    ReportDataSource rds = new ReportDataSource();
    rds.Name = "DataSet1";//This refers to the dataset name in the RDLC file
    rds.Value = EmployeeRepository.GetAllEmployees();
    report.DataSources.Add(rds);
    Byte[] mybytes = report.Render("WORD");
    //Byte[] mybytes = report.Render("PDF"); for exporting to PDF
    using (FileStream fs = File.Create(@"D:\SalSlip.doc"))
    {
        fs.Write(mybytes, 0, mybytes.Length);
    }
}  




Reference url:http://www.codeproject.com/Articles/492739/Exporting-to-Word-PDF-using-Microsoft-Report-RDLC

How to render client report definition files (.rdlc) directly to the Response stream without preview

/// <summary>
/// References:
/// </summary>
private void RenderReport() {
    LocalReport localReport = new LocalReport();
    localReport.ReportPath = Server.MapPath("~/Report.rdlc");
  
    //A method that returns a collection for our report
    //Note: A report can have multiple data sources
    List<Employee> employeeCollection = GetData();

    //Give the collection a name (EmployeeCollection) so that we can reference it in our report designer
    ReportDataSource reportDataSource = new ReportDataSource("EmployeeCollection", employeeCollection);
    localReport.DataSources.Add(reportDataSource);

    string reportType = "PDF";
    string mimeType;
    string encoding;
    string fileNameExtension;

    //The DeviceInfo settings should be changed based on the reportType
    //http://msdn2.microsoft.com/en-us/library/ms155397.aspx
    string deviceInfo =
    "<DeviceInfo>" +
    "  <OutputFormat>PDF</OutputFormat>" +
    "  <PageWidth>8.5in</PageWidth>" +
    "  <PageHeight>11in</PageHeight>" +
    "  <MarginTop>0.5in</MarginTop>" +
    "  <MarginLeft>1in</MarginLeft>" +
    "  <MarginRight>1in</MarginRight>" +
    "  <MarginBottom>0.5in</MarginBottom>" +
    "</DeviceInfo>";

    Warning[] warnings;
    string[] streams;
    byte[] renderedBytes;

    //Render the report
    renderedBytes = localReport.Render(
        reportType,
        deviceInfo,
        out mimeType,
        out encoding,
        out fileNameExtension,
        out streams,
        out warnings);

    //Clear the response stream and write the bytes to the outputstream
    //Set content-disposition to "attachment" so that user is prompted to take an action
    //on the file (open or save)
    Response.Clear();
    Response.ContentType = mimeType;
    Response.AddHeader("content-disposition""attachment; filename=foo." + fileNameExtension);
    Response.BinaryWrite(renderedBytes);
    Response.End();

}


reference url: http://weblogs.asp.net/rajbk/archive/2006/03/02/How-to-render-client-report-definition-files-_28002E00_rdlc_2900_-directly-to-the-Response-stream-without-preview.aspx

Wednesday, January 22, 2014

how many saturdays and sundays between two dates with sql server

declare @StartDate datetime,@EndDate datetime
set @StartDate='20140101'
set @EndDate='20140131'

                           
                            select count(*) as Daycount
                                                                    from master..spt_values as Number
                                                                    where Number.type = 'P' and
                                                                          dateadd(day, Number.number, @StartDate) <= @EndDate and
                                                                          datepart(dw, dateadd(day, Number.number, @StartDate)) in ( 1,7)



SELECT  dATE , COUNT(dATE)   FROM   (
                SELECT TOP (datediff(DAY,@StartDate,@EndDate) + 1 ) [Date] = DATENAME(dw , dateadd(DAY,ROW_NUMBER() OVER(ORDER BY c1.name),
                            DATEADD(DD,-1,@StartDate)))        
                            FROM   [master].[dbo].[spt_values] c1 ) X
                            GROUP BY Date


                                                                         
          -- mondays count with replication                                      
     DECLARE
   @StartDate date = '2014-01-01',
   @EndDate date = '2014-01-31';

WITH A AS (SELECT DayCount = DateDiff(day, @StartDate, @EndDate) + 1),
B AS (
   SELECT
      DayCount,
      WeekCount = DayCount + 6 / 7,
      Dow = DateDiff(day, '18991230', @StartDate) % 7 FROM A
)

SELECT
   MondayCount =
      Len(Replace(Substring(
         Replicate('0100000', WeekCount),
         Dow, DayCount
      ), '0', ''))
FROM B;

Wednesday, January 15, 2014

Serverside alert control in C#

     Serverside alert control 
   string javaScript = "<script language=JavaScript>\n" + "alert(' Data has been added successfully '); location = 'Home.aspx';\n" + "</script>";
        ClientScript.RegisterStartupScript(typeof(Page), "btnSave_Click", javaScript);




With UpdatePanel Serverside alert control

    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), Guid.NewGuid().ToString(), "alert(' Data has been added successfully'); window.location ='Home.aspx';", true);