The error is that for
is not running, it is terminating before it even starts, after all, at the beginning of it ;
already exists. The correct would be to have a block of commands then 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();
Note that I made some changes.
I removed the exception because it was not doing anything useful. If you do not have something useful to do with the exception, do not to capture .
I've simplified putting the statement and assignment on the same line, there's no reason to do it on separate lines. In some cases I've used var
which simplifies too.
And mostly I used the right command to mount the query securely. What was being done was reckless.
Using double
type for monetary value is another temerity .
There are other things that seem strange, but since I can not see the whole thing, I will not say it. One of these is the use of Hungarian notation in the column names. There are others.
If the types of dblValorParcel
and dtVencimento
are numeric and date, you probably need to do a conversion before using it, since what comes from outside comes as string
. If some 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 true even for the existing Convert
. The ideal would be to treat it differently and avoid the exception, but it is another matter.