0

While clicking on add button data is saved in database but after 2-3 times refresh data in database there 2-4 copies of same data is shown.

How to get to fix this?

String cs = ConfigurationManager.ConnectionStrings["MyDBConnectionString1"].ConnectionString;

using (SqlConnection con = new SqlConnection(cs))
{
  SqlCommand cmd = new SqlCommand("Insert into tblBrands values('" + txtBrandName.Text + "')", con);
  con.Open();
  cmd.ExecuteNonQuery();
  txtBrandName.Text = string.Empty;
}
1
  • Your code clearly inserts data and it has no provisions or checks to prevent duplicates. Either don't call the code using the same data more than once, or if you can't avoid that, add code to check for existing data before inserting new data. Commented Aug 29, 2018 at 8:48

1 Answer 1

1

If you are trying to solve in SQL (assuming from your tags) you could check before inserting using:

if not exists (select * from tblBrands where ...)

Build your where clause based on your criteria - what would you consider duplicate entry

More info on exists in Microsoft Docs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.