Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how i Stop Saving Duplicated Records in Access Database Table
i Need if RollNumber exist in my DataBase Then Data Not Save

What I have tried:

C#
private void SaveData()
        {
            String connstring = ConfigurationManager.ConnectionStrings["FincorpData"].ConnectionString;
            string cmdstring = "insert into customer (RollNumber, Name, FatherName, Address, Address1, MobileNo, SecMobileNo, City, District, State, PinCode, Remark) VALUES (@RollNumber, @Name, @FatherName, @Address, @Address1, @MobileNo, @SecMobileNo, @City, @District, @State, @PinCode, @Remark)";
          
            using (OleDbConnection con = new OleDbConnection(connstring))
            {
                using (OleDbCommand cmd = new OleDbCommand(cmdstring, con))
                {
                    con.Open();

                    cmd.Parameters.AddWithValue("@RollNumber", textBoxRollNumber.Text);
                    cmd.Parameters.AddWithValue("@Name", txtName.Text);
                    cmd.Parameters.AddWithValue("@FatherName", txtFatherName.Text);
                    cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
                    cmd.Parameters.AddWithValue("@Address1", txtAddress1.Text);
                    cmd.Parameters.AddWithValue("@MobileNo", txtMobileNo.Text);
                    cmd.Parameters.AddWithValue("@SecMobileNo", txtSecMobileno.Text);
                    cmd.Parameters.AddWithValue("@City", txtCity.Text);
                    cmd.Parameters.AddWithValue("@District", txtDistrict.Text);
                    cmd.Parameters.AddWithValue("@State", txtState.Text);
                    cmd.Parameters.AddWithValue("@PinCode", txtPinCode.Text);
                    cmd.Parameters.AddWithValue("@Remark", txtRemark.Text);

                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Recourd is saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    con.Close();
                }   
            }
        }
Posted
Updated 26-Mar-20 9:13am
v2

You should do a SELECT command first, searching for corresponding id.
If the query does not return any row, then the id does not exist, and you can issue your INSERT command. Otherwise, you should perform an UPDATE command instead.
 
Share this answer
 
Comments
Richard MacCutchan 26-Mar-20 11:18am    
I hope this wasn't a wild guess.
Maciej Los 26-Mar-20 11:55am    
This answer sounds reasonable.
And your guess is correct ;)
Richard MacCutchan 26-Mar-20 11:57am    
I was referring to a previous exchange with phil.o. What we might call "an 'in' joke".
Maciej Los 26-Mar-20 15:11pm    
;)
phil.o 26-Mar-20 12:29pm    
This one wasn't, indeed :)
In order to safely prevent duplicate records, you should utilize constraints in the database. For typical constraints see SQL Constraints[^]

What comes to preventing duplicates you should use either SQL PRIMARY KEY Constraint[^] or SQL UNIQUE Constraint[^].

This is the only reliable method to ensure that duplicates do not exist.

What comes to inserting the data, a brute-force approach is to insert the data and if you get an error message stating that the constraint has prevented the insert then do an update.
 
Share this answer
 
Comments
Maciej Los 26-Mar-20 15:12pm    
5ed!

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