Wednesday, November 20, 2013

Compare Date in SQL Query

Compare date in SQL:
AND DATEDIFF(d, @PeriodFrom, tblA.CreatedOn) >= 0 AND DATEDIFF(d, tblA.CreatedOn, @PeriodTo) >= 0

Wednesday, June 5, 2013

Restrict User to Not to Right Click on Page

Add this script inside form tag

< script language="javascript" type="text/javascript" >
document.onmousedown = noRightClick
function noRightClick() {
    if (event.button == 2) {
        alert("Right Click Not Allowed!");
        return false;
    }
}
document.onmousedown = noRightClick
< /script >

Thursday, February 28, 2013

Use XML table in SQL

Code Part Change

StringBuilder sbXml = new StringBuilder();
sbXml.Append("<MyDetails>");
foreach (DataGridItem item in this.dgUserLeave.Items)
{
sbXml.Append("<Details EmpId=\"" + ViewState["empid"].ToString() + "\" FromDt=\"" + item.Cells[1].Text.ToString() + "\"/>");
}
sbXml.Append("</MyDetails>");
return sbXml.ToString();


Access the same in SQL
/*
Proc_AddLeave '<MyDetails><Details EmpId="953" FromDt="05/02/2012"/><Details EmpId="953" FromDt="05/02/2012"/></LeaveDetails>'
*/
CREATE PROC Proc_MyProc (  @Detailss xml )
AS
BEGIN
--INSERT INTO #tmpTable
 SELECT
 x.value('@EmpId', 'int') EmpId,
 x.value('@FromDt', 'date') FromDt
 FROM @Detailss.nodes('LeaveDetails/Details') AS a(x)
END

Thursday, February 7, 2013

JIT Compiler

Just In Time Compiler is responsible for Converting IL code to native code.

•Pre-JIT COMPILER
Pre-JIT compiles complete source code into native code in a single compilation cycle. This is done at the time of deployment of the application.
 
•Econo-JIT COMPILER:
Econo-JIT compiles only those methods that are called at runtime. However, these compiled methods are removed when they are not required.
 
•Normal-JIT COMPILER:
Normal-JIT compiles only those methods that are called at runtime. These methods are compiled the first time they are called, and then they are stored in cache. When the same methods are called again, the compiled code from cache is used for execution.

Wednesday, February 6, 2013

Export to Excel(Concatenate the row in case of BR)

When export to excel if some columns are displayed as new row then in the exported file it show the details in new row. This export will concatenate both the column in same row.

Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendCookie(new HttpCookie("fileDownloadToken", exportToken.Value));
Response.AddHeader("content-disposition", "attachment;filename=ProjectResouces.xls");
string header = @" < html xmlns:x=""urn:schemas-microsoft-com:office:excel"" > 
< head >
< style >
< !--table
br {mso-data-placement:same-cell;}
tr {vertical-align:top;}
-- >
< /style >
< /head >
< body >";
Response.Write(header);

Response.Charset = "";
dgDetails.EnableViewState = false;
StringWriter sw = new StringWriter();
StringWriter headWrite = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dgDetails.RenderControl(htw);
headWrite.Write("< table >  < /table >");
headWrite.WriteLine(sw);
Response.Write(headWrite.ToString());
string footer = "< /body> </HTML >";
Response.Write(footer);

Response.Flush();
Response.End();

Tuesday, February 5, 2013

Using Pivot in SQL

Empid EmpName EmpDay empStatus
1 Bikash 1 P
2 Bikash 2 P
3 Bikash 3 P
4 Bikash 4 A
5 Bikash 5 A
6 Bikash 6 P
7 Bikash 8 P
8 Rama Rao 1 P
9 Rama Rao 2 P
10 Rama Rao 6 P
11 Rama Rao 3 P
12 Rama Rao 7 P

SELECT EMPNAME,[1],[2],[3],[4],[5],[6],[7],[8],[9] FROM (SELECT EMPNAME,EMPDAY,EMPSTATUS FROM EMPTEST) X PIVOT (MAX(EMPSTATUS) FOR EMPDAY IN([1],[2],[3],[4],[5],[6],[7],[8],[9])) Y


EMPNAME 1 2 3 4 5 6 7 8 9
Bikash P P P A A P NULL P NULL
Rama Rao P P P NULL NULL P P P NULL

DECLARE @cols AS NVARCHAR(MAX)
DECLARE@query AS NVARCHAR(MAX)
 
SET @cols = (SELECT DISTINCT ',['+ CAST(EMPDAY AS VARCHAR(10)) +']' FROM EMPTEST FOR XML PATH(''))
SELECT @cols= SUBSTRING(@COLS,2,LEN(@cols))
  SET @query = 'SELECT EMPNAME,'+@cols+' FROM
(SELECT EMPNAME,EMPDAY,EMPSTATUS FROM EMPTEST) AS X
PIVOT (MAX(EMPSTATUS) FOR EMPDAY IN('+@cols+')) P'

EXEC (@query)

Monday, October 29, 2012

Convert String in Title Case

string asTitleCase = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(txt1.Text.ToLower());

It will return "This Is Blog" from "this is blog"