Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello Everyone,

I have a code which I insert values every month in vb.net, what I need now is to insert certain values every 3 months. I have a textBox which I can decide how many times or how many months I want to insert into the database.
This is the code that I insert every month how many times I want :


Dim startdate As DateTime = DateTimePickerFixCosts1.Value
           Dim enddate As DateTime = startdate.AddMonths(Convert.ToInt32(txtNumberofMonths.Text))
           Dim amount As Decimal = Decimal.Parse(txtPriceFixCosts.Text)
           Const query As String = "INSERT INTO Test_Table(Date,Description,Payment,Quantity,Price,Department,Status,Store) VALUES (@date,@description,@payment,@quantity,@price,@department,@status,@Store)"
           Do While (startdate > enddate)

               Using conn As SqlConnection = New SqlConnection("Data Source=#######\SQLEXPRESS;Initial Catalog=Expenses;Trusted_Connection=yes;")
                   Using command As SqlCommand = New SqlCommand(query, conn)

                       command.Parameters.AddWithValue("@description", TxtItemDescFixCosts.Text)
                       command.Parameters.AddWithValue("@payment", TxtPaymentFixCosts.Text)
                       command.Parameters.AddWithValue("@price", amount)
                       command.Parameters.AddWithValue("@date", startdate)
                       command.Parameters.AddWithValue("@status", ComboBox1.Text)
                       command.Parameters.AddWithValue("@quantity", TextBoxIDd.Text)
                       command.Parameters.AddWithValue("@department", TxtFixCosts.Text)
                       command.Parameters.AddWithValue("@Store", TxtStore.Text)

                       conn.Open()
                       command.ExecuteNonQuery() ' NB: Don't use ExecuteReader when the query doesn't return anything!
                       startdate = startdate.AddMonths(1)
                   End Using

               End Using
           Loop
           MsgBox("Your Data has been Inserted")


What I have tried:

I tried to change the add months to a

different number but it won't record anything.

If CategoryBox.SelectedItem = "Monthly" Then
        Dim startdate As DateTime = DateTimePickerFixCosts1.Value
        Dim enddate As DateTime = startdate.AddMonths(Convert.ToInt32(txtNumberofMonths.Text))
        Dim amount As Decimal = Decimal.Parse(txtPriceFixCosts.Text)
        Const query As String = "INSERT INTO Test_Table(Date,Description,Payment,Quantity,Price,Department,Status,Store) VALUES (@date,@description,@payment,@quantity,@price,@department,@status,@Store)"
        Do While (startdate > enddate)

            Using conn As SqlConnection = New SqlConnection("Data Source=#######\SQLEXPRESS;Initial Catalog=Expenses;Trusted_Connection=yes;")
                Using command As SqlCommand = New SqlCommand(query, conn)

                    command.Parameters.AddWithValue("@description", TxtItemDescFixCosts.Text)
                    command.Parameters.AddWithValue("@payment", TxtPaymentFixCosts.Text)
                    command.Parameters.AddWithValue("@price", amount)
                    command.Parameters.AddWithValue("@date", startdate)
                    command.Parameters.AddWithValue("@status", ComboBox1.Text)
                    command.Parameters.AddWithValue("@quantity", TextBoxIDd.Text)
                    command.Parameters.AddWithValue("@department", TxtFixCosts.Text)
                    command.Parameters.AddWithValue("@Store", TxtStore.Text)

                    conn.Open()
                    command.ExecuteNonQuery() ' NB: Don't use ExecuteReader when the query doesn't return anything!
                    startdate = startdate.AddMonths(3)
                End Using

            End Using
        Loop
        MsgBox("Your Data has been Inserted")


    End If

So I need to insert this certain information every 3 months for a certain period of time but it doesn't work like this.

Any help is much appreciated.

Thankfully
Posted
Updated 26-Jul-20 7:28am
Comments
Maciej Los 27-Jul-20 3:42am    
Please, do not repost!
Member 13410460 27-Jul-20 6:36am    
This is based on quarterly, annually, semi-annually insertion based on the number of how many times to repeat insertion. ITs not the same.
Thank you

1 solution

VB
Do While (startdate > enddate)

Don't you mean
VB
Do While (startdate < enddate) // while startdate is less than enddate?
 
Share this answer
 
Comments
Member 13410460 27-Jul-20 10:13am    
Yep, the second one is right, my mistake. But still, It won't record how many times I want.
Thank you for your answer.
Bests
Richard MacCutchan 27-Jul-20 10:33am    
Well you need to explain exactly which part of the code is failing and why. Looking at what you have above I cannot see any other issue.
Member 13410460 27-Jul-20 10:37am    
Thank you for your reply and help, I fixed it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900