Click here to Skip to main content
15,886,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So, I am trying to build a piece of code where I am able to depending on a value from a column it will insert that row or not into the database. Basically it will check all the rows that I am trying to insert and see if that is any one with a value column, in this case, with "M".

What I have tried:

<pre>for (int linhas = 0; linhas < dataGridView1.Rows.Count; linhas++)
                    {
                        if (dataGridView1.Rows[linhas].Cells[8].Value.ToString() != "M")
                        {

                            string queryInsert = @"INSERT INTO CIM_Etiquetas(Carga, Referencia, Quantidade, DataEmissao, Linha, Marca, DescricaoWeb, CodProd, TipoEmb, NomePC) VALUES (@Carga, @Referencia, @Quantidade, @DataEmissao, @Linha, @Marca, @DescricaoWeb, @CodProd, @TipoEmb, @NomePC)";
                            cmd = new SqlCommand(queryInsert);
                            cmd.Connection = con;

                            cmd.Parameters.Add(new SqlParameter("@Carga", SqlDbType.VarChar, 11, "Carga"));
                            cmd.Parameters.Add(new SqlParameter("@Referencia", SqlDbType.VarChar, 30, "Referencia"));
                            cmd.Parameters.Add(new SqlParameter("@Quantidade", SqlDbType.Decimal, 11, "Quantidade"));
                            cmd.Parameters.Add(new SqlParameter("@DataEmissao", SqlDbType.SmallDateTime, 11, "DataEmissao"));
                            cmd.Parameters.Add(new SqlParameter("@Linha", SqlDbType.SmallInt, 4, "Linha"));
                            cmd.Parameters.Add(new SqlParameter("@Marca", SqlDbType.VarChar, 11, "Marca"));
                            cmd.Parameters.Add(new SqlParameter("@DescricaoWeb", SqlDbType.VarChar, 150, "DescricaoWeb"));
                            cmd.Parameters.Add(new SqlParameter("@CodProd", SqlDbType.VarChar, 20, "CodProd"));
                            cmd.Parameters.Add(new SqlParameter("@TipoEmb", SqlDbType.VarChar, 8, "TipoEmb"));
                            cmd.Parameters.Add(new SqlParameter("@NomePC", SqlDbType.VarChar, 30, "NomePC"));

                            cmd.Parameters["@Carga"].Value = dataGridView1.Rows[linhas].Cells[0].Value.ToString();
                            cmd.Parameters["@Referencia"].Value = dataGridView1.Rows[linhas].Cells[1].Value.ToString();
                            cmd.Parameters["@Quantidade"].Value = dataGridView1.Rows[linhas].Cells[2].Value.ToString();
                            cmd.Parameters["@DataEmissao"].Value = dataGridView1.Rows[linhas].Cells[3].Value.ToString();
                            cmd.Parameters["@Linha"].Value = dataGridView1.Rows[linhas].Cells[4].Value.ToString();
                            cmd.Parameters["@Marca"].Value = dataGridView1.Rows[linhas].Cells[5].Value.ToString();
                            cmd.Parameters["@DescricaoWeb"].Value = dataGridView1.Rows[linhas].Cells[6].Value.ToString();
                            cmd.Parameters["@CodProd"].Value = dataGridView1.Rows[linhas].Cells[7].Value.ToString();
                            cmd.Parameters["@TipoEmb"].Value = dataGridView1.Rows[linhas].Cells[8].Value.ToString();
                            cmd.Parameters["@NomePC"].Value = System.Environment.MachineName;

                            cmd.ExecuteNonQuery();
                        }
                    }


If that row as a column with that value it will jump to the next row until it's finished. Do you have any ideas how can I solve it?
Posted
Updated 6-Feb-17 3:11am
v3

1 solution

If I understand your question, you want to skip rows that have an "M" in a certain column.

Change your if statement from this:

C#
if ((Myrow.Cells[8].Value.ToString()) == "M")
{
    //What should I do here?
}
else
{


...to this:

C#
if (Myrow.Cells[8].Value.ToString()) != "M")
{

This will only insert rows that do not have a value of M in the 8th column, and will not insert them if they do.

If that's NOT what you meant, improve your question.
 
Share this answer
 
Comments
Member 12977712 6-Feb-17 9:05am    
I've updated the post with some changes in the code.
Member 12977712 6-Feb-17 9:14am    
Done. Thanks!
Member 12977712 6-Feb-17 9:14am    
if (Myrow.Cells[8].Value.ToString().Trim() != "M") - I've changed it
#realJSOP 6-Feb-17 9:22am    
So do you still have a question?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900