Wednesday, January 6, 2010

Using SqlDataSource to Insert manually--code

In many situations, the SqlDataSource can provide the convenient functions to get the data from SQL Server, and display them using the GridView, DetailView or ListView. It also can generate the Insert Update or Delete code for you to update the data in the SQL Server. And the ListView and GridView also generate some Item Templates, like Edit Template, for you. But sometime, you do not want to use these code generated automatically, you may have your own idea. You can invoke the Insert Update and Delete by yourself.
At first, you need a SqlDataSource. From below, you can see I have a Insert Command with three parameters. And I add a Inserting event for this DataSource.



Then we can create a Button control, in the Click event, we call the Insert() method which would invoke the Inserting Event. In this event, we can pass the proper parameter to the Insert Command.

Then you can call the DataBind() to see what happens in the SQL Server.

No comments:

Post a Comment