Friday, March 21, 2014

Convert Datatable to html table

1. pass datatable as parameter to function below it generates html table

public static string ConvertDataTableToHTML(DataTable dt)
    {
        string html = "<table>";
        //add header row
        html += "<tr>";
        for (int i = 1; i < dt.Columns.Count; i++)
            html += "<td>"
                + dt.Columns[i].ColumnName + "</td>";
        html += "</tr>";
        //add rows
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            html += "<tr>";
            for (int j = 1; j < dt.Columns.Count; j++)
            {
                    html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
            }
            html += "</tr>";
        }
        html += "</table>";
        return html;

    }

Thursday, March 20, 2014

Get server date in Indian Standard Time

1. Get date according to timezone

private static TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");

DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);

Get date for particular time zone by changing the timezone id

Split String in Sql Server

1. Create a function which return split values in a tabular form

CREATE FUNCTION [dbo].[fnSplitString]
(
    @string NVARCHAR(MAX),
    @delimiter CHAR(1)
)
RETURNS @output TABLE(id int identity(1,1), splitdata NVARCHAR(MAX)
)
BEGIN
    DECLARE @start INT, @end INT
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
    WHILE @start < LEN(@string) + 1 BEGIN
        IF @end =
            SET @end = LEN(@string) + 1
      
        INSERT INTO @output (splitdata) 
        VALUES(SUBSTRING(@string, @start, @end - @start))
        SET @start = @end + 1
        SET @end = CHARINDEX(@delimiter, @string, @start)
       
    END
    RETURN
END
2. Call function and get result in tabular form

select splitdata from fnSplitString(Dotnet,Programming,',')



This will give result

1 Dotnet
2 Programming

Wednesday, March 5, 2014

Text input validation

Text input validation with jquery
 <script type="text/javascript">
  $(function () {
            $('input[type=text]').on('keyup', function (e) {
                if (/[^.a-z_ 0-9- A-Z]/.test(this.value)) {
                    this.value = this.value.replace(/[^.a-z_ 0-9- A-Z]/g, '');
                }
            });
        });   
</script>

this will allow only A to Z ,a to z , 0-9 and some character ,-_. also