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