Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Frnds,

I want to validate records, if it already exists show message Record already exists.
If it is new record, it must insert.

In this i have 3 fields as Username,EmployeeNumber, Email.

Existing username must not be inserted once again.
Existing must not be inserted once again.
Existing Email must not be inserted once again.

so it should validate, when add button clicks in gridview footer.

--------------------------------

So, this is my code....

C#
protected void GVRegistrations_RowCommand(object sender, GridViewCommandEventArgs e)
 {
if (e.CommandName.Equals("AddNew Registrations"))
        {
            TextBox TxtUserName= (TextBox)GVRegistrations.FooterRow.FindControl("TxtFOOTERUserName");
            TextBox EmployeeNumber= (TextBox)GVRegistrations.FooterRow.FindControl("TxtFOOTEREmpNo");
            TextBox TxtEmail = (TextBox)GVRegistrations.FooterRow.FindControl("TxtFOOTEREmail");

            con.Open();

            SqlCommand cmd = new SqlCommand("SELECT Count(*) FROM ACRegistrations where UserName= '" + TxtUserName.Text + "'", con);
            int count = int.Parse(cmd.ExecuteScalar() + "");

            if (count == 0)
            {

            cmd = new SqlCommand("INSERT into ACRegistrations(UserName,EmployeeNumber,Email) values('" + TxtUserName.Text + "','" + EmployeeNumber.Text + "','" + TxtEmail.Text + "')", con);
            int result = cmd.ExecuteNonQuery();

           
            if (result == 1)
            {
                BindRegistrations();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Record Added successfully');", true);
               
            }
            }

                     else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Sorry, This Record already exists');", true);
                    
                }

        }
            con.Close();
               
}


I want to validate three fields, one after the other
Please help me, Thanks in advance.
Posted
Updated 24-Aug-13 22:43pm
v2
Comments
Mahesh Bailwal 25-Aug-13 4:50am    
so where you got stuck?
Software Engineer 892 25-Aug-13 4:55am    
only i can validate username here, please can you help for other fields.

Below query will check if any of these three fields (Username,EmployeeNumber, Email) exist in database it will return count more than zero. Please try below query.


"SELECT Count(*) FROM ACRegistrations where UserName= '" + TxtUserName.Text + "' or EmployeeNumber='" + EmployeeNumber.Text + "' or Email='" + TxtEmail.Text +"'";
 
Share this answer
 
Comments
Software Engineer 892 26-Aug-13 1:58am    
hello sir,
according to ur its working fine. but how should i know which record already exists in database. i mean which field is already exists.

when i click on add button, it should get a popup message. which field already exists.

Please Please can you help me.

Thanks.
Mahesh Bailwal 26-Aug-13 3:00am    
"select top 1
(select COUNT(*) from ACRegistrations where UserName ='" + TxtUserName.Text + "') as UserNameCount,
(select COUNT(*) from ACRegistrations where EmployeeNumber ='" + EmployeeNumber.Text + "') as EmployeeNumberCount,
(select COUNT(*) from ACRegistrations where Email ='" + TxtEmail.Text +"') as EmailCount
from ACRegistrations"

Above query will work for you, use this query with SqlDataReader, below link is reference for using SqlDataReader.

http://www.java2s.com/Code/CSharp/Database-ADO.net/controlSqlCommandtoreturnasinglerow.htm
Software Engineer 892 26-Aug-13 3:31am    
Sir, am confused am unable to understand. I need a popup message for every already existing column/ field.

Please help me. Thanks.
Mahesh Bailwal 26-Aug-13 3:39am    
From above query you will get count of existing data for three fields in database in return set.

For Example if username already exist then UserNameCount value will greater than zero.

Now based on value of UserNameCount,EmployeeNumberCount and EmailCount. You can display popup message to the user.

Let me know if you need more clarification.

Note : Always use Trim() feature for any value(Good Practice).
SQL
"SELECT Count(*) FROM ACRegistrations where UserName= '" + TxtUserName.Text.Trim() + "' or EmployeeNumber='" + EmployeeNumber.Text.Trim() + "' or Email='" + TxtEmail.Text.Trim() +"'";


if Count is greater than one dont insert alert user with error message else insert.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900