Add days on increasing date via For

Asked

Viewed 1,233 times

1

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

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();
  • The solution is simple to give a complete and correct answer I need to know what is the type of column dtVencimento.?

  • at that time string, but if it’s easier I know how to switch to datetime

1 answer

2


First, you are taking a text and you need to do a conversion to date, at least to make it easier. Then just add 30 thirty days on date to each step, it’s pure math, i * 30. If instead of being 30 days it is 1 month (are different things), you can do too. Ideally save dates in proper format. If you want to keep your spine like character, can do the conversion again for this type.

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

I put in the Github for future reference.

Note that it is necessary to ensure that the date will always be in correct format, otherwise you should take care of it before. As I said on previous question.

I can’t guarantee that’s exactly what you need because the question doesn’t go into so much detail, but that’s pretty much it. You may need to make some adaptation. It may be that the format generated by ToString() is not suitable for what you need and you have to choose your own format. Or it’s even more interesting to change the column type.

It also needs to analyze whether this is the most correct algorithm. It seems that it tries to establish the payoffs of installments. I have my doubts that this should be the strategy. It may be a naive way to do this, but I can’t say anything. Every company has a way of working.

Browser other questions tagged

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