Ok, after 2 days I got grid and max id from table working, now new problem: How to insert all products on the gridview which is also on datatable dtprodisp I got the first product inserted but don't know to do for loop and not repeat any already inserted product
private void Btngravar_Click(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection("ConnectionString");
conn.Open();
MySqlCommand objcmd = new MySqlCommand("insert into dispensacao (DESTINATARIO,COD_UNIDADE,COD_DEPARTAMENTO,DATA,SOLICITANTE,DEFERIDO_POR) values(?,?,?,?,?,?)", conn);
objcmd.Parameters.Add("@DESTINATARIO", MySqlDbType.VarChar, 45).Value = Cmbdestinatario.Text;
objcmd.Parameters.AddWithValue("@COD_UNIDADE", string.IsNullOrEmpty(Txtcodigounidade.Text) ? (object)DBNull.Value : Txtcodigounidade.Text);
objcmd.Parameters.AddWithValue("@COD_DEPARTAMENTO", string.IsNullOrEmpty(Txtcodigodep.Text) ? (object)DBNull.Value : Txtcodigodep.Text);
DateTime fdate = DateTime.Parse(Txtdata.Text);
objcmd.Parameters.Add("@DATA", MySqlDbType.DateTime).Value = fdate;
objcmd.Parameters.Add("@SOLICITANTE", MySqlDbType.VarChar, 45).Value = Txtsolicitante.Text;
objcmd.Parameters.Add("@DEFERIDO_POR", MySqlDbType.VarChar, 45).Value = Txtdeferido.Text;
objcmd.ExecuteNonQuery();
conn.Close();
conn.Open();
objcmd = new MySqlCommand("insert into produtos_disp(CODIGO_DISP,PRODUTO,QUANTIDADE,CODIGO_PROD) values (?,?,?,?)", conn);
objcmd.Parameters.AddWithValue("@CODIGO_DISP", Cmbid.Text);
objcmd.Parameters.AddWithValue("@PRODUTIO", dtproddisp.Rows[0]["descricao"]);
objcmd.Parameters.AddWithValue("@QUANTIDADE", dtproddisp.Rows[0]["quantidade"]);
objcmd.Parameters.AddWithValue("@COD_PRODUTO", dtproddisp.Rows[0]["codigo"]);
objcmd.ExecuteNonQuery();
this.Close();
}
this is the code and it works for the fist row
dtproddisp.Rows.Add(Txtcodigopro.Text, Txtproduto.Text, Txtquantidade.Text);
Do I need to change that? the code to add data to dtprodisp DataTable is not geting the codigo for some reason, was working a few minutes ago and i din't change anything:
private void Btnaddproduto_Click(object sender, EventArgs e)
{
dtproddisp.Rows.Add(Txtcodigopro.Text, Txtproduto.Text, Txtquantidade.Text);
Gridprodutos.DataSource = dtproddisp;
}
both other fields are working code to get "DESCRICAO" and "CODIGO":
private void Txtproduto_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MySqlConnection connection = new MySqlConnection("connectionString");
string selectQuery = "select descricao,codigo from produtos where barras =" + (Txtproduto.Text);
connection.Open();
MySqlCommand command = new MySqlCommand(selectQuery, connection);
MySqlDataReader reader = command.ExecuteReader();
if(reader.Read())
{
Txtproduto.Text = reader.GetString("DESCRICAO");
Txtcodigopro.Text = reader.GetString("CODIGO");
}
}
}
fororforeachloop through the table’s rows would work. In each iteration, clear the previous parameters and re-add the parameters with the values from that particular row. I am guessing this may be what you are asking. - JohnGforeachloop is empty? You could also use aforloop as you have done in the previous loop. Both will accomplish the same thing. - JohnGforeach ()… this is invalid and the code will not compile. So I am not sure how you would get an executing error on the line…objcmd.Parameters.AddWithValue("@anyText", dtproddisp.Rows['i']['j']);… I can only guess another compilation error foriandjsince they are out of scope and technically do not exist. Please edit your question with your updated code. - JohnG