Wednesday, March 21, 2012

submit large amount of data with jquery.post() or $.post()

There is  a limit assoicated with form key collection, and we can increase this limit by setting configuration values in web.config in our application

<appSettings>

        <add key="aspnet:MaxHttpCollectionKeys" value="50000"/>
</appSettings>

We can use this settings to reset data upload restriction implemented by IIS.
This is one of reason when we fail to submit data to server if there is large amount of data is associated with our Page.
 
 

Happy Living...
Happy Coding....
Happy Concepts....

Monday, March 5, 2012

Export HTML to Image or PDF

Export HTML to Image or PDF

A few days back one of my friend asked to me How can we export HTML page into Pdf.
We searched a lot there are a large number of thired party tools are available to do this but these
all tools are paid. So we cant buy a tool and because it also include Html reandring engine code so its
not easy to create a tool for project its time consuming task.

As i told you above we are not able to export HTML to Pdf we used a differnt approach to resolve it.
We planned to convert HTML to Image and then write down this image into Pdf and it working for us.

Now i am going to explain it Step by Step:

1. Get reference of System.UI.Form , System.Drawing
2. Create object of WebBrowser
3. On Completed event

    a) Create Bitmap object and specify size of Bitmap
    b) Convert WebBrowser to Control and then use DrawToImage()
    c) Save Bitmap to jpg file.
   
4. Add Reference of iTextSharp
5. Create a Object of Document
6. Create a Object of PdfWriter
7. Add Image to PdfWriter using PdfWriter.Add();
8. Close PdfWriter and Document Object.

Happy Living...
Happy Coding...
Happy Concepts...




Saturday, March 3, 2012

Merge to Update / Insert using a single statement


My best guess is that you can use Merge to Update / Insert using a single statement

-- Merge Statement
MERGE ProjectDetailsTransit  AS spdt1
    USING (select d.ProjectDetailsTransitID,d.Startdate ,d.enddate from DeletedDayInclude d) AS ddi
    ON spdt1.ProjectDetailsTransitID = ddi.ProjectDetailsTransitID
    WHEN MATCHED THEN UPDATE SET spdt1.startDatetime =ddi.startDate, spdt1.FinishDateTime = ddi.enddate
    WHEN NOT MATCHED THEN INSERT(startDatetime ,FinishDateTime )    VALUES(ddi.startDate,ddi.enddate );
GO

Note : In insert statement of  Merge we can not use alias name with fields for ex:
INSERT(stm.User_Id ,stm.Favorite_question )
VALUES(sd.User_Id ,(cast(@questionid as varchar(50)))

if we will write it in the way it defined into blue marked code it will give us following error :
Error 1:  The insert column list used in the MERGE statement cannot contain multi-part identifiers. Use single part identifiers instead.

Because Query parse looks for column name in target table and it densest expect any other table name. So if we will write the alias table name with column name then it will try to find "stm.User_Id" name in columns of target table,but it wont find it there we will get above error message.

 Error 2:   Sometime we get following error if we try to execute newly build sp in which we have used Merge statement:
Msg 325, Level 15, State 1, Line 7
Incorrect syntax near . You may need to set the compatibility level of the current database to a higher value to enable this feature. See help for the stored procedure sp_dbcmptlevel.

 Reason :  

Mostly with each new version of sql server we upgrade our data base and this is very true for application build several year back. In this case, the new functionality get issues to execute because still we are running database on compatibility mode of old version and it is not Compatible with new version of sqlserver for eg.
Compatibility mode is 90 for sqlserver 2005 
and Compatibility mode is 100 for sqlserver 2008


Change the database compatibility level using following command.
For SQL Server 2005:
EXEC sp_dbcmptlevel 'DatabaseName', 90
For SQL Server 2008:
EXEC sp_dbcmptlevel 'DatabaseName', 100



Happy Living...
Happy Coding...
Happy Concepts...

Thursday, March 1, 2012

get Comma separated string for Column in Sql

Assuming i have table

Create Table shifts
(
RequestNo int, ShiftId int


and for each request number we want to get a comma separated string of shift ids associated with a particulare request



select SubString
( (SELECT ', ' + convert(varchar(1),p2.ShiftID )FROM shifts p2
FOR XML PATH('')),2,200000)


Happy Living ...
Happy Coding...
Happy Concepts