Having a connection open doesn’t achieve much in itself. We need a command object to retrieve data, insert, update and delete records.
We’ll declare an OledbCommand variable that’s fit for the purpose of holding a command object.
After declaring the command variable we need to create a command object. We’ll create our Command object using the New Keyword and at the same time the Command object accepts 2 parameters namely the SQL Statement and the connection object. The connection object holds all the connection information to the database that our command object needs.
Once we’ve created our command object we can assign it to our command variable ‘command’.
As we’re just doing a straightforward insert and not returning any records we don’t need a DataReader object which we did in the Reading records code. We can simply execute the command, and to do this we can use the ExecuteNonQuery method of our Command object.
That’s pretty much all there is too it. On the next page is the full source code. We’ve added a label ‘label1’ control to our page and set it’s text property to ‘Record Inserted’ once the code has been successfully run.
<< Inserting Records with ASP.NET Part 1 |
Inserting Records with ASP.NET Part 3>> |
Get the best asp hosting provider from web-hosting-top.com and save up to 30%
Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.
