ARTICLE

Verify Availability of name in database in VB.NET

Posted by Satyapriya Nayak Articles | ASP.NET using VB.NET August 11, 2011
Here, we will see how to Verify Availability of name in database.
Reader Level:

While doing registration how we will know that a particular name is available to the user.

Program

Default.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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 runat="server">

    <title>Untitled Page</title>

</head>

<body>

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

    <div>   

    </div>

    <asp:Button ID="Button1" runat="server" Text="Availability of name from the database"

        BackColor="#FF99FF" Font-Bold="True" Width="329px" /><br />

    <asp:Label ID="Labelcheck"  Text="Please enter any name to be verified from the database" runat="server" BackColor="#FFFF99"

        Width="197px" ForeColor="#FF3300"></asp:Label>

        <asp:TextBox ID="txtUserName" runat="server" Width="197px"></asp:TextBox>  

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"

        ControlToValidate="txtUserName" ErrorMessage="*Name Required"></asp:RequiredFieldValidator>   

    <br />

    <asp:Label ID="lblMessage" runat="server" BackColor="#FF3300"

        ForeColor="Black"></asp:Label>

    </form>

</body>

</html>

 Default.aspx.vb code

Imports System.Data.SqlClient

Imports System.Data

Partial Class _Default

    Inherits System.Web.UI.Page

    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()

    Dim sqlda As SqlDataAdapter

    Dim com As SqlCommand

    Dim ds As DataSet

    Dim str As String

    Dim count As Integer

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

        namecheck()

    End Sub

    Sub namecheck()

        Dim con As New SqlConnection(strConnString)

        con.Open()

        str = "select count(*)from employee where ename='" & txtUserName.Text & "'"

        com = New SqlCommand(str, con)

        count = Convert.ToInt32(com.ExecuteScalar)

        If count > 0 Then

            lblMessage.Text = "Sorry! you can't take this username"

        Else

            lblMessage.Text = "You can take this username"

        End If

    End Sub

End Class

Output

name.gif

Thanks for reading

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.
    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.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor