Insert value into a C #

2

I have to read a column (which is the primary key of a table) and see if in that column there is a value of a variable, if it does not exist, I want to add it, if it exists, I want to move on. I have this code, but it tries to insert even if it already exists then the program gives error because there can not be 2 primary keys alike):

List<string> valoresExistentes = new List<string>();

                conn.Open();
                SqlCommand cmd3 = new SqlCommand("SELECT Code FROM ArticleFamily", conn);
                SqlDataReader rd3 = cmd3.ExecuteReader();

                if (rd3.HasRows)
                {
                    rd3.Read();   
                    if (group != rd3["Code"].ToString())
                    {
                        if (valoresExistentes.Contains(group))
                        {
                            continue;
                        }
                        else
                        {
                            valoresExistentes.Add(group);

                            conn.Close();
                            conn.Open();
                            SqlCommand cmd4 = new SqlCommand(@"INSERT INTO ArticleFamily(Code, [Desc], IsDefault, IsToWeb, CreatedBy, CreatedOn, ModifiedBy, ModifiedOn, IsDeleted, Margin, IsPOSVisible, IsWithArticleCodePrefix, ArticleCodePrefix, ExternalCode, IsSubFamilyRequired)
                                                         VALUES (@code, 'Descrição', 0, 0, 1, @date, 1, @date, 0, 0, 1, 1, @code, 1, 0)", conn);
                            cmd4.Parameters.AddWithValue("@code", group);
                            cmd4.Parameters.AddWithValue("@date", DateTime.Now);
                            cmd4.ExecuteNonQuery();
                        }
                    }
                }
                conn.Close();
    
asked by anonymous 27.04.2017 / 16:22

1 answer

1

I changed the code like this with a try catch and it worked:

conn.Open();
                SqlCommand cmd3 = new SqlCommand("SELECT Code FROM ArticleFamily", conn);
                SqlDataReader rd3 = cmd3.ExecuteReader();

                if (rd3.HasRows)
                {
                    rd3.Read();
                    if (group != rd3["Code"].ToString())
                    {
                        try
                        {
                            conn.Close();
                            conn.Open();
                            SqlCommand cmd4 = new SqlCommand(@"INSERT INTO ArticleFamily(Code, [Desc], IsDefault, IsToWeb, CreatedBy, CreatedOn, ModifiedBy, ModifiedOn, IsDeleted, Margin, IsPOSVisible, IsWithArticleCodePrefix, ArticleCodePrefix, ExternalCode, IsSubFamilyRequired)
                                                         VALUES (@code, 'DESCRIÇÃO', 0, 0, 1, @date, 1, @date, 0, 0, 1, 1, @code, 1, 0)", conn);
                            cmd4.Parameters.AddWithValue("@code", group);
                            cmd4.Parameters.AddWithValue("@date", DateTime.Now);
                            cmd4.ExecuteNonQuery();
                        }
                        catch { }
                    }
                }
                conn.Close();
    
27.04.2017 / 18:23