FormView: Select, Insert, update and delete data in Form View using SqlDataSource.

Aman Sharma
Introduction: Form View data control can be used to Display, Insert, Delete and update data. We can insert data to database using FormView and also update or delete data. There is no need to write code to do this. We can achieve this by using SQLDataSource control. We write Insert, Delete, update command in SQLDataSource and pass parameter.

In this article I will explain how to display, insert and Update data using Form View Control and SQLDataSource.

Steps to display Data in FormView:
1.       Create webpage in your application and add Form view control.
2.       Now Customize ItemTemplate for display data and EditItemTemplate to edit data and InsertItemTemplate to insert data.
3.       Now Add SqlDataSource control to webform and connect it to your database and write queries for select, Insert, update and delete.

In this article I have explained how to create queries for select, insert, delete and update in SqlDataSource control.

Create table in database:
Create Table “Student_Info”  in Database “Blog” and Enter some data to display.

Column Name

ASP.NET Source Code

Form View Design:

<asp:FormView ID="FormView1" runat="server" Width="300px"
     DataKeyNames="Student_Id"  AllowPaging="True" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" GridLines="Vertical" ForeColor="Black"  >
                        <EditRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <FooterStyle BackColor="#CCCCCC" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <table border="1" style="width:100%"><tr><td> Student_Id:</td><td> <asp:Label ID="Student_IdLabel" runat="server" Text='<%# Eval("Student_Id") %>' /></td></tr>
           <tr><td> Student_name:</td><td><asp:Label ID="Student_nameLabel" runat="server" Text='<%# Bind("Student_name") %>' /></td></tr>
            <tr><td> Age:</td><td> <asp:Label ID="AgeLabel" runat="server" Text='<%# Bind("Age") %>' /></td></tr>
           <tr><td>Class:</td><td> <asp:Label ID="ClassLabel" runat="server" Text='<%# Bind("Class") %>' /></td></tr>
           <tr><td></td><td><br /></td></tr>
                <tr><td> </td><td> <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />&nbsp; &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" /> &nbsp;&nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" </td></tr>/></table>                
            <table border="1" style="width:100%">
            <tr><td> Student_Id:</td><td> <asp:Label ID="Student_IdLabel1" runat="server" Text='<%# Eval("Student_Id") %>' /></td></tr>          
           <tr><td> Student_name:</td><td>  <asp:TextBox ID="Student_nameTextBox" runat="server" Text='<%# Bind("Student_name") %>' /></td></tr>
            <tr><td>  Age:</td><td> <asp:TextBox ID="AgeTextBox" runat="server" Text='<%# Bind("Age") %>' /></td></tr>
            <tr><td> Class:</td><td> <asp:TextBox ID="ClassTextBox" runat="server" Text='<%# Bind("Class") %>' /></td></tr>
            <tr><td></td><td><asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />&nbsp;&nbsp;
            &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /></td></tr></table>
            <table border="1" style="width:100%">
            <tr><td> Student_name:</td><td> <asp:TextBox ID="Student_nameTextBox" runat="server" Text='<%# Bind("Student_name") %>' /></td></tr>
          <tr><td> Age:</td><td> <asp:TextBox ID="AgeTextBox" runat="server" Text='<%# Bind("Age") %>' /></td></tr>
           <tr><td> Class:</td><td><asp:TextBox ID="ClassTextBox" runat="server" Text='<%# Bind("Class") %>' /></td></tr>
           <tr><td></td><td> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />&nbsp;&nbsp;
            &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /></td></tr></table>

Customize SqlDataSource Control:

              <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BlogConnectionString2 %>"
DeleteCommand="DELETE FROM Student_info WHERE (Student_Id = @Student_ID)" InsertCommand="INSERT INTO Student_info(Student_name, Age, Class) VALUES (@Student_Name, @Age, @Class)"
SelectCommand="SELECT [Student_Id], [Student_name], [Age], [Class] FROM [Student_info] ORDER BY [Student_name]"
UpdateCommand="UPDATE Student_info SET Student_name = @student_name, Age = @Age, Class = @class WHERE (Student_Id = @Student_id)">
                        <asp:Parameter Name="Student_ID" />
                        <asp:Parameter Name="Student_Name" />
                        <asp:Parameter Name="Age" />
                        <asp:Parameter Name="Class" />
                        <asp:Parameter Name="student_name" />
                        <asp:Parameter Name="Age" />
                        <asp:Parameter Name="class" />
                        <asp:Parameter Name="Student_id" />

Post a Comment

Post a Comment

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !