Monday, September 9, 2013

Calculate days month and year in sql server

DECLARE @date datetime, @tmpdate datetime, @years int, @months int, @days int
SELECT @date = '10/19/77'

SELECT @tmpdate = @date

SELECT @years = DATEDIFF(yy, @tmpdate, GETDATE()) - CASE WHEN (MONTH(@date) > MONTH(GETDATE())) OR (MONTH(@date) = MONTH(GETDATE()) AND DAY(@date) > DAY(GETDATE())) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(yy, @years, @tmpdate)
SELECT @months = DATEDIFF(m, @tmpdate, GETDATE()) - CASE WHEN DAY(@date) > DAY(GETDATE()) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(m, @months, @tmpdate)
SELECT @days = DATEDIFF(d, @tmpdate, GETDATE())

SELECT @years as year, @months as month, @days as days

Friday, May 24, 2013

merge two datatable and sum the data and bind into gridview

  MergeDT.Merge(ds.Tables[1]);
MergeDT.Merge(ds.Tables[2]);

 protected void CreateTotalTarget(DataTable MergeDT)
    {
        if (MergeDT != null)
        {
            if (MergeDT.Rows.Count > 0)
            {
                DataTable PrincipalDT = new DataTable();
                DataView dv = new DataView(MergeDT);
                DataTable FinalDT = MergeDT.Clone();
                PrincipalDT = dv.ToTable(true, "PrincipalName");
                for (int i = 0; i < PrincipalDT.Rows.Count; i++)
                {
                    object ISGQ1TgT;
                    ISGQ1TgT = MergeDT.Compute("SUM([ISG Q1 Trgt])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");
                    object ISGQ1Ach;
                    ISGQ1Ach = MergeDT.Compute("SUM([ISG Q1 Ach])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");

                    object ISGQ2TgT;
                    ISGQ2TgT = MergeDT.Compute("SUM([ISG Q2 Trgt])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");
                    object ISGQ2Ach;
                    ISGQ2Ach = MergeDT.Compute("SUM([ISG Q2 Ach])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");

                    object ISGQ3TgT;
                    ISGQ3TgT = MergeDT.Compute("SUM([ISG Q3 Trgt])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");
                    object ISGQ3Ach;
                    ISGQ3Ach = MergeDT.Compute("SUM([ISG Q3 Ach])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");

                    object ISGQ4TgT;
                    ISGQ4TgT = MergeDT.Compute("SUM([ISG Q4 Trgt])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");
                    object ISGQ4Ach;
                    ISGQ4Ach = MergeDT.Compute("SUM([ISG Q4 Ach])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");

                    object Annual_Trgt;
                    Annual_Trgt = MergeDT.Compute("SUM([Annual_Trgt])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");
                    object Annual_Ach;
                    Annual_Ach = MergeDT.Compute("SUM([Annual_Ach])", "PrincipalName='" + PrincipalDT.Rows[i]["PrincipalName"].ToString() + "'");

                    DataRow drow = null;
                    drow = FinalDT.NewRow();
                    drow["PrincipalName"] = PrincipalDT.Rows[i]["PrincipalName"].ToString();
                    drow["ISG Q1 Trgt"] = ISGQ1TgT;
                    drow["ISG Q1 Ach"] = ISGQ1Ach;
                    drow["ISG Q2 Trgt"] = ISGQ2TgT;
                    drow["ISG Q2 Ach"] = ISGQ2Ach;
                    drow["ISG Q3 Trgt"] = ISGQ3TgT;
                    drow["ISG Q3 Ach"] = ISGQ3Ach;
                    drow["ISG Q4 Trgt"] = ISGQ4TgT;
                    drow["ISG Q4 Ach"] = ISGQ4Ach;
                    drow["Annual_Trgt"] = Annual_Trgt;
                    drow["Annual_Ach"] = Annual_Ach;

                    FinalDT.Rows.InsertAt(drow, 1);
                  
                }
                DataView _dv = new DataView(FinalDT);
                _dv.Sort = "PrincipalName ASC";
                gvTotalTarget.DataSource = _dv;
                gvTotalTarget.DataBind();
            }
        }
   
    }

Monday, April 8, 2013

Disable right click control and many keys

function DisableRightClick(event)
       {
               if (event.button==2)
               {
               alert("Right Clicking not allowed!");
               }
       }


 function DisableCtrlKey(e)
           {
               var code = (document.all) ? event.keyCode:e.which;
               var message = "Ctrl key functionality is disabled!";
               if (parseInt(code)==17)
                       {
                       alert(message);
                       window.event.returnValue = false;
                       }
               if (parseInt(code)==18)
                       {
                       alert("Alt key functionality is disabled!");
                       window.event.returnValue = false;
                       }
               if (parseInt(code)==32)
                       {
                       alert("Space key is disabled");
                       window.event.returnValue = false;
                       }
               if (parseInt(code)==45)
                       {
                       alert("Insert key is disabled!");
                       window.event.returnValue = false;
                       }
           }

oncopy="return false;" onpaste="return false;" oncut="return false;" ondragstart="return false" onselectstart="return false"

Wednesday, April 3, 2013

trim in javascript

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function isBlank(s)
{
     if(s==null)
     return true;
     if(s.match(/^\s*$/))
     {
         return true
     }
     return false;
}

    if(isBlank(trim(objTourName.value,' ')) || trim(objTourName.value,' ').length ==0)
    {
        alert("Tour Name can not be left blank.");
        objTourName.focus();
        return false;
    }

Only numbers decimal not allowed

<script type="text/javascript">
        function checkIt(evt) {
            evt = (evt) ? evt : window.event
            var charCode = (evt.which) ? evt.which : evt.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                alert("Only Numbers allowed");
                return false
            }
            
            document.getElementById("errorMessage").innerHTML = ""
            return true
        }
        function Changed( textControl )
        {
           if(textControl.value.indexOf(".") !== -1)
           {
               alert("decimal values not allowed");textControl.value='';return false;
           }
           return true;
        }

</script>
<asp:TextBox ID="TextBox1" runat="server" onKeyPress="return checkIt(event)" onchange="javascript: Changed(this);" ></asp:TextBox>

Tuesday, February 26, 2013

If you want to check null value in aspx value while binding data


<%#Eval("BranchFax") != DBNull.Value ? "Fax:" : ""%>
<%#Eval("BranchFax") != DBNull.Value ? Eval("BranchFax") + ",&nbsp;" : ""%>

<asp:TextBox ID="TextBox1" Height="19" Width="30" runat="server" Text = '<%#Eval("CName") != null ? Eval("CName") : "Not available" %>'></asp:TextBox>

bind date in aspx file

<%# DataBinder.Eval(Container.DataItem,"MarDate","{0:dd-MMM-yyyy}")%>