Insert value in column C#

Asked

Viewed 86 times

2

I have to read a column (which is the primary key of a table) and see if there is the value of a variable in that column, 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 enter even if it already exists so the program gives error because there can be 2 equal primary keys):

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();
  • friend this group variable comes from where? what value has in it?

  • @Meajudasilvio I have an xml file with product information (the group is one of them) and I use a foreach to go through all the products and each foreach the variable takes different values.

  • But come to think of it, man, you want a list with no repeat values, right? vc can not fill this list as the friend said below, there in the list will have the repeated values, then you use a list.distinct() then you will have the list only with the codes that do not repeat there on your LSE, you use this 'clean' list to make your Links

  • You have a list of "Codes" and want to see if these Codes are "Code" from your Articlefamily?

  • @Meajudasilvio the list I created was to put the values that have already been entered, to then not be inserted again

  • @Aline no, I have a variable, I want to see if that variable exists in a column, if it doesn’t exist, I want to insert there, if it exists, it does nothing.

  • the problem and that your logic is correct friend, it seems to me that the error is in this line if (group != rd3["Code"].Tostring() tries to play that rd3["Code"]. Tostring() in a variable before if and see the values qeu it returns while Voce debugs the code.

  • @Meajudasilvio already managed to solve this problem, I deleted the list I had created, I don’t need it. Instead, I made a Try catch and it worked

  • So beauty guy, sorry We have helped more, but put your solution there and mark it as an answer for other users to analyze what was done who knows does not help someone in the future

  • 1

    @Meajudasilvio helped enough ! Thank you very much, I will post how the code and how it is working !

  • You said in another comment that: "I have an xml file with product information (the group is one of them) and I use a foreach to go through all the products and each foreach the variable takes different values." Then there is a list of Codes that Voce checks. A list of objects with this property. I think you might want to think about adding this list to the Where from select para not in and foreach just what’s left. Just hint. Like you here.

Show 6 more comments

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.