Wednesday, 4 June 2014

Insert Data Into SQL Server Using jQuery in ASP.Net


Creating SQL Database Table
This database contains one table named test.
CREATE TABLE [dbo].[TestTable](
      [Name] [varchar](50) NULL,
      [Email] [varchar](100) NULL
)
Now press F5 to execute the script above that looks like this:
TestTable-in-SQLServer.jpg
.ASPX Page
Right-click your ASP.NET project then select "Add New Item" -> "New Page" and give it the name "Default.aspx" and add the following control into it:
Insert-Data-into-SQL-Server-using-JQuery-in-ASP.NET.jpg
Now click on the source tab and add the following code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>AutoComplete Box with jQuery</title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css"
        rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
       <script type="text/javascript">
        $(document).ready(function () {
            $('#Button1').click(function () {
                $.ajax({
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    url: 'Default.aspx/InsertMethod',
                    data: "{'Name':'" + document.getElementById('txtUserName').value + "', 'Email':'" + document.getElementById('txtEmail').value + "'}",
                    async: false,
                    success: function (response) {                       
           $('#txtUserName').val('');
           $('#txtEmail').val('');
                        alert("Record Has been Saved in Database");
                    },
                    error: function ()
                    { console.log('there is some error'); }
                });
            });
        });      
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="demo">
        <div class="ui-widget">
            <label for="tbAuto">
                Enter UserName:
            </label>
&nbsp;<asp:TextBox ID="txtUserName" runat="server" ClientIDMode="Static" Width="202px"></asp:TextBox>
            <br />
            <br />
            Email: <asp:TextBox ID="txtEmail" runat="server" ClientIDMode="Static" Width="210px"></asp:TextBox>
            <br />
            <br />           
            <asp:Button ID="Button1" runat="server" Text="Button" ClientIDMode="Static" />
        </div>
    </div>
    </form>
</body>
</html>

 Now double-click on the default form and add the following .cs file code:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {     
    }
      
    [WebMethod]
    public static string InsertMethod(string Name, string Email) 
    {       
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TestDB;User ID=sa;Password=Micr0s0ft");
        {
            SqlCommand cmd = new SqlCommand("Insert into TestTable values('" + Name + "', '" + Email + "')", con);
            {
                con.Open();
                cmd.ExecuteNonQuery();
                return "True";
            }
        }
    }
}

Now run the application.

Insert-Data-into-SQL-Server-using-JQuery-in-ASP.NET-1.jpg
Now enter the name and email into the corresponding TextBox.

Insert-Data-into-SQL-Server-using-JQuery-in-ASP.NET-2.jpg
Now click on the Button control.

Insert-Data-into-SQL-Server-using-JQuery-in-ASP.NET-3.jpg

Now open the SQL Server database and check it.

Insert-Data-into-SQL-Server-using-JQuery-in-ASP.NET-4.jpg

Tuesday, 3 June 2014

Creating Captcha Code with JavaScript

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"%>
<!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 language="javascript">
         var a = 49, b = 65;
         var c = 100;
         var d = 70;
         function show() {
             if (a == 57) {
                 a = 49;
             }
             var main = document.getElementById('txt1');
             var a1 = String.fromCharCode(a);
             var b1 = String.fromCharCode(b);
             var c1 = String.fromCharCode(c);
             var d1 = String.fromCharCode(d);
             main.value = a1 + b1 + c1 + d1;
             a = a + 1;
             b = b + 1;
             c = c + 1;
             d = d + 1;
         }
  </script>
    <style type="text/css">
        #form1
        {
            height: 95px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="text" id="txt1" runat="server"
            style="border-style: none; border-color: inherit; border-width: medium; background-color:black; color:red; font-family: 'Curlz MT'; font-size: x-large; font-weight: bold; font-variant: normal; letter-spacing: 10pt; width: 120px; background-image: url('glitter_background_b4.gif');"
            value="5AbD" />
  <input type="button" onclick="show()" value="Change"  />
        </div>
    <asp:TextBox ID="txtverification" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" Text="Verification"
        onclick="Button1_Click" />
    &nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Label ID="lblmsg" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
    </form>
</body>
</html>

Step 2:

In the (.cs) page I write the following code for verification:

protected void Button1_Click(object sender, EventArgs e)
{
    if (txtverification.Text == txt1.Value)
    {
        lblmsg.Text = "Successful";
    }
    else
    {
        lblmsg.Text = "Failure";
    }
}

How to create Captcha Image in Asp.net with javascript

<html>
<head>
<title>Captcha Validation</title>

<STYLE>
// CSS is use for just formatting
.input_img 
{
 background-image: url("captcha.png"); 
text-align: center;
 border: medium none;
  font-weight: bold;
  font-family: Modern;
  height: 80px;
 width: 150px;
 font-size: 14px;
}

.align
{
 margin: 0 auto;
 width: 250px;
 
 }

</STYLE>
    <script type="text/javascript">  
    function DrawCaptcha()
    {
        var a = Math.ceil(Math.random() * 10)+ '';
        var b = Math.ceil(Math.random() * 10)+ '';       
        var c = Math.ceil(Math.random() * 10)+ '';  
        var d = Math.ceil(Math.random() * 10)+ '';  
        var e = Math.ceil(Math.random() * 10)+ '';  
        var f = Math.ceil(Math.random() * 10)+ '';  
        var g = Math.ceil(Math.random() * 10)+ '';  
        var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' '+ f + ' ' + g;
        document.getElementById("txtCaptcha").value = code
    }
    function ValidCaptcha(){
        var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
        var str2 = removeSpaces(document.getElementById('txtInput').value);
        if (str1 == str2) return true;        
        return false;
  }

    function removeSpaces(string)
    {
        return string.split(' ').join('');
    }
  </script>
    
    
    
</head>
<body onload="DrawCaptcha();">
<div class="align">
        Welcome To Captcha<br />
      <input id="txtCaptcha" type="text" readonly="" class="input_img">
      <input type="button" id="btnrefresh" value="Refresh" onclick="DrawCaptcha();" /><br>
  <input type="text" id="txtInput"/>    
  <input id="Button1" type="button" value="Check" onclick="alert(ValidCaptcha());"/>
      </div>
</body>
</html>

The result will shows something like this

image
- See more at: http://codingresolved.com/discussion/540/how-to-make-captcha-in-javascript/p1#sthash.vkreVXdE.dpuf