Variable with unassigned value within a FOR Loop

3
    int i;
    string cpf;
    cpf = "11111111111";
    DbConnection cnx = ADO_Utils.GetConnection();
    DbCommand cmd = ADO_Utils.GetComando(cnx);
    cmd.CommandType = CommandType.Text;
    for (i = 1; i < Convert.ToInt32(txbQtde.Text); i++) ;
    {
        string query;
        query = "insert into tblAcordoParcel (txtCPF, intParcela, dblValorParcel, dtVencimento, blnBaixada) ";
        query = query + "Values (  " + "'" + cpf + "',";
        query = query + i + ",";
        query = query + "" + txbParcel.Text + ",";
        query = query + "'" + txbDt.Text + "',";
        query = query + "0"+ ")";
        try
        {
            cmd.CommandText = query;
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
            {
                throw ex;
            }

When executing this code, the variable i assumes the value typed on the screen and not the number 1 that I declared inside FOR, can someone help me to correct it?

    
asked by anonymous 21.04.2016 / 03:59

1 answer

5

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.

    
21.04.2016 / 07:03