If you want to insert data, you should be calling cmd.ExecuteNonQuery()
.
I'm also not convinced that SELECT * FROM [Student]
is the best way to insert your data, especially if you have primary keys in place. Instead you want to specify the columns you want insert into with the columns you want to select from.
I would strongly consider using parameters to avoid SQL injection. See Bobby Tables for more information on this. I use ?
as a placeholder for the parameters. With OleDbCommand
it's important to note that it's not the names of the parameters but the order in which they are declared that is important. I specify the data type so consider using the OleDbParameter Constructor (String, OleDbType) to add your parameters.
Lastly, I would also consider implementing Using:
Managed resources are disposed of by the .NET Framework garbage collector (GC) without any extra coding on your part. You do not need a Using block for managed resources. However, you can still use a Using block to force the disposal of a managed resource instead of waiting for the garbage collector.
This is some sample code:
Using con As OleDbConnection = cn,
cmd As New OleDbCommand("INSERT INTO [promote_student] (column1, column2, column3) SELECT column1, column2, column3 FROM [Student] WHERE [SrNo] = ? AND [session] = ?", con)
con.Open()
cmd.Parameters.Add("@srNo", OleDbType.[Type]).Value = row.Cells("sr_no").Value
cmd.Parameters.Add("@session", OleDbType.[Type]).Value = from_session.Text
cmd.ExecuteNonQuery()
End Using
Note that I don't know your table structure so have provided an example. You will have to change the column names to suit
Also note that I have used OleDbType.[Type]
. You will want to replace [Type]
with the data type you've used on your database.