Compare and validate string with another string using C# and VB.Net in ASP.Net

Last Reply one year ago By pandeyism

Posted one year ago

Hi All,

In my form i have one textbox Customer Name and database field name is Cust_Nm it allows nvarchar(max)

if the user enter data like below then it compare with Cust_Nm data and alert the message.

suppose, in database already one name saved like "aspforums.net" and once again the user try like "aspforums..net" then alert the message name alredy exist.

same like if the user saved "aspforums.net-1" and once again the user tried like "aspforums.net-2" then no need to alert it allows to save the details.

if Name like "aspforums  net" (here two spaces) and once again the user tried like "aspforums net"(here one space) then alert name alredy exist .

if the user saved "aspforums.net-1" and once again the user tried like "aspforums..net-1" then alert name alredy exist.

How to do it.

Thanks in advance

Posted one year ago

Hi nagaraju60,

Refer below sample.

HTML

Name :
<asp:TextBox runat="server" ID="txtName" />
<br />
<asp:Button Text="Submit" runat="server" OnClick="Submit" />

Namespaces

C#

using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient

Code

C#

protected void Submit(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        SqlCommand cmd = new SqlCommand("SELECT Cust_Nm FROM tblCheckData WHERE Cust_Nm = @Name", con);
        cmd.Parameters.AddWithValue("@Name", txtName.Text.Replace("..", ".").Replace("  ", " "));
        con.Open();
        string name = Convert.ToString(cmd.ExecuteScalar());
        if (string.IsNullOrEmpty(name))
        {
            cmd = new SqlCommand("INSERT INTO tblCheckData VALUES(@Name)", con);
            cmd.Parameters.AddWithValue("@Name", txtName.Text.Replace("..", ".").Replace("  ", " "));
            cmd.ExecuteNonQuery();
        }
        else
        {
            con.Close();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Name already exist');", true);
        }
    }
}

VB.Net

Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Dim cmd As SqlCommand = New SqlCommand("SELECT Cust_Nm FROM tblCheckData WHERE Cust_Nm = @Name", con)
        cmd.Parameters.AddWithValue("@Name", txtName.Text.Replace("..", ".").Replace("  ", " "))
        con.Open()
        Dim name As String = Convert.ToString(cmd.ExecuteScalar())

        If String.IsNullOrEmpty(name) Then
            cmd = New SqlCommand("INSERT INTO tblCheckData VALUES(@Name)", con)
            cmd.Parameters.AddWithValue("@Name", txtName.Text.Replace("..", ".").Replace("  ", " "))
            cmd.ExecuteNonQuery()
        Else
            con.Close()
            ScriptManager.RegisterStartupScript(Me, Me.GetType(), "Message", "alert('Name already exist');", True)
        End If
    End Using
End Sub

Screenshot