GridView + SQLDataSource is what you need:
Create a SQLDataSource and specify the connection string and the InsertCommand.
<asp:sqldatasource ID="customSqlDataSource" runat="server"
ConnectionString="theconnectionstring"
InsertCommand="Insert into mytable(col1, col2, col3) values (@val1, @val2, @val3)"
</asp:sqldatasource>
In your Gridview,
a) set the ShowFooter property to true and
b) Create an OnRowCommand event handler.
c) Also specify the Empty data template as shown below.
d) Set the DataSourceID to the SQLDataSource created in above step.
<asp:GridView ID="myGridView" runat="server" DataSourceID="customSqlDataSource" ShowFooter="true"
onrowcommand="myGridView_RowCommand">
<Columns>
...
<EmptyDataTemplate>
type first val:<asp:TextBox runat="server" ID="firstcol" />
type seond val:<asp:TextBox runat="server" ID="secondcol" />
type third val:<asp:TextBox runat="server" ID="thirdcol" />
<asp:Button runat="server" ID="insertdata" CommandName="insert" Text="Insert data"/>
</EmptyDataTemplate>
</asp:GridView>
Handle the command that you set in the RowCommand event of the Gridview.
private List<SqlParameter> params = new List<SqlParameter>();
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "insert")
{
TextBox firstVal = myGridView.Controls[0].Controls[0].FindControl("firstcol") as TextBox;
..
SqlParameter fv = new SqlParameter("@val1", SqlDbType.VarChar, 30);
fv .Direction = ParameterDirection.Input;
fv.Value = FirstName.Text;
params.Add(fv);
... and so on
customSqlDataSource.Insert();
}
}
Add those params from the params collection collected in the rowcommand event to the sqldatasource command. Sample below:
protected void customsqldatasource_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
foreach (SqlParameter prm in params)
e.Command.Parameters.Add(prm);
}
That should be it.