Click here to Skip to main content
14,486,704 members
Rate this:
Please Sign up or sign in to vote.
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:

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))

                    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);

                    MessageBox.Show("Recourd is saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
Updated 4 days ago
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
Richard MacCutchan 4 days ago
I hope this wasn't a wild guess.
Maciej Los 4 days ago
This answer sounds reasonable.
And your guess is correct ;)
Richard MacCutchan 4 days ago
I was referring to a previous exchange with phil.o. What we might call "an 'in' joke".
Maciej Los 4 days ago
phil.o 4 days ago
This one wasn't, indeed :)
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
Maciej Los 4 days ago

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100