This is the scenario, I have a select query then all fetched data must be inserted into another table.. This is what I came up, I don't know if the for loop does anything.
If I would remove the for loop. Example: Fetched data is id1, id2, id3 the inserted data in my database is id1, id1, id1 instead of id1, id2, id3
sql = "SELECT * FROM dummy_violate WHERE res_month <> @month Or res_year <> @year"
cmd = New MySqlCommand(sql, con)
cmd.Parameters.AddWithValue("@month", DateTime.Now.ToString("MMMM"))
cmd.Parameters.AddWithValue("@year", DateTime.Now.ToString("yyyy"))
dr = cmd.ExecuteReader
While dr.Read
id += dr(0)
count += 1
End While
dr.Close()
If count > 0 Then
For i As Integer = 1 To count
sql2 = "INSERT INTO dummy_violate(res_id, res_month, res_year, is_paid)VALUES(@id,@month,@year,@paid)"
cmd = New MySqlCommand(sql2, con)
cmd.Parameters.AddWithValue("@id", id)
cmd.Parameters.AddWithValue("@month", DateTime.Now.ToString("MMMM"))
cmd.Parameters.AddWithValue("@year", DateTime.Now.ToString("yyyy"))
cmd.Parameters.AddWithValue("@paid", 0)
cmd.ExecuteNonQuery()
Next
ElseIf count = 0 Then
MsgBox("Wrong Query")
End If