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");
}
}
}
After weeks i finally found the way to make it work;
for (int i = 0; i < dtproddisp.Rows.Count; i++)
{
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[i]["descricao"]);
objcmd.Parameters.AddWithValue("@QUANTIDADE", dtproddisp.Rows[i]["quantidade"]);
objcmd.Parameters.AddWithValue("@COD_PRODUTO", dtproddisp.Rows[i]["codigo"]);
objcmd.ExecuteNonQuery();
}
If anyone else has the same problem, I've posted my solution now i need to polish it, need to get max id from table, for now i i'n using a combobox with desc order to get it, but i believe there're a better way to do it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.