ARTICLE

How to check email address exists or not in ASP.NET using VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET August 03, 2011
Here, we check email address exists or not in database.
Download Files:
 
Reader Level:

Here, we will see How to check email address exists or not in SQL server database. To do that we create a table in SQL server database and enter some email address in the table.

Creating Table in SQL server Database

Now create a table in a SQL Server database with username, password and email fields. The table looks like this.

create table emailcheck

(

Username varchar(100),

Email varchar(100),

)

go

insert into emailcheck values('Ram','ram@gmail.com');

go

insert into emailcheck values('Rahul','rahul@gmail.com');

go

insert into emailcheck values('Rajesh','rajesh@gmail.com');

go

select * from emailcheck ;

OUTPUT

Check1.gif

Now creating a Stored procedure

Now create a stored procedure for checking the existence of email. The stored procedure looks like:

create PROCEDURE Emailexists

(

@email as varchar(50)

)

AS

SELECT * FROM emailcheck WHERE email= @email

 

Create form to check email in ASP. NET

 

Now create a form in ASP. Net with the name and email field defined in the table. The form looks like the below figure.


Check2.gif

 

Figure1

 

.aspx code

 

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="emailcheck.WebForm1" %>

<!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 runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

     Name:

        <asp:TextBox ID="txtname" runat="server"></asp:TextBox>

        <br />

        <br />

        Email:

        <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>

        <asp:Label ID="Labelinfo" runat="server" Text="Label"></asp:Label>

        <br />

        <br />

        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"

            Text="Checkemail" />   

    </div>

    </form>

</body>

</html>

 

Now double click on the Button control and add the following code.

 

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        Dim strcon As String = "Data Source=.;uid=sa;pwd=Password$2;database=userinfo"

        Dim con As New SqlConnection(strcon)

        Dim com As New SqlCommand("Emailexists", con)

        com.CommandType = CommandType.StoredProcedure

        Dim p1 As New SqlParameter("email", txtemail.Text)

        com.Parameters.Add(p1)

        con.Open()

        Dim rd As SqlDataReader = com.ExecuteReader()

        If rd.HasRows Then

            rd.Read()

            Me.Labelinfo.ForeColor = System.Drawing.Color.Red

            Me.Labelinfo.Text = "Your email address already Exist!"

        Else

            Me.Labelinfo.Text = "Your email address does not exists"

            Me.txtname.Text = ""

 

            Me.txtemail.Text = ""

        End If

    End Sub

 

Now run the application and enter the username and email address to check for the existence of the email in the database. Suppose we enter a new email address.

Check3.gif

Figure2

Then click on the Checkemail Button.

Check4.gif

Figure3

 

Now enter an existing email address which is stored in database table and click on the Checkemail Button.


Check5.gif


Figure4

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor