Uncategorized

Generate and Download PDF file on Ajax Post method, C#

Requirements: On ajax post, save data to database, generate invoice and download as PDF file. There are many open source packages available to generate PDF file. So I’m not going describe that using which paid/free package is used to generate PDF file. I am going to describe the trick how can download the generated PDF file without saving on hard drive.

HomeController.CS


public class HomeController : Controller
    {
        public ActionResult Index()
        {         
            return View();
        }

        public ActionResult Download()
        {
            //using (var ms = new MemoryStream())
            //{
                //using (var document = new Document(PageSize.A4, 50, 50, 15, 15))
                //{
                //    PdfWriter.GetInstance(document, ms);
                //    document.Open();
                //    document.Add(new Paragraph("HelloWorld"));
                //    document.Close();
                //}
                //Response.Clear();
                ////Response.ContentType = "application/pdf";
                //Response.ContentType = "application/octet-stream";
                //Response.AddHeader("content-disposition", "attachment;filename= Test.pdf");
                //Response.Buffer = true;
                //Response.Clear();
                //var bytes = ms.ToArray();
                //Response.OutputStream.Write(bytes, 0, bytes.Length);
                //Response.OutputStream.Flush();
           // }

            return View();
        }

        [HttpPost]
        public ActionResult Save()
        {

// write code here to save the data in database. 
            var fName = string.Format("MYPDF-{0}.pdf", DateTime.Now.ToString("s"));
            using (var ms = new MemoryStream())
            {
                using (var document = new Document(PageSize.A4, 50, 50, 15, 15))
                {
                    PdfWriter.GetInstance(document, ms);
                    document.Open();
                    document.Add(new Paragraph("HelloWorld"));
                    document.Close();
                }

                var bytes = ms.ToArray();              
                Session[fName] = bytes;
            }

            return Json(new { success = true, fName }, JsonRequestBehavior.AllowGet);
            //return View();
        }
         public ActionResult DownloadInvoice(string fName)
         {
             var ms =  Session[fName] as byte[] ;
             if(ms == null)
                 return new EmptyResult();
             Session[fName] = null;
             return File(ms, "application/octet-stream", fName);
         }
    }

Index.cshtml

 <input type="button" id="btnSave" value="Save & Download PDF" />
 
 $(document).ready(function () {
 $("#btnSave").click(function () {
 $.ajax({
 type: 'POST',
 url: "/home/save",
 dataType: "json",
 success: function (resultData)
 {
 if (resultData.success) {
 window.location = "/home/DownloadInvoice" + "?fName=" + resultData.fName;
 }
 }
 });
 })
 })
 

On ajax post method, data will save into the database. Here Save is a post method. Check the HomeController.CS file. Generate file and save an array of byte in Session. In JSON result, send the name of file. Ajax post method retrieve the data and using window.location send request to server to download the file. Now, check DownloadInvoice method in HomeController.cs.

Thank you for reading this post.

Most important topics

Static Constructor

  • A static constructor is used to initialize the static data once or performed an action to once only.
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • User has no control when static constructor executed. In real life scenario static constructor use when class write the log file.

Is MVC Design Pattern or Architectural Pattern?
MVC is an architectural pattern. ASP.NET MVC is a part of ASP.NET Web application framework.

Caching
Caching is a use to improve performance of website by storing frequently access data and reusing that data.
Advantages: Reduce hosting server round trip. Reduce database server round trips. Reduce network traffic.
Disadvantages: Avoid caching if data unique per user. Avoid caching if database server has not enough RAM. For caching of dynamic contents that change frequently, define a short cache–expiration time rather than disabling caching.

Delegates
A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.

Difference between Web Services, WCF and Web API
Web Services

  • It supports only http protocol.
  • It is based on SOAP and return data in XML format only
  • It can be host only on IIS

WCF

  • It supports http, https, tcp, MSMQ protocol.
  • It is based on SOAP and return data in XML format only
  • It can be hosted with in the applicaion or on IIS or using window service
  • WCF Rest support XML, JSON and ATOM data format.

Web API

  • It supports http protocol.
  • It supports MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, unit testing that makes it more simple and robust.
  • It can be hosted with in the application or on IIS.
  • WCF Rest support XML, JSON data format.

Components of WCF
There are three main components in WCF.

  • Service Class
  • Hosting Environment
  • End Point

Service Class
Service Contract attribute define saying which application interface will be exposed as a service.

[ServiceContract()]
public interface IServiceGetResult
{
}

Operation Contract
Operation Contract dictates which methods should be exposed to the external client using this service.

[ServiceContract()]
public interface IServiceGetResult
{
[OperationContract()]
decimal GetPercentage(Student model);
}

Data Contract
Data Contract attributes defines which type of complex data will be exchanged between the client and the service. They determine which parameters to be serialized. When you are using simple data types like int, it is not necessary that you need to mark the data contract attribute. Because you will always find matching types on the client. However, complex structure like one shown in the below code snippet you will need to define a data contract. Remember data contract define how this data will be passed during transmission. In short data contract attribute define how data will be serialized will transmission.

As data contract are all about serialization you need to import System.Runtime.Serialization name space.
[DataContract]
public class Student
{
int StudentId {get;set;}
string GrNo {get;set;}
string Name {get;set;}
}
public class serviceGetResult: IServiceGetResult
{
public decimal GetPercentage(Student model)
{
decimal percentage=100;
//get percentage stored procedure call or calculation here.
return percentage;
}
}

Hosting Environment
Self-hosting the service in his own application domain. The service comes in to existence when you create the object of Service Host class and the service closes when you call the Close of the Service Host class.

Host in application domain or process provided by IIS Server.

Host in Application domain and process provided by WAS (Windows Activation Service) Server.

End Points
WCF offers its services to its client using an endpoint. An endpoint comprises of three key elements, the address, binding and contract.

WCF endpoints provide the necessary resources and directions, which help clients to communicate with the various services WCF offers.

We commonly refer the three elements (attributes) as ABC.

01) A stands for Address
02) B stands for Binding
03) C stands for Contract

UK Postcode Validation

Regular expression for JavaScript

var regPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/;

var result = regPostcode.test(SearchTerm);

if (!result) {
alert(“Please enter valid postcode”);
return false;
}

Regular expression for C#

string regPostcode = “([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}”;

System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(regPostcode);

if(!regx.IsMatch(address0.PostalCode))
{
results.Add(new ValidationResult(String.Format(“Please enter valid postcode for {0}.”, addressRank)));
}

You can check the regular expression at http://www.regexplanet.com/

Postal code format and sample postcodes are as below.

Format Example
A9 9AA S1 1AA
A99 9AA M60 1NW
AA9 9AA CR2 6XH
AA99 9AA DN55 1PT
A9A 9AA W1A 1HQ
AA9A 9AA EC1M 1BB

Cube and Rollup Operator in SQL Server 2008

CUBE operator is used with Group by clause. It use to get the subtotal and grand total of all permutation of columns provided by the CUBE operator.

ROLLUP operator is used with Group by clause. It use to get the subtotal and grand total of a set of columns provided by the ROLLUP operator.

For example,


Declare @tmpTable table(Product varchar(10), Shop varchar(10), Quantity int)
insert into @tmpTable (Product, Shop, Quantity) values ('Tea', 'Tea-Post',100)
insert into @tmpTable (Product, Shop, Quantity) values ('Tea', 'GH2',90)
insert into @tmpTable (Product, Shop, Quantity) values ('Puva', 'Tea-Post',10)
insert into @tmpTable (Product, Shop, Quantity) values ('Puva', 'GH2',25)
insert into @tmpTable (Product, Shop, Quantity) values ('Lassi', 'Anand',2)
insert into @tmpTable (Product, Shop, Quantity) values ('Lassi', 'Nadiad',20)
insert into @tmpTable (Product, Shop, Quantity) values ('Lassi', 'Khambhat',50)
select * from @tmpTable

SELECT Product,Shop,sum(Quantity) FROM @tmpTable GROUP BY ROLLUP (Product,Shop)

SELECT Product,Shop,sum(Quantity) FROM @tmpTable GROUP BY CUBE (Product,Shop)

Result would be

Cube_Rollup_SQL

Insert multiple rows into a table from another table and get identity values

Let me explain the case first, so you will get the idea when this is required to use. In the database, there is more than one tables which store the future values and will move on specific time to standard tables. For example,
Table “UserList2” contains the future values consider as temporary table. Table “UserList1” contains standard values consider as standard table. SQL SCRIPT as below:

GO
/****** Object: Table [dbo].[UserList2] Script Date: 11/26/2015 07:59:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[UserList2](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[City] [varchar](50) NULL,
CONSTRAINT [PK_UserList2] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[UserList2] ON
INSERT [dbo].[UserList2] ([Id], [Name], [City]) VALUES (1, N'Prakash', N'Anand')
INSERT [dbo].[UserList2] ([Id], [Name], [City]) VALUES (2, N'Rupal', N'Baroda')
INSERT [dbo].[UserList2] ([Id], [Name], [City]) VALUES (3, N'Arpit', N'Anand')
INSERT [dbo].[UserList2] ([Id], [Name], [City]) VALUES (4, N'Vishal', N'Baroda')
INSERT [dbo].[UserList2] ([Id], [Name], [City]) VALUES (5, N'Ajay', N'Gandhinagar')
INSERT [dbo].[UserList2] ([Id], [Name], [City]) VALUES (6, N'Kamal', N'Rajkot')
INSERT [dbo].[UserList2] ([Id], [Name], [City]) VALUES (7, N'Akshay', N'Rajkot')
INSERT [dbo].[UserList2] ([Id], [Name], [City]) VALUES (8, N'Khimjibhai', N'Gandhinagar')
SET IDENTITY_INSERT [dbo].[UserList2] OFF
/****** Object: Table [dbo].[UserList1] Script Date: 11/26/2015 07:59:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[UserList1](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[City] [varchar](50) NULL,
[UserList1Id] [int] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[UserList1] ON
INSERT [dbo].[UserList1] ([Id], [Name], [City], [UserList1Id]) VALUES (1, N'Prakash', N'Anand', 1)
INSERT [dbo].[UserList1] ([Id], [Name], [City], [UserList1Id]) VALUES (2, N'Arpit', N'Anand', 3)
INSERT [dbo].[UserList1] ([Id], [Name], [City], [UserList1Id]) VALUES (3, N'Prakash', N'Anand', 1)
INSERT [dbo].[UserList1] ([Id], [Name], [City], [UserList1Id]) VALUES (4, N'Arpit', N'Anand', 3)
INSERT [dbo].[UserList1] ([Id], [Name], [City], [UserList1Id]) VALUES (5, N'Prakash', N'Anand', 1)
INSERT [dbo].[UserList1] ([Id], [Name], [City], [UserList1Id]) VALUES (6, N'Arpit', N'Anand', 3)
SET IDENTITY_INSERT [dbo].[UserList1] OFF

Want to move data from UserList2 to UserList1 and need to use UserList1 Identity column for further process, how to get all newly inserted which as below:


DECLARE @output TABLE (id int)

Insert into UserList1 (Name, City, UserList1Id)
OUTPUT inserted.Id INTO @output
SELECT Name, City, Id FROM UserList2 Where City='Anand'

select * from @output

OUTPUT clause introduced in MS SQL Server 2005. Want to know more about OUTPUT clause refer this link

Remove illegal characters from file name or file path in c#

File cannot be saved if file name has illegal characters. So how to remove the illegal characters by a single line code as below. I use LINQ Querty for that.


string fileName = "prakash C/O swami.xml";
fileName = System.IO.Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty));

Now the fileName is “rakash CO swami.xml”. The code removes slash ‘/’ because it is a illegal characters. As same any illegal characters will remove using this above code.

SQL Server Transaction

BEGIN TRANSACTION
BEGIN TRY

    -- put SQL commands here    INSERT/UPDATE/Delete

    -- if successful - COMMIT the work
    COMMIT TRANSACTION
END TRY
BEGIN CATCH
    -- handle the error case (here by displaying the error)
    SELECT 
        ERROR_NUMBER() AS ErrorNumber,
        ERROR_SEVERITY() AS ErrorSeverity,
        ERROR_STATE() AS ErrorState,
        ERROR_PROCEDURE() AS ErrorProcedure,
        ERROR_LINE() AS ErrorLine,
        ERROR_MESSAGE() AS ErrorMessage

    -- in case of an error, ROLLBACK the transaction    
    ROLLBACK TRANSACTION

    -- if you want to log this error info into an error table - do it here 
    -- *AFTER* the ROLLBACK
END CATCH

Selected value not set on DropDownListFor() in MVC

Let me tell you small story.

There is a ‘title’ column name for salutation in database. Based on that, I create a view model so property name is ‘title’ and list name is ‘titles’. Due to property name is ‘title’, selected value is not set on DropdownListFor. Because ‘title’ is a reserved keyword. It is an attribute of any html element.

I renamed property name in viewmodel ‘title’ to ‘salutation’. All is working fine..

Enjoy day 🙂

How to display Progress bar while page is loading

<pre>

<code>

protected void Page_Load(object sender, EventArgs e)
{
showPageLoad();
//do somthing
    }
private void showPageLoad()
{
int i=0;
Response.Write(“<div id=’divTitle’>Loading(<span id=’message’>0%</span>)</div>”);
Response.Flush();
for(i=5;i<=100;i=i+5)
{
System.Threading.Thread.Sleep(200);// any codes can be typed here
outputflash(i.ToString() + “%”);
Response.Flush();
}
}
private void removePageLoad()
{
ScriptManager.RegisterStartupScript(Page, this.GetType(), “deleteLoading”, “var d = document.getElementById(‘divTitle’);d.parentNode.removeChild(d);”, true);
    }
private void outputflash(string value)
{
Response.Write(“<script type=’text/javascript’>document.getElementById(‘message’).innerHTML='” + value + “‘;</script>”);
    }

</code>

</pre>

Download / Upload Files on FTP Server

Require following ftp details where you want to upload the files:

FTP URL: “ftp://ftp.yoursite.com/”
FTP Username: username
FTP Password: password
FTP Port: 21

Upload File


public string UploadFile(string FtpUrl, string fileName, string userName, string password, string UploadDirectory = "")
        {
            string PureFileName = new FileInfo(fileName).Name;
            String uploadUrl = String.Format("{0}{1}/{2}", FtpUrl, UploadDirectory, PureFileName);
            FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
            req.Proxy = null;
            req.Method = WebRequestMethods.Ftp.UploadFile;
            req.Credentials = new NetworkCredential(userName, password);
            req.UseBinary = true;
            req.UsePassive = true;
            byte[] data = File.ReadAllBytes(fileName);
            req.ContentLength = data.Length;
            Stream stream = req.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();
            FtpWebResponse res = (FtpWebResponse)req.GetResponse();
            return res.StatusDescription;
        }

Download File


 public static string DownloadFile(string FtpUrl,string FileNameToDownload,
                        string userName, string password,string tempDirPath)
    {
        string ResponseDescription = "";
        string PureFileName = new FileInfo(FileNameToDownload).Name;
        string DownloadedFilePath  =  tempDirPath+"/"+PureFileName;
        string downloadUrl = String.Format("{0}/{1}", FtpUrl, FileNameToDownload);
        FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(downloadUrl);
        req.Method = WebRequestMethods.Ftp.DownloadFile;
        req.Credentials = new NetworkCredential(userName, password);
        req.UseBinary = true;
        req.Proxy = null;
        try
        {
            FtpWebResponse response = (FtpWebResponse)req.GetResponse();
            Stream stream = response.GetResponseStream();
            byte[] buffer = new byte[2048];
            FileStream fs = new FileStream(DownloadedFilePath, FileMode.Create);
            int ReadCount = stream.Read(buffer, 0, buffer.Length);
            while (ReadCount > 0)
            {
                fs.Write(buffer, 0, ReadCount);
                ReadCount = stream.Read(buffer, 0, buffer.Length);
            }
            ResponseDescription = response.StatusDescription;
            fs.Close();
            stream.Close();
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return ResponseDescription;
    }