Categories
.NET ASP.NET

ASP.NET MVC 5: Session not working

Just encounter an issue with ASP.NET MVC 5, the sessions are not working. i.e. when I put a value in controller like Session[“mysession”], it do not shows the same value in Views or in fact anywhere else. Search online, some create custom classes to manage them, and some give different patch, finally got the easiest solution. Just add “Session_Start” function call in Global.asax file and we are good to go.

Categories
Article ASP.NET HTML/CSS PHP Programming

PDF Creation: wkhtmltopdf

In past I post about how good wkhtmltopdf is. I have used it for few of my projects. Really it is the cheapest [as it is open source and free] and powerful solution. But it is not the reason I love it. I love this because it is the Easiest solution available. Most of PDF library range few hundred dollars to start with. Then we learn through their call of function to draw Tables and add media content and text. But with wkhtmltopdf, all you need to do is simple create a HTML page, add CSS and Javascript (yup, it support jQuery’s document load function and other other load method of your favourite javascript library.)

I recently test jQuery load function as I need to write text as Arc, however I don’t want to use Image as image manipulation will generate a rectangle image and then placing it going to be problem. But with wkhtmltopdf, and it’s support for JavaScript it make my life easier. I use a cool ArcText js following a nice article/tutorial http://tympanus.net/codrops/2012/01/24/arctext-js-curving-text-with-css3-and-jquery/ .

And yes, I recently saw that develpoment of wkhtmltopdf has started again. So this is going to be very good for those who want easy and best solution for their application.

Categories
.NET ASP.NET C#.NET VB.NET/VB

ASP.NET: Why my cookie is not saving

Funny, today after years I thought to finally use Cookies in one of my project. It is probably my first attempt to use Cookie in ASP.NET in last 5 yrs. WOW, that is amazing. Anyways, I just notice one thing and thought it might help any first timer, so here is the key note I want to share. In ASP.NET if you want to read Cookie, use REQUEST.Cookies, but if you want to add new Cookie use RESPONSE.Cookies. This is very minor difference and I really waste 30 minutes before I release I was using Request.Cookies to add new cookie, but it won’t get added that way.

Categories
Article ASP.NET Bookmarked C#.NET PHP Programming VB.NET/VB

WKHTMLTOPDF: A Free PDF solution

As any other developer we often find it difficult to get a Website to print something accurately, specially when printing labels for mailing as they are outside the default print area for most browser and no client want to adjust or memorize the print setting to adjust them manually. Also it doesn’t make sense to relay on client to do adjustments.

So the only universal solution I found is.. of course PDF files. But how do I create PDF. Well easy enough use some library. I did that I use iTextPDF very good Library that works for my C# project well for couple of years. But then what is the problem now? Well iTextPDF is now have commercial license at ridiculous pricing in fact it is now most expensive to any other paid software. Yet another Open Source project going paid on success. 

Also, there is a problem with library like iTextPDF that they need you to draw each and every element programmatically more often or not. And you find yourself caught in a web of long written code. I remember one of my report’s PDF generation code goes 1000 lines. I hate it. So, the best solution is to Generate PDF from a HTML source. We can handle HTML much better way and rather easily with our skills as web programmer. And even a designer in team can change PDF design giving time for programmer for other work. 

In this regard I look for free alternative of iTEXTPDF, which does have HTML parser to generate PDF from HTML file, and my search for now ends on http://code.google.com/p/wkhtmltopdf/ (wkhtmltopdf ) project. A Simple Executable that I can call from a my code to execute and it does the parsing. 

It is not really a good idea to call it through web page, but we should create a Windows service and use it there, but I have successfully test it to work on web server with quite a load and at least with my limitation to access Windows Service I can live with it running through HTML page, after all my PDF is just 1-2 page and it works fine. Will post code for it some other day. But do try it if you want a good and quick solution to your problem.

Categories
.NET ASP.NET C#.NET Programming

Sandcastle: Code Documentation in C#

Over the years I use lot of documentation and those help me build my software with ease. However, never need to build one for my projects as they are not quite often big and what more never need to do so, as they are quite handful of function in complete project. But now in order to upgrade my coding and making it more friendly for my brain. I finally was looking for a decent method to start documenting my code in separate code files much like PHP or MSDN documentation. 

The best was to start with .NET documentation tools as they are more readily available. After searching and trying to make sense what all these tools are good for, I finally reach to Sandcastle. Install it, but found nothing in my start menu. Read about it, seems a command line tool. But I need GUI, been a lazy programmer. Well I got the answer in term of Sandcastle Help File Builder on Codeplex (http://shfb.codeplex.com/) . Looks promising, I install it. And within 3 minute of installation, my documentation is ready. How?

Simple. I open the software, Add Documentation Source as my project SLN file, click build and it create file for me. I need nothing else. There maybe lot of configuration to do, but for first time, it is more than enough. I have been commeting my code day I start programming. And from about a year or so, I start using those /// commenting as in XML generation tool within Visual Studio. But never compile them in help file. 

Now I know easiest method to do. i.e. open Builder GUI, click build [For second time of course]. I will keep generating lot of help file so I can check them back when needed in future. Changing my programming style when nobody is there to talk or see face to face is enjoyable. Me and my computer sitting alone and doing lot of new learning. Going back to days of programmer is really good to continue my learning process that I left 4 years ago.

I am still good and hope to improve further.

Categories
.NET ASP.NET C#.NET VB.NET/VB WPF

GDI+ Generic Error

I see lot of people asking about GDI+ Generic Error. Since this is “generic” error nobody can tell what is wrong with code. So did a work around and get it working. I had this error quite a time in early days of my programming and just found same error in one of junior’s code. Well from my experience I can say one thing. GDI+ Generic error 99.9% means “developer miss a detail or overlooked something”. Most common reason of error are negligence of

1. Path should be physical path

2. All folder should present before you create file.

3. Previous files should be in write mode if case of overriding files.

4. Make sure permission are for writing to User of process.

5. Do not include any special character which windows do not permit in folder name or file name.

6. FILE FORMAT has nothing to do with this error most often.

7. Recheck permission if you try all above.

8. Recheck Path again :).

Point #7 and #8 are most important solution and always works for me.

Categories
.NET ASP.NET C#.NET Concept/algorithm Database VB.NET/VB

Static Object in ASP.NET

Recently I found a problem in one of application we developed. Here my colleague use a Static SqlConnection object through a class in his code. He did that to save his effort of writing and Connection everytime. Everything works great during our development and online reviewing. However as soon as we lauch website in beta mode, it start creating problem which seems bit random in occurance and hence were hard to track. During this time I get involve in the project development and I was not very big fan on Static object and start to think against that code, which looks good to me at first. However for some reason I miss the basic of C# and ASP.NET and hence it took a while to recognize the problem was indeed the static code. So for those who did same mistake here is explanation of why Static object didn’t work in ASP.NET for connection.

When ASP.NET application is first called the complete code is loaded in Memory, specially the pages in App_Code folder and they act as part of web server and not as standard PHP site where they run and loaded on each request. Hence, once this happens all static object become static in memory for each and every request your server will get. This is indeed good way to share information between all User and all request, if that help your case. But in most general case you never want to share information between user in this dynamic method, which is not permanent. Anyways, for connection it really doesn’t make a difference if you share the object as you anyways want to open connection and close it as soon as you are done. In most case you have 1 simple insert or fetch query to run. And in a website with several users at a time it hardly make a difference as Sqlconnection are open and close in jiffy and took less than 1 second in most cases. For a shopping cart which has 100 -200 order per month this case will work just fine.

However, since connection is shared, there is every possible chance that if you fire a SQL query for say Reader, it get shared with some user on some other session and this is what you don’t want. It happens to our project when we have Bulk CSV upload which usually take 30-50 seconds, but if someone during this time fires and open other page of website they start to get random Database related errors. From SqlReader is close to field “Xyz” is not available, as they tend to share the connection with other requests.

Hope you avoid those problem. The colleague who did this in our project is not working with us anymore, but hey, where ever you are , please don’t punish us like this again !!!