The mistake is that the for
is not running, it is being shut down even before starting, after all right at the beginning of it there is already a ;
that closes it. The correct thing would be to have a block of commands next, like this:
var cpf = "11111111111"; //se não for só um teste não precisa da variável
DbConnection cnx = ADO_Utils.GetConnection();
DbCommand cmd = ADO_Utils.GetComando(cnx);
cmd.CommandType = CommandType.Text;
for (var i = 1; i < Convert.ToInt32(txbQtde.Text); i++) { // <==== note que não tem o ;
cmd.CommandText = @"insert into tblAcordoParcel (txtCPF, intParcela, dblValorParcel,
dtVencimento, blnBaixada) Values (@cpf, @i, @parcel, @dt, 0)";
cmd.Parameters.AddWithValue("@cpf", cpf);
cmd.Parameters.AddWithValue("@i", i);
cmd.Parameters.AddWithValue("@parcel", txbParcel.Text);
cmd.Parameters.AddWithValue("@dt", txbDt.Text);
cmd.ExecuteNonQuery();
I put in the Github for future reference.
Note that I made some changes.
I pulled the exception because she wasn’t doing anything useful. If you don’t have something useful to do with the exception, don’t capture it.
I simplified putting the statement and assignment on the same line, there is no reason to do in separate lines. In some cases I used the var
which also simplifies.
And mostly I used the right command to mount the query safely. What was being done was a recklessness.
Use a guy double
for monetary value is another temerity.
There are other things that seem weird, but as I am not seeing the whole I will not pronounce. One of them is the use of hungarian notation in column names. Have other.
If the types of dblValorParcel
and dtVencimento
are numeric and date, probably need to make a conversion before using it, since what comes from outside comes as string
. If any conversion goes wrong for some reason (a text that does not produce a number, for example), there will be an exception and it will be a programming error. This is even true for the Convert
already existing. Ideally it would be to treat this differently and avoid the exception, but it is another matter.
I’ve never done anything using C# so I have some questions about your code: there is a
;
in front of the command linefor
, I believe you are wrong; you create a variableint
but compares with a variableint32
(Convert.ToInt32(txbQtde.Text)
) and then concatenates her with astring
, from my point of view there is something very wrong with that.;
on the line of your commandfor
– user3603
@Gerep
int
is alias forInt32
.– Maniero