Thursday, 21 June 2012

Return a comma seperated string in sql ?

create function funcationCommaSeparete(@myID int)
returns varchar(8000) as
begin
declare ps cursor for select columnName from TableName where columnName = @myID
declare @comma varchar(1),@res varchar(8000), @colvalue varchar(100)
set @res=''
set @comma=''
open ps
fetch next from ps into @colvalue
while @@fetch_status=0 begin
  set @res=@res+@comma+@colvalue
  set @comma=','
  fetch next from ps into @colvalue
end
close ps
deallocate ps
return @res
end

------------------- Using
select dbofuncationCommaSeparete(1)

Monday, 18 June 2012

Selecting distinct value from DataTable in C#

DataTable dtMain = new DataTable();

dtMain.Columns.Add("ID");
dtMain.Columns.Add("Name");
dtMain.Rows.Add(1, "AA");
dtMain.Rows.Add(1, "AA");
dtMain.Rows.Add(2, "CC");
dtMain.Rows.Add(3, "DD");
dtMain.Rows.Add(4, "EE");
dtMain.Rows.Add(5, "BB");
dtMain.Rows.Add(6, "AA");
dtMain.Rows.Add(5, "BB");
dtMain.Rows.Add(7, "FF");
dtMain.Rows.Add(1, "AA");

DataTable dtDistinct = dtMain.DefaultView.ToTable(true, "ID", "Name");

Wednesday, 13 June 2012

Disable cut, copy & paste using Javascript and Jquery

Javascript  
<script language="javascript" type="text/javascript">
function DisableCopyPaste (e) 
{
 // Message to display
 var message = "Cntrl key/ Right Click Option disabled";
 // check mouse right click or Ctrl key press
 if (e.keyCode == 17 || e.button == 2)
 {
 alert(message);
 return false;
 }
}
</script>
<asp:TextBox ID="TextBox1" runat="server" onKeyDown="return 
  DisableCopyPaste(event)"onMouseDown="return DisableCopyPaste
 (event)"></asp:TextBox> 
 
JQuery 
<script type="text/javascript">
$(document).ready(function() {
 $('#TextBox1').bind('copy paste cut',function(e) { 
 e.preventDefault(); //disable cut,copy,paste
 alert('cut,copy & paste options are disabled !!');
 });
});
</script>
 <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>  

Disable cut, copy & paste in textbox in asp.net without scripting

<asp:TextBox ID="TextBox1" runat="server" oncopy="return false" 
onpaste="return false" oncut="return false"></asp:TextBox> 

Wednesday, 6 June 2012

Date compare in javascript ?

var returnDate = "";
        function convertMonthText(DTime) {
            var monthtext = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
            var DateFormat = DTime.getDate();           
            if (DateFormat < 10) {
                returnDate = "0" + DTime.getDate() + "/" + monthtext[DTime.getMonth()] + "/" + DTime.getYear();
            }
            else {
                returnDate = DTime.getDate() + "/" + monthtext[DTime.getMonth()] + "/" + DTime.getYear();
            }           
        }

        function CompareDateToToday() {
            var dateChoosed = document.getElementById('TextBox1').value;
            var someDate = new Date();
            var numberOfDaysToAdd = 7;
            someDate.setDate(someDate.getDate() + numberOfDaysToAdd);

            convertMonthText(someDate);
            if (dateChoosed < returnDate) {
                alert('The date for responses should be at least a week after start date.')
                document.getElementById('TextBox1').value = "";
            }
        }

//---------- Support all browse below code

function convertMonthTextToNumeric(DTime) {
            var ReturnDateFormatedValue = "";
            var _Array = DTime.split('/');
            if (_Array.length > 2) {
                var _Month = "";
                if (_Array[1] == "Jan")
                    _Month = "01";
                else if (_Array[1] == "Feb")
                    _Month = "02";
                else if (_Array[1] == "Mar")
                    _Month = "03";
                else if (_Array[1] == "Apr")
                    _Month = "04";
                else if (_Array[1] == "May")
                    _Month = "05";
                else if (_Array[1] == "Jun")
                    _Month = "06";
                else if (_Array[1] == "Jul")
                    _Month = "07";
                else if (_Array[1] == "Aug")
                    _Month = "08";
                else if (_Array[1] == "Sep")
                    _Month = "09";
                else if (_Array[1] == "Oct")
                    _Month = "10";
                else if (_Array[1] == "Nov")
                    _Month = "11";
                else if (_Array[1] == "Dec")
                    _Month = "12";
                else
                    _Month = "01";

                ReturnDateFormatedValue = _Array[2] + "/" + _Month + "/" + _Array[0];
            }
            return ReturnDateFormatedValue;
        }



        function CheckDateValidation() {
            var getStartDateFormated = "";
            var getEndtDateFormated = "";
            getStartDateFormated = convertMonthTextToNumeric(document.getElementById('txtStartDate').value);
            getEndtDateFormated = convertMonthTextToNumeric(document.getElementById('txtEndDate').value);

            getStartDateFormated = new Date(getStartDateFormated);
            getEndtDateFormated = new Date(getEndtDateFormated);

            var numberOfDaysToAdd = 7;
            getStartDateFormated.setDate(getStartDateFormated.getDate() + numberOfDaysToAdd);

            if (getEndtDateFormated < getStartDateFormated) {
                alert('The date for responses should be at least a week after start date.')
                document.getElementById('txtEndDate').value = "";
            }
        }


Thursday, 24 May 2012

Get all store procedure that references with perticular tables

SELECT DISTINCT o.name, o.xtype FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tblname%'

Thursday, 10 May 2012

Handle Sessions when browser close at client side in .NET

Every programmer has the Question that how to handle sessions or kill the sessions when browser close at client end? and how the server knows when client close the browser?I have also searched on this a lot... and find the solution... it may helpful to someone.
This can be achieved through the ajax.
we have a onunload event in the javascript,it will be fired when client close the connection.we need to add the ScriptManager control of ajax in .aspx page.. and set the EnablPageMethods to true.(by setting the property we can call the web methods in server side code).
 sample.aspx page
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Handle Sessions when close the browser</title>
</head>
<script type="text/javascript" language="javascript">
    function BrowserClose(){       
           PageMethods.BrowserCloseMethod();
    }

    </script>

<body onunload="BrowserClose()">
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    Testing
    </div>
    </form>
</body>
</html>
 in the Code behind, we should put this code
    [System.Web.Services.WebMethod]
    public static void BrowserCloseMethod()
    {
        HttpContext.Current.Session.Abandon();
    }