Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

How could be remove the duplicate name when I click on save button and tell me it’s already exists

i am using above code for save name ,address etc. from the database.

C#
private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
                string query = "insert into CustomerTable values('" + txtCustomerName.Text + "','" + txtAddress.Text + "','" + txtMobileNo.Text + "','" + lblGoldBalance.Text + "','" + lblSilverBalance.Text + "','" + lblCashBalance.Text + "')";
                SqlConnection conn = new SqlConnection(connstr);

                if (txtCustomerName.Text != "" & txtAddress.Text != "")
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(query, conn);
                    cmd.ExecuteNonQuery();

                    txtCustomerName.Clear();
                    txtAddress.Clear();
                    txtMobileNo.Clear();

                    MessageBox.Show("Values Save in DataBase");
                    conn.Close();
                }
                else
                {
                    MessageBox.Show("Only Mobile Field Can be Empty");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("At Least 0 value can be save in the Gold Silver Cash Area",ex.Message);
                
            }
Posted
Updated 2-Sep-13 18:57pm
v2

1 solution

Hi,
You can create a function which checks for the value from data base
here is the sample code.
1:You will write a function
C#
private Boolean usernameexists()
      {

              OleDbCommand com = new OleDbCommand("select Username from User_master where        Username ='" + txt_username.Text + "'", con);
              OleDbDataReader oledbrd1;
              oledbrd1 = com.ExecuteReader();
              if (oledbrd1.HasRows)
              {
                  oledbrd1.Read();
                      unameerrmsg.Visible = true;
                      unameerrmsg.Text = "username already exist";
                      oledbrd1.Close();
                      return true;

              }
              else
              {
                  unameerrmsg.Visible = false;
                  return false;
              }
          }


2: On your save button if username exist return
C#
if (usernameexists() == true)
           {
               return;
           }


You can also call this function on textbox textchanged()

hope this helps
Happy coding :)
 
Share this answer
 
v2
Comments
Manish Arya 3-Sep-13 1:33am    
its working
thanks

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