Hello Everyone,
I want to update my tables in SQL based on the number of months I insert in text.box,
this is my code :
<pre> Dim startdate As DateTime = DateTimePickerFixCosts1.Value
Dim enddate As DateTime = startdate.AddMonths(Convert.ToInt32(txtNumberofMonths.Text))
Do While (startdate < enddate)
Const query As String = "Update Test_Table set [Status] =@status, [Date] = @date "
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("@date", startdate)
command.Parameters.AddWithValue("@status", ComboBox1.Text)
conn.Open()
command.ExecuteNonQuery() '
startdate = startdate.AddMonths(1)
End Using
End Using
Loop
MsgBox("Your Data has been Updated")
What I have tried:
This code works perfectly fine if I use :
Const query As String = "INSERT INTO Test_Table(Date,Description,Payment,Quantity,Price,Department,Status) VALUES (@date,@description,@payment,@quantity,@price,@department,@status)"
but when I want to update it updates all the records I have in Table based on values in text.box.
Any Help is appreciated,
Best
E