Umair
Umair

Reputation: 13

How to handle data duplication in c#

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

Upvotes: 0

Views: 116

Answers (1)

RVid
RVid

Reputation: 1297

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

Upvotes: 1

Related Questions