Wednesday, February 27, 2019

Swagger UI : Can't read from file https:///swagger/docs/v1

I recently noticed that web api swagger UI contains Error notification on footer 
SchemaValidation Message
once I click on it and  it redirects to other page and contains below message


{"schemaValidationMessages":[{"level":"error","message":"Can't read from file https:///swagger/docs/v1"}]} 
Swagger UI use online validator to web api swagger document. If API URL is not accessible in public, then
this schema validation message is occurred.

There are two ways to rectify this schema validation error. 
·         Disable Swagger Validation in SwaggerConfig.cs  

GlobalConfiguration.Configuration.EnableSwagger().EnableSwaggerUi(c => { c.DisableValidator(); }); 

·         API URL should be public available. 

Thanks for visiting!!

Friday, February 22, 2019

SQL Not NULL Constraint

SQL Not NULL constraint ensures the columns cannot have NULL value and if you insert or update NULL value to this column, it throws exception

Cannot insert the value NULL into column 'StatusCode', table 'dbo.OrderStatus'; column does not allow nulls. INSERT fails.



Syntax: Create NOT NULL constraint on Create Table

The below SQL will create a NOT NULL constraint on StatusCode Column

Create Table OrderStatus

(

       StatusID int IDENTITY(1,1) NOT NULL,

       StatusCode varchar(3) NOT NULL,

       StatusDescription varchar(50),

       CONSTRAINT [OrderStatus_PK] PRIMARY KEY CLUSTERED ( StatusID ASC),  

)



SQL Constraints

In this blog, we will discuss about the SQL constraints like Primary Key, Not NULL etc. and what type of constraints are available in SQL and their purposes.

SQL constrains is used to enforce the data rule while inserting or updating record in table. If rule is not satisfied, it will not allowed to complete the action. It ensures the data accuracy and reliability in the table.

Following SQL constraints are used in SQL: 
  • Primary Key -  it uniquely identify each row in table but it cannot accept NULL value
  • Unique Key -  it ensure the column’s value are not duplicate
  • Foreign Key – it enforce the integrity constraints and make sure the referenced key is uniquely identify each row in the referenced table
  • Check  - it ensure the inserted or updated value matched with given values
  • Not NULL – it ensures the column cannot accept NULL value
  • Default – set default value for a column, if value is not specified.
Thanks for visiting!!

how to create SQL unique constraint


We will discuss about the SQL unique constraint and difference between Primary Key and Unique key and demonstrate how to create/drop unique constraint

The SQL unique constraint ensures that all values in a columns are unique.
By default Primary Key constraint also granted the uniqueness of columns values and the only difference is that primary key does not accept NULL value but unique key constraint accept a single NULL value in column.

Difference between Primary Key Vs Unique Key:

·         Primary Key does not accept NULL value but Unique Key do
·         Only one Primary key per table but we can create multiple unique key per table.

Here is a table OrderStatus, which contains Primary Key on StatusID

Create Table OrderStatus

(

       StatusID int IDENTITY(1,1) NOT NULL,

       StatusCode varchar(3) NOT NULL,

       StatusDescription varchar(50) NOT NULL,

       CONSTRAINT [OrderStatus_PK] PRIMARY KEY CLUSTERED ( StatusID ASC)

)


Syntax: Create SQL Unique Constraint on Create Table:

The below SQL script will create a unique constraint  OrderStatusCode_UC  ’ on  a Column StatusCode in table and it ensures that Column should not duplicate Code.

Create Table OrderStatus

(

       StatusID int IDENTITY(1,1) NOT NULL,

       StatusCode varchar(3) NOT NULL,

       StatusDescription varchar(50) NOT NULL,

       CONSTRAINT [OrderStatus_PK] PRIMARY KEY CLUSTERED ( StatusID ASC),

       CONSTRAINT OrderStatusCode_UC UNIQUE (StatusCode)

)


Syntax: Create SQL Unique Constraint on Alter Table:

The below SQL script will create a unique constraint ‘OrderStatusCode_UC’ on already existing table

ALTER TABLE OrderStatus

ADD CONSTRAINT OrderStatusCode_UC UNIQUE (StatusCode);


The below SQL script will create a unique constraint ‘OrderStatusDescCode _UC’ on multiple columns

ALTER TABLE OrderStatus

ADD CONSTRAINT OrderStatusDescCode_UC UNIQUE (StatusCode, StatusDescription);


Drop SQL Unique Key Constraint:

You can simply use below SQL script to Drop unique constraint

ALTER TABLE OrderStatus

DROP CONSTRAINT OrderStatusDescCode_UC;

Wednesday, February 20, 2019

SSRS : Set RDLC Report Orientation To Landscape

In this blog, we will discuss how to set print orientation to landscape (by default it is portrait) in rdlc report.
  • Go to rdlc report (report designer)
  • Right Click on the report page and select Report properties
RDLC Report Properties
  • Select Page Setup à Select Landscape under Orientation.
RDLC Report Page Orientation



Wednesday, February 13, 2019

SSRS : merge multiple rdlc report PDF in single PDF

In this blog we will discuss how to merge more than one PDF in single PDF file generated by RDLC report.


Here is code to generate PDF report data (in memory stream) from rdlc file:
C# code:

public MemoryStream WritePDFReport(List<ReportModelreportData)
        {
            Microsoft.Reporting.WinForms.Warning[] warnings;
            string[streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;

            string deviceInfo =
          "<DeviceInfo>" +
          "  <OutputFormat>PDF</OutputFormat>" +
          "</DeviceInfo>";

            ReportViewer viewer = new ReportViewer();
            try
            {
                string exeFolder = System.AppDomain.CurrentDomain.BaseDirectory;
                string reportPath = Path.Combine(exeFolderstring.Format(@"Sample.rdlc));

                viewer.ProcessingMode = ProcessingMode.Local;
                viewer.LocalReport.ReportPath = reportPath;                              
                
                ReportDataSource ds = new ReportDataSource("ReportDataSet"reportData);
                viewer.LocalReport.DataSources.Add(ds);

               viewer.LocalReport.Refresh();
              
               byte[] bytes = viewer.LocalReport.Render("PDF"deviceInfo
               out mimeTypeout encoding, out extension, out streamIdsout warnings);

                return new MemoryStream(bytes);           
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                viewer.LocalReport.Dispose();
                viewer = null;
            }
        }

WritePDfReport() method takes report data as input and it generates report by using ReportViewer Class and it calls Render method to generate the memory stream of report.

the above code generates only single PDF file and if you want to generate more than one fPDF iles and merge all into a single PDF file, for this you need to use PDFsharp.


Here is code to merge PDF files
C# Code:

using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;


          List<ReportModelreportData;

          PdfDocument document = new PdfDocument();
            for (int ix = 0; ix < 4; ix++)
            {
                PdfDocument tempPDFDoc = PdfReader.Open(WritePDFReport(reportData), PdfDocumentOpenMode.Import);

                for (int i = 0; i < tempPDFDoc.PageCounti++)
                {
                    PdfPage page = tempPDFDoc.Pages[i];
                    document.AddPage(page);
                }
            }

            document.Save("C://logs/sample.pdf");       
            document.Close()

·         Call WritePDFReport() method to generate report and get memory stream ( in byte arrays)
·         Import report data into temporary PdfDocument object - tempPDFDoc by Call PdfReader.Open() method
·         Add the imported PDF page from into Main PdfDocument object – document.
·         Call PdfDocument ‘s save method to save all imported pdf page into single pdf page (sample.pdf)
·         Call Close() method of PdfDocument – document

For more information about PDFSharp dll, Code Samples and installation steps please visit at below URLs
·         http://www.pdfsharp.net 
Thanks for visiting!!

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...