Add days to date increasing via For

1

In the code below I add 1 to each step in the variable i , to add 30 days in the dt field, do I need another for or what to do in it?

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();
    
asked by anonymous 23.04.2016 / 01:45

1 answer

2

First, you're getting text and need to do a conversion to date, at least to make it easier. Then just add 30 thirty days to the date at each step, it's pure mathematics, i * 30 . If instead of being 30 days for 1 month (they are different things), you can do too. It is best to save dates in an appropriate format. If you want to keep the column as character , you can do the conversion again for this type.

cmd.Parameters.AddWithValue("@dt", Convert.ToDateTime(txbDt.Text).AddDays(i * 30).ToString());

Note that you need to ensure that the date is always in the correct format, otherwise you need to take care of this beforehand. As I said in the previous question .

I can not guarantee that's exactly what you need because the question does not give you so much detail, but it's basically this. Maybe I need to make some adjustments. It may be that the format generated by ToString() is not suitable for what you need and you have to choose a format of your own. Or even more interesting to change the column type.

You also need to analyze whether this is the most correct algorithm. It seems that he tries to establish the maturities of installments. I have my doubts that this should be the strategy. It can be a naive way of doing this. But I can not say anything. Every company has a way of working.

    
23.04.2016 / 01:57