Friday, 15 February 2013

Date Time Formate

-->


In SQL Server used Cast or Convert function to Format DateTime value or column into a specific date format.Both function are used to convert datetime to varchar or string.

CAST function Syntax: CAST(expression as data_type)

Let's convert current date time to varchar

select cast(getdate() as varchar)

CONVERT function is used to change or convert the DateTime formats.By using convert
function you can get only Date part or only Time part from the datetime.

CONVERT Function Syntax- 
 CONVERT(data_type,expression,date Format style)

Let's take Sql Server DateTtime styles example:



Format

Query


USA mm/dd/yy


select convert(varchar, getdate(), 1)

ANSI yy.mm.dd


select convert(varchar, getdate(), 2)

British/French dd/mm/yy

select convert(varchar, getdate(), 3)


German dd.mm.yy

select convert(varchar, getdate(), 4)


Italian dd-mm-yy


select convert(varchar, getdate(), 5)

dd mon yy


select convert(varchar, getdate(), 6)

Mon dd, yy

select convert(varchar, getdate(), 7)


USA mm-dd-yy


select convert(varchar, getdate(), 10)

JAPAN yy/mm/dd


select convert(varchar, getdate(), 11)

ISO yymmdd

select convert(varchar, getdate(), 12)


mon dd yyyy hh:miAM

(or PM)

select convert(varchar, getdate(), 100)

mm/dd/yyyy

select convert(varchar, getdate(), 101)


yyyy.mm.dd

select convert(varchar, getdate(), 102)


dd/mm/yyyy


select convert(varchar, getdate(), 103)

dd.mm.yyyy


select convert(varchar, getdate(), 104)

dd-mm-yyyy

select convert(varchar, getdate(), 105)


dd mon yyyy

select convert(varchar, getdate(), 106)


Mon dd, yyyy

select convert(varchar, getdate(), 107)


hh:mm:ss

select convert(varchar, getdate(), 108)


Default +
milliseconds mon dd yyyy hh:mi:ss:mmmAM (or PM)


select convert(varchar, getdate(), 109)

mm-dd-yyyy

select convert(varchar, getdate(), 110)


yyyy/mm/dd


select convert(varchar, getdate(), 111)

yyyymmdd


select convert(varchar, getdate(), 112)

Europe default +
milliseconds dd mon yyyy hh:mm:ss:mmm(24h)


select convert(varchar, getdate(), 113) or
select convert(varchar, getdate(),
13)

hh:mi:ss:mmm(24h)

select convert(varchar, getdate(), 114)


18. 9. 2. Use the SqlHierarchicalDataSource control when working with the TreeView control

 
. Use the SqlHierarchicalDataSource control when working with the TreeView control
<%@ Page Language="C#" %>
<%@ Register TagPrefix="custom" Namespace="MyNamespace" %>
html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
"Head1" runat="server">
    Show TreeView

<body>
    <form id="form1" runat="server">
    


    
        id="TreeView1"
        DataSourceID="srcDiscuss"
        OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"
        ImageSet="News"
        Runat="server">
        
            
                TextField="Subject"
                ValueField="MessageId" />
        
    

    
        id="srcDiscuss"
        ConnectionString='<%$ ConnectionStrings:Discuss %>'
        DataKeyName="MessageId"
        DataParentKeyName="ParentId"
        SelectCommand="SELECT MessageId,ParentId,Subject FROM Discuss"
        Runat="server" />

    


    You selected message number:
    
        id="lblSelected"
        Runat="server" />

    
    </form>
</body>
</html>

Generating Ms Word document in ASP.NET and C#



 

Lets start with creating a simple .aspx page where we will give a text box to enter user's Name and a button to click on. After clicking on the button Ms Word document will be generated. 

.aspx page

The code will be 


   

        Write your name:

       

       

       

   

   


When button will be clicked, GenerateMsWordDoc event will fire. 

Now lets write the code behind code. What we are going to do here is we are generating a dynamic html content based on the textbox value. Also we are writing one table with 2 cell and at last we are adding some text. 

.cs file code



protected void GenerateMsWordDoc(object sender, EventArgs e)

    {

        string strBody = "" +

            "" +

                "
Your name is: " + txtName.Text + "
" +

                "
1st Cell body data2nd cell body data
" +

                "Ms Word document generated successfully." +

            "
" +

            "
";

        string fileName = "MsWordSample.doc";

        // You can add whatever you want to add as the HTML and it will be generated as Ms Word docs

        Response.AppendHeader("Content-Type", "application/msword");

        Response.AppendHeader ("Content-disposition", "attachment; filename="+ fileName);

        Response.Write(strBody);

    }