Insert Data (union of label and textbox)

0

In my interface I have 7 label with already predetermined data and 7 textbox, each textbox is linked to a label. What I want is that when you write a number in a textbox that is in front of the label only the value is inserted in the code that indicates the label, so far I can only insert in a single field of the database and the code What I'm driving is this:

cmd.Parameters.AddWithValue("@codigo_integra", lbCodigo1.Text);
cmd.Parameters.AddWithValue("@cantidad", textBox1.Text);

I would like to know how I could do with the other 6 fields that I need because if I put the same code and change the label and the textbox I get an error that is already using.

In my database I have several codes and each code corresponds to a product. What I want is that when the user types a number in the texbox, that number is saved in the database. Example if in the code 284 in the textbox give 5 in the database I keep those 5, if in the code 288 in the textbox they give 10 in the database keep those 10 but only and only the product that is linked that code 288 or 284.

    
asked by Cristian R.M 05.08.2016 в 01:18
source

1 answer

0

Searching on the internet I found this code

 private void btnGuardar_Click(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection("MiCadenaConexion"))
        {
            con.Open();
            string consulta = "Insert into miTabla (...) values (@codigo_integram @cantidad)";

            SqlCommand cmd = new SqlCommand(consulta, con);

            //Recorro el form y obtengo los TextBox que no son vacíos, 
            //y los ordeno por el nombre ascendente
            List<TextBox> inputs = this.Controls.OfType<TextBox>().
                             Where(t => t.Text.Trim() != string.Empty)
                             .OrderBy(t => t.Name).ToList();

            foreach (TextBox txt in inputs)
            {
                //Obtengo el número identificador
                string num = txt.Name.Substring(txt.Name.Length - 1, 1);

                //Busco el Label que tenga el nombre lbCodigo#
                //dónde # es el número que encontré
                Label lbl = this.Controls.OfType<Label>().
                                  Where(x => x.Name == string.Concat("lbCodigo", num)).
                                  FirstOrDefault();

                //Si no es nulo, es porque encontró
                if (lbl != null)
                {
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@codigo_integra", lbl.Text);
                    cmd.Parameters.AddWithValue("@cantidad", txt.Text);

                    //Ejecutamos la consulta
                    cmd.ExecuteNonQuery();
                }//fin lbl
            }//fin foreach
        }//fin using
    }

and this works properly for me, and I leave it as it is reflected in the database

    
answered by 09.08.2016 / 00:56
source