<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.toString() &#187; Web Service</title>
	<atom:link href="http://www.dottostring.com/category/web-service/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dottostring.com</link>
	<description>Programming is our passion</description>
	<lastBuildDate>Sun, 04 Oct 2009 15:57:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JQuery Communicating with Web Service to check for duplicate emails</title>
		<link>http://www.dottostring.com/2009/04/jquery-communicating-with-web-service-to-check-for-duplicate-emails/</link>
		<comments>http://www.dottostring.com/2009/04/jquery-communicating-with-web-service-to-check-for-duplicate-emails/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 15:36:34 +0000</pubDate>
		<dc:creator>ahsan</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[Web Service]]></category>
		<category><![CDATA[WebService]]></category>

		<guid isPermaLink="false">http://www.dottostring.com/?p=331</guid>
		<description><![CDATA[Few days ago I had a task that on user registration I had to check the email address of the user. When the user enters the email address the application should check the database that if the email exists in the database user will not be able to add it again and the whole task [...]]]></description>
			<content:encoded><![CDATA[<p>Few days ago I had a task that on user registration I had to check the email address of the user. When the user enters the email address the application should check the database that if the email exists in the database user will not be able to add it again and the whole task should be done in a way that user enters the email in a textbox and then starts filling the other form and system in the meanwhile automatically checks the existence of email in the database. I used <strong>Jquery</strong> for this task, the selection of jquery can be debatable but its not the issue at hand. I made a web service which communicates with my database. In the web service I have a method  named CheckEmail which checks the existence of email in the particular table. The web service looks like this:</p>
<pre name="code" class="vb">Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services
Imports System.Data.OleDb

&lt;WebService(Namespace:="http://tempuri.org/")&gt; _
&lt;WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)&gt; _
&lt;ScriptService()&gt; _
&lt;Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()&gt; _
Public Class Service
Inherits System.Web.Services.WebService

&lt;WebMethod()&gt; _
Public Function CheckEmail(ByVal email As String) As String
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionStringManual").ToString()
Dim connection As OleDbConnection = New OleDbConnection(connectionString)
connection.Open()
Dim sqlString As String = "Select count(*) from Customers where CustomerEmail =?"
Dim command As OleDbCommand = New OleDbCommand(sqlString, connection)
command.Parameters.AddWithValue("CustomerEmail", email)
Return command.ExecuteScalar()

End Function
End Class</pre>
<p>This web service returns an integer based on the count of rows after running the query.</p>
<p>I have an aspx page which looks like the code below:</p>
<pre name="code" class="html">&lt;body&gt;
&lt;form id="form1" runat="server"&gt;
&lt;div&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;
Email:
&lt;/td&gt;
&lt;td&gt;
&lt;asp:TextBox ID="txtEmail" runat="server" &gt;&lt;/asp:TextBox&gt;&lt;span id="spErrorMessage"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
Password:
&lt;/td&gt;
&lt;td&gt;
&lt;asp:TextBox ID="txtPassword" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;br /&gt;
&lt;/td&gt;
&lt;/tr&gt;

&lt;/table&gt;
&lt;/div&gt;

&lt;/form&gt;
&lt;/body&gt;</pre>
<p>And finally the last but the most important section of  Jquery in the head section of the page.</p>
<pre name="code" class="js">&lt;head runat="server"&gt;
&lt;script src="jquery-1.3.2.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;

$(function()
{
$("#&lt;%= txtEmail.ClientID %&gt;").change(DuplicateEmailCheck);
}
);

function DuplicateEmailCheck()
{
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{email: '" + $('#&lt;%= txtEmail.ClientID %&gt;').val() + "'}",
dataType: "json",
url: "http://localhost:1561/TestWebService/Service.asmx/CheckEmail",
success: function(response)
{
$("#spErrorMessage").empty();
if (response.d != "0")
{
$("#spErrorMessage").html('Email address already exists.');
}
}
});

}
&lt;/script&gt;</pre>
<p>The first script section contains the reference to the latest jquery library till date. You can download it from internet very easily. The second script section does the real magic but its very easy. The first function just hooks the txtEmail textbox with the DuplicateEmailCheck function. The DuplicateEmailCheck function makes an ajax call to the web service which we created above. Now based on the result the span belonging to the textbox will be populated if email already exists.</p>
<p>Thats all folks, don&#8217;t forget to leave feedback/suggestions.</p>
<p>Cheers.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.dottostring.com%2F2009%2F04%2Fjquery-communicating-with-web-service-to-check-for-duplicate-emails%2F';
  addthis_title  = 'JQuery+Communicating+with+Web+Service+to+check+for+duplicate+emails';
  addthis_pub    = 'erfaan';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.dottostring.com/2009/04/jquery-communicating-with-web-service-to-check-for-duplicate-emails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
