Monday, August 27, 2012

Javascript Form Validation

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type='text/javascript'>
        function formValidator() {
            // Make quick references to our fields
            var firstname = document.getElementById('firstname');
            var addr = document.getElementById('addr');
            var zip = document.getElementById('zip');
            var state = document.getElementById('state');
            var username = document.getElementById('username');
            var email = document.getElementById('email');
            // Check each input in the order that it appears in the form!
            if (isAlphabet(firstname, "Please enter only letters for your name")) {
                if (isAlphanumeric(addr, "Numbers and Letters Only for Address")) {
                    if (isNumeric(zip, "Please enter a valid zip code")) {
                        if (madeSelection(state, "Please Choose a State")) {
                            if (lengthRestriction(username, 6, 8)) {
                                if (emailValidator(email, "Please enter a valid email address")) {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
        function notEmpty(elem, helperMsg) {
            if (elem.value.length == 0) {
                alert(helperMsg);
                elem.focus(); // set the focus to this input
                return false;
            }
            return true;
        }
        function isNumeric(elem, helperMsg) {
            var numericExpression = /^[0-9]+$/;
            if (elem.value.match(numericExpression)) {
                return true;
            } else {
                alert(helperMsg);
                elem.focus();
                return false;
            }
        }
        function isAlphabet(elem, helperMsg) {
            var alphaExp = /^[a-zA-Z]+$/;
            if (elem.value.match(alphaExp)) {
                return true;
            } else {
                alert(helperMsg);
                elem.focus();
                return false;
            }
        }
        function isAlphanumeric(elem, helperMsg) {
            var alphaExp = /^[0-9a-zA-Z]+$/;
            if (elem.value.match(alphaExp)) {
                return true;
            } else {
                alert(helperMsg);
                elem.focus();
                return false;
            }
        }
        function lengthRestriction(elem, min, max) {
            var uInput = elem.value;
            if (uInput.length >= min && uInput.length <= max) {
                return true;
            } else {
                alert("Please enter between " + min + " and " + max + " characters");
                elem.focus();
                return false;
            }
        }
        function madeSelection(elem, helperMsg) {
            if (elem.value == "Please Choose") {
                alert(helperMsg);
                elem.focus();
                return false;
            } else {
                return true;
            }
        }
        function emailValidator(elem, helperMsg) {
            var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
            if (elem.value.match(emailExp)) {
                return true;
            } else {
                alert(helperMsg);
                elem.focus();
                return false;
            }
        }
    </script>
</head>
<body onsubmit='return formValidator()'>
    <form id="form1" runat="server">
    <table border="0">
        <tr>
            <td>
                First Name:
            </td>
            <td>
                <input type='text' id='firstname' />
            </td>
        </tr>
        <tr>
            <td>
                Address:
            </td>
            <td>
                <input type='text' id='addr' />
            </td>
        </tr>
        <tr>
            <td>
                Zip Code:
            </td>
            <td>
                
                <input type='text' id='zip' />
            </td>
        </tr>
        <tr>
            <td>
                State:
            </td>
            <td>
                <select id='state'>
                    <option>Please Choose</option>
                    <option>AL</option>
                    <option>CA</option>
                    <option>TX</option>
                    <option>WI</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                Username(6-8 characters):
            </td>
            <td>
                <input type='text' id='username' />
            </td>
        </tr>
        <tr>
            <td>
                Email:
            </td>
            <td>
                <input type='text' id='email' />
            </td>
        </tr>
        <tr>
            <td>
                <input type='submit' value='Check Form' />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>



Output:

Screenshot_1

Monday, August 13, 2012

SQL Write CASE STATEMENT IN WHERE CONDITION

Write CASE STATEMENT IN WHERE CONDITION
--------------------------------------------
create table Billings (
     BankerID           INTEGER,
     BillingNumber      INTEGER,
     BillingDate        datetime,
     BillingTotal       INTEGER,
    TermsID            INTEGER,
     BillingDueDate     datetime ,
    PaymentTotal       INTEGER,
     CreditTotal        INTEGER
 );
 GO
 
 INSERT INTO Billings VALUES (1, 1, '2005-01-22', 165, 1,'2005-04-22',123,321);
 INSERT INTO Billings VALUES (2, 2, '2001-02-21', 165, 1,'2002-02-22',123,321);
 INSERT INTO Billings VALUES (3, 3, '2003-05-02', 165, 1,'2005-04-12',123,321);
 INSERT INTO Billings VALUES (4, 4, '1999-03-12', 165, 1,'2005-04-18',123,321);
 INSERT INTO Billings VALUES (5, 5, '2000-04-23', 165, 1,'2005-04-17',123,321);
 INSERT INTO Billings VALUES (6, 6, '2001-06-14', 165, 1,'2005-04-18',123,321);
 INSERT INTO Billings VALUES (7, 7, '2002-07-15', 165, 1,'2005-04-19',123,321);
 INSERT INTO Billings VALUES (8, 8, '2003-08-16', 165, 1,'2005-04-20',123,321);
 INSERT INTO Billings VALUES (9, 9, '2004-09-17', 165, 1,'2005-04-21',123,321);
 INSERT INTO Billings VALUES (0, 0, '2005-10-18', 165, 1,'2005-04-22',123,321);
 
DECLARE @BANKERID varchar(100)
Set @BANKERID = 'test'
SELECT BankerID,BillingNumber, BankerID * (SELECT COUNT(BankerID) FROM Billings) 
FROM Billings
WHERE     
BANKERID = CASE(@BANKERID)
  WHEN 'test' THEN 1
 -- WHEN 2 THEN 2
 END 
AND BillingNumber = 2
------------------------------------------------------------------------------------------------------
DECLARE @BANKERID varchar(100)
Set @BANKERID = 'test'
SELECT BankerID,BillingNumber, BankerID * (SELECT COUNT(BankerID) FROM Billings) 
FROM Billings
WHERE    
 BillingNumber = 2 AND
 
 @BANKERID =
CASE @BANKERID
  WHEN 'test' THEN 'test'
 -- WHEN 2 THEN 2
 END 
 
 
 --------------------------------------------------------------------------------------------------------
 
 DECLARE @BANKERID varchar(100)
Set @BANKERID = 'test'
SELECT @BANKERID,BankerID,BillingNumber, BankerID * (SELECT COUNT(BankerID) FROM Billings) 
FROM Billings
WHERE    
 BillingNumber = 2 AND
 
 @BANKERID =
CASE @BANKERID
  WHEN 'test' THEN 'test'
 -- WHEN 2 THEN 2
 END 

Export to CSV File

Code Snippet
  1. <%@ Page Language="C#" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  4.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head>
  7.     <title>Export To CSV</title>
  8. <script runat="server">
  9.     public static void ExportCSV(System.Data.DataTable data, string fileName)
  10.     {
  11.         try
  12.         {
  13.             HttpContext context = HttpContext.Current;
  14.  
  15.             context.Response.Clear();
  16.             context.Response.ContentType = "text/csv";
  17.             context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
  18.  
  19.             //rite column header names
  20.             for (int i = 0; i < data.Columns.Count; i++)
  21.             {
  22.                 if (i > 0)
  23.                 {
  24.                     context.Response.Write(",");
  25.                 }
  26.                 context.Response.Write(data.Columns[i].ColumnName);
  27.             }
  28.             context.Response.Write(Environment.NewLine);
  29.             //Write data
  30.             foreach (System.Data.DataRow row in data.Rows)
  31.             {
  32.                 for (int i = 0; i < data.Columns.Count; i++)
  33.                 {
  34.                     if (i > 0)
  35.                     {
  36.                         context.Response.Write(",");
  37.                     }
  38.                     context.Response.Write(row[i].ToString());
  39.                 }
  40.                 context.Response.Write(Environment.NewLine);
  41.             }
  42.             context.Response.End();
  43.         }
  44.         catch (Exception ex)
  45.         {
  46.  
  47.         }
  48.  
  49.     }
  50.     
  51.     
  52.      private System.Data.DataTable CreateDataTable()
  53.     {
  54.         System.Data.DataTable dataTable = new System.Data.DataTable("Employee");
  55.  
  56.         dataTable.Columns.Add(new System.Data.DataColumn("ID", typeof(int)));
  57.         dataTable.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
  58.         dataTable.Columns.Add(new System.Data.DataColumn("Address", typeof(string)));
  59.  
  60.         System.Data.DataRow dataRow;
  61.  
  62.         dataRow = dataTable.NewRow();
  63.         dataRow["ID"] = "1";
  64.         dataRow["Name"] = "sandeep";
  65.         dataRow["Address"] = "Ncode";
  66.         dataTable.Rows.Add(dataRow);
  67.  
  68.         return dataTable;
  69.     }
  70.  
  71.      protected void btnExportDataTableToCSV_Click(object sender, EventArgs e)
  72.      {
  73.          ExportCSV(CreateDataTable(), "FileName");
  74.      }
  75.  
  76. </script>
  77.  
  78. </head>
  79. <body>
  80.  
  81.     <h3>FileUpload.SaveAs Method Example</h3>
  82.  
  83.     <form id="Form1" runat="server">
  84.  
  85.        <asp:Button ID="btnExportDataTableToCSV" runat="server" OnClick="btnExportDataTableToCSV_Click" Text="Export DataTable to CSV" />
  86.  
  87.     </form>
  88.  
  89. </body>
  90. </html>

Ajax Editor Validation

Code Snippet
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
  2.  
  3. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit.HTMLEditor"
  4.     TagPrefix="cc1" %>
  5.  
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7.  
  8. <html xmlns="http://www.w3.org/1999/xhtml">
  9. <head id="Head1" runat="server">
  10.     <title></title>
  11.  
  12.  
  13.     <script type="text/javascript">
  14.         function Validation() {
  15.             var text = document.getElementById('txt1').value;
  16.             if (text == "") {
  17.                 alert('Name Cannot be empty');
  18.                 document.getElementById('txt1').focus();
  19.                 return false;
  20.             }
  21.  
  22.             test()
  23.         }
  24.  
  25.         function test() {
  26.             var a = $find("Editor1");
  27.             var value = a.get_content();
  28.             if (value == "") {
  29.                 $get("result").innerHTML = "Editor's content is empty";
  30.                 alert('Editor Cannot be empty');
  31.                
  32.                 return false;
  33.             }
  34.         }
  35.  
  36.  
  37.     </script>
  38.  
  39.  
  40. </head>
  41. <body>
  42.     <form id="form1" runat="server">
  43.     <div>
  44.         <asp:ScriptManager ID="ScriptManager1" runat="server" />
  45.  
  46.         Name:<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
  47.         <br />
  48.  
  49.         <cc1:Editor ID="Editor1" runat="server" Width="300" Height="300" ValidationGroup="Editor" />
  50.         <br />
  51.         <span id="result"></span>
  52.         <br />
  53.         <asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="return Validation();"
  54.             CausesValidation="true" ValidationGroup="Editor" />
  55.     </div>
  56.     </form>
  57. </body>
  58. </html>

Ajax Editor Validation

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit.HTMLEditor"
TagPrefix="cc1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>


<script type="text/javascript">
function Validation() {
var text = document.getElementById('txt1').value;
if (text == "") {
alert('Name Cannot be empty');
document.getElementById('txt1').focus();
return false;
}

test()
}

function test() {
var a = $find("Editor1");
var value = a.get_content();
if (value == "") {
$get("result").innerHTML = "Editor's content is empty";
alert('Editor Cannot be empty');

return false;
}
}


</script>


</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />

Name:<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<br />

<cc1:Editor ID="Editor1" runat="server" Width="300" Height="300" ValidationGroup="Editor" />
<br />
<span id="result"></span>
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="return Validation();"
CausesValidation="true" ValidationGroup="Editor" />
</div>
</form>
</body>
</html>