3
I am trying to change the data saved in the Database, basically when I double click on DataGrid
he populates my TextBoxs
to make the changes in the bank, but when I click to save it from the following error. Detail this error only happens when I add one more value to the txtPreco
, if I delete what was inserted and put another value it registers a new product (which is not supposed to happen when I edit)
here the photo from the database
this is my code I’m using to try to change the data registered in the database
//criando a bool
bool informacoesSalvas = false;
private void btnSalvar_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtNome.Text))
{
System.Windows.Forms.MessageBox.Show("O nome do produto está vazio, por favor digite algo");
return; // sai do método
}
else if (string.IsNullOrWhiteSpace(txtPreco.Text))
{
System.Windows.Forms.MessageBox.Show("O Preço do produto está vazio, por favor digite algo");
return;
}
else if (informacoesSalvas)
{
// Abre a conexão
conectar.Open();
//Query SQL
MySqlCommand command = new MySqlCommand("INSERT INTO Produto (Nome,Descricao,Preco)" +
"VALUES('" + txtNome + "','" + txtDescricao.Text + "','" + txtPreco.Text + "' )", conectar);
//Executa a Query SQL
command.ExecuteNonQuery();
// Fecha a conexão
conectar.Close();
//Mensagem de Sucesso
MessageBox.Show("Alterado com Sucesso!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information);
informacoesSalvas = true;
}
else
{
conectar.Open();
//Convertendo
Converter = Convert.ToDecimal(txtPreco.Text);
//MessageBox.Show("Conectado");
MySqlCommand Inserir = new MySqlCommand();
Inserir.Connection = conectar;
Inserir.CommandText = "INSERT INTO Produto (Nome, Descricao, Preco, `status`) VALUES (@peca, @nome, @quantidade, @dataentrada)";
Inserir.Parameters.AddWithValue("@peca", txtNome.Text);
Inserir.Parameters.AddWithValue("@nome", txtDescricao.Text);
Inserir.Parameters.AddWithValue("@quantidade", txtPreco.Text);
Inserir.Parameters.AddWithValue("@dataentrada", ckbAtiv.Checked);
Inserir.ExecuteNonQuery();
conectar.Close();
MessageBox.Show("Produto Cadastrado", "Concluido",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
selecionarCategoria();
}
}
I think it’s not your code that’s the problem, it’s your bank insertion. There is something wrong with your 'Price' column, it may be that this column does not exist in your current table or you are trying to insert some type of variable that is not accepted by the type of your column, for example, add a string in a double column. You checked out this information I mentioned?
– Luis Faconi
What value goes in the text
txtPreco.Text
? In the database the columnPreco
is of what type?– João Martins
The database of the column
Preco
it’s double, I’ll put a photo from the database– Pietro Nunciaroni
What is the value of the property
Inserir.CommandText
?And show the line you pass the parameter on@Preco
.– Augusto Vasques
@Augustovasques did not understand very well, I’m starting in C# yet. could explain a little better
– Pietro Nunciaroni
Ali in the picture is giving error in
Inserir.ExecuteNonQuery()
i want to know the value of the propertyCommandText
of the objectInserir
.– Augusto Vasques
I guess that’s it?
Inserir.CommandText = "INSERT INTO Produto (Nome, Descricao, Preco,
status) VALUES (@peca, @nome, @quantidade, @dataentrada)";
– Pietro Nunciaroni
I would also like to see the line on
Inserir.Parameters.AddWithValues(...
where the first parameter is@Preco
– Augusto Vasques
That’s the mistake right there :
Inserir.CommandText = "INSERT INTO Produto (Nome, Descricao, Preco, status) VALUES (@peca, @nome, @quantidade, @dataentrada)";
the parameters do not match– Augusto Vasques
Opa I’ll try here to see how it unfolds
– Pietro Nunciaroni
Needs to be put in the same order ?
– Pietro Nunciaroni
yes and has to be compatible.
– Augusto Vasques
OK, I’m modifying and I’ll compile to see how it unfolds, thank you @Augustovasques
– Pietro Nunciaroni
@Augustovasques good he started to only register the product, still can not save the changes
– Pietro Nunciaroni
You have to open one data transaction insert into database using command commit and that’s the only way he can record in the database.
– Augusto Vasques
I’m kind of young, can you give me an example of how to do this ?
– Pietro Nunciaroni
@Augustovasques, the example you presented was good, and I’m using, however it still only register, I decided to do it on another button but gave another error, follow the link of the other error link
– Pietro Nunciaroni