.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Verify Availability of name in database

Posted By:satyapriyanayak       Posted Date: March 11, 2013    Points: 40    Category: ADO.NET    URL: http://www.dotnetspark.com  

Verify Availability of name in database
 

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


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend