Detailsview Insert

The GridView control in ASP.NET allows you to display all records from a data source control simultaneously, presenting them in a tabular format. On the other hand, the DetailsView control renders an HTML table and displays one row from a data source at a time.

DetailsView control

One notable feature of the DetailsView control is its support for inserting new records. To enable the insert command, you can add a SqlDataSource control to the page and configure it to include the necessary INSERT statements. By setting the AutoGenerateInsertButton property of the DetailsView control to true, an insert button will automatically be generated, providing users with a convenient way to add new records.

AutoGenerateInsertButton="true"
detailsview-insert

Each field within the DetailsView control has an InsertVisible property that can be modified through the Fields dialog box. By setting this property to False, you can choose to hide specific fields during the process of adding a new record. This allows for greater flexibility in determining which fields are visible and editable when inserting data into the DetailsView. The following ASP.NET program shows how to insert data in a database using DetailsView control.

Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DetailsView id="DetailsView1" DataSourceID="SqlDataSource1" AllowPaging ="true" Runat="server" AutoGenerateInsertButton="true" /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>" SelectCommand="select * from stores" InsertCommand="INSERT [stores] ([stor_id],[stor_name],[stor_address],[city],[state],[zip]) VALUES (@stor_id,@stor_name,@stor_address,@city,@state,@zip)" /> </div> </form> </body> </html>

Conclusion

The control's ability to display one record at a time and its support for customizing the insert process provide a powerful toolset for building interactive web applications.