Variable with unassigned value within a "for" loop

Asked

Viewed 180 times

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 I declared within the for.

  • I’ve never done anything using C# so I have some questions about your code: there is a ; in front of the command line for, I believe you are wrong; you create a variable int but compares with a variable int32 (Convert.ToInt32(txbQtde.Text)) and then concatenates her with a string, from my point of view there is something very wrong with that. ; on the line of your command for

  • @Gerep int is alias for Int32.

1 answer

5


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.

  • Thank you for using several the word rashness... Shows that you committed to respond in the best way for me to learn. Thanks for the help!

  • Thank you for understanding this. There are people who love seeing problems in others. This shows their maturity. I could not classify otherwise. If everyone was like you, this site would be a marvel. The problem is not not knowing, everyone here knows almost nothing of everything, the problem is when one does not want to learn and prefers to stay mimimi. Congratulations.

  • The whole world has been too friendly even.... But recklessness aside when I have to pass pennies of error as I insert these values into the insert without the comma???

  • 1

    Converting to value. While using a double has to convert to this type, preferably "try to convert". Of course if you are sure that the conversion will be successful (there is no chance of having an invalid value in that string, then you can make it simpler, you can even use the Convert that you already know how to use. Example that depends on format: http://answall.com/q/106672/101,

  • It worked both for and monetary value... Thank you... Have any article or even reply here in the flow that shows how to create PDF document type receipt or statement???

  • I specifically do not know. http://answall.com/search?q=[c%23]+pdf

  • Within this same IT is necessary that at each step of the variable i be added 30 days in txbDt.Text, I need to use another for or have another way of doing??

  • No, you’re lining up a lot of questions anyway. Each question needs to be a new question to be organized and to be able to facilitate the search of other people.

  • question asked.... thanks for the guidance

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.