Click here to Skip to main content
15,914,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on asp.net c# sqlserver 2005 database.

on my webpage i have a gridview, in this gridview i have 4 fields (Username,Password, email,designation) and 4 textboxes in footer to enter the records. and a add button to add the record.

if the record already exists in database, display a message Username already exists.

Thanks in advance.

This is my code in row command in gridview.

C++
protected void GVRegistrations_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew Registrations"))
        {
 TextBox TxtUsername  = (TextBox)GVRegistrations.FooterRow.FindControl("TxtFooterUsername");

 TextBox TxtPassword = (TextBox)GVRegistrations.FooterRow.FindControl("TxtFooterPassword");

            TextBox TxtEmail = (TextBox)GVRegistrations.FooterRow.FindControl("TxtFooterEmail");

TextBox TxtDesignation = (TextBox)GVRegistrations.FooterRow.FindControl("TxtFooterDesignation");

            con.Open();
            SqlCommand cmd = new SqlCommand("INSERT into ACRegistrations(Username,Password,Email,Designation) values('" + TxtUsername.Text + "','" + TxtPassword.Text + "','" + TxtEmail.Text + "','" + TxtDesignation.Text + "')", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                BindRegistrations();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Record Added successfully');", true);
            }
            
        }
    }


here just i want to add to code, that if username already exists. if i enter the same username onceagain.
Posted
Updated 20-Aug-13 0:19am
v2

1 solution

Do a select query for checking whether user name already present. Use below query for select

SELECT *
FROM ACRegistrations
WHERE Username = TxtUsername.text

If its not present then do an insert query.
 
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