Thursday, April 26, 2012

Working with HTML Tags

Textbox:

To set width use , Style="width:100px;"


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

Saturday, February 25, 2012

Alter Table ,add Column ,alter Column datatype

Adding column

You can add new columns to an existing table. 

ALTER TABLE [dbo].[TableName]
ADD   NameOfNewColumn Varchar(500) NULL  

GO

Alter column

You can add modify an existing column
ALTER TABLE [dbo].[TableName]
 
ALTER COLUMN [NameOfNewColumn ]  int NULL

GO

Drop column

You can add modify an existing column
ALTER TABLE [dbo].[TableName]
Drop
[NameOfNewColumn ]  int

GO 

For more details visit msdn link

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

Friday, February 24, 2012

Post Form Data using jQuery $.post() or jQuery.post()

Post Form Data using jQuery $.post() or jQuery.post()


// script tag in html page

$("#btnGetDetails").click(function (){

var txtOrderId= $("#txtOrderId").val(); // assuming form1 is the id of the form we are trying to submit
var url= "controllerName/actionName" // set target url

$.post(url,{OrderId:txtOrderId},function(res){
// process Response Data
});

});

//  Controller Action

public ActionResult actionName(int OrderId)
{
// TO do task
return resultstring;
}

Name specified in Red should match otherwise we wont be able to get values on Action method.

But if think same we cam do to post form data then this is not the case where it will work. To post form data
we need some changes in code as specified below:


$("#btnSubmit").click(function (){

var formData= $("#form1").serialize(); // assuming form1 is the id of the form we are trying to submit
var url= "controller/action" // set target url

$.post(url,formData,function(res){
// process Response Data
});

});
//  Controller Action

public ActionResult actionName(OrderModel modelData) // assuming view is strong typed view
{
// TO do task
return resultstring;
}

public ActionResult actionName(FormCollection modelData) // assuming view is not strong typed view
{
// TO do task
return resultstring;
}

in this case we don't need to map parameter in action method and post parameters.

Edit: 3 May 2012

A few days back i was caught in a situation where i had to send some other parameter along with formData so initial i wasnt aware of what should i need to do but with the help of my friend (Rohit Rao)  i got this solution.
so if we have to send additional parameter with formdata

$("#btnSubmit").click(function (){

var formData= $("#form1").serialize(); // assuming form1 is the id of the form we are trying to submit
var url= "controller/action?param1=val1&param2=val2" // set target url Line:1
// url= "controller/action"                        Line:2
$.post(url,formData,function(res){
// process Response Data
});
});

Line 1 with query sting parameter and Line 2 is without parameter we can call same action with following action method definition.

//  Controller Action

public ActionResult actionName(string param1="",string param2="",OrderModel modelData) // assuming view is strong typed view
{
// TO do task
return resultstring;
}
we have assigned default values to param1 and param2 in the case where both are missing from query string parameter.

Note: I am not sure about below action method because it isn't tested  but as much as i know there isn't any reason it shouldn't work.
public ActionResult actionName(string param1="",string param2="",FormCollection modelData) // assuming view is not strong typed view
{
// TO do task
return resultstring;
}



I hope this will help you.

Happy Living...
Happy Concept...
Happy Coding....