Existance Of Email Address In ASP.NET using VB.NET

Here, we check email address exists or not in database.
  • 4338
 

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','[email protected]');

go

insert into emailcheck values('Rahul','[email protected]');

go

insert into emailcheck values('Rajesh','[email protected]');

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 ObjectByVal e As EventArgsHandles 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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.