ARTICLE

FormView control in ASP. NET using VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET June 24, 2011
In this article we will learn how to use FormView control to display a single record from a datasource.
Download Files:
 
Reader Level:

The FormView control is used to display a single record from a data source in a table. FormView control Display Data with the FormView Control. The FormView control is similar to the DetailsView. Rather than using field tags like <asp:BoundField>, the FormView specifies its data using template tags like <ItemTemplate>. Use the itemTemplate to display database records. The FormView control allows you to edit, delete, and insert records.

  1. Open Visual Studio.
  2. Add a webForm to your website, name it formView.aspx.

Now drag and drop a FormView control from the Toolbox on the form.

fv1.gif

Figure1

The ASP. NET code for the DataView control.

<form id="form1" runat="server">

   <div style="width: 262px">

          <asp:FormView ID="FormView1" runat="server" CellPadding="4"

           DataSourceID="SqlDataSource1" ForeColor="#333333" Height="137px"

           Width="380px" Caption="User Detail" CaptionAlign="Top">

           <EditRowStyle />

              <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

              <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

           <ItemTemplate>

                UserId:

               <asp:Label ID ="userid" runat ="server" Text ='<%# Bind("UserId") %>'></asp:Label>

               <br />

                Username:

               <asp:Label ID ="username" runat ="server" Text ='<%# Bind("Username") %>'></asp:Label>

               <br />

                Email:

                <asp:Label ID ="useremail" runat ="server" Text ='<%# Bind("Email") %>'></asp:Label>

               <br />

                Address:

                <asp:Label ID ="useraddress" runat ="server" Text ='<%# Bind("Address") %>'></asp:Label>

               <br />

           </ItemTemplate>

              <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />

               <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />         

       </asp:FormView>

      

 

Now The following code snippet shows how to connect SQL database.

 

Imports System.Data.SqlClient

Public Class WebForm1

    Inherits System.Web.UI.Page

    Dim conn As New SqlConnection("Data Source=.;uid=sa;pwd=Password$2;database=master")

    Dim ad As New SqlDataAdapter()

    Dim cmd As New SqlCommand()

    Dim dataTable As DataTable

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        dataTable = New DataTable()

        cmd.Connection = conn

        cmd.CommandText = "SELECT * FROM userinfo1"

        ad = New SqlDataAdapter(cmd)

        ad.Fill(dataTable)

        FormView1.DataSource = dataTable

        FormView1.DataBind()

    End Sub

End Class

 

Now run the application.


fv2.gif

 

Figure2

 

Paging, Border Style and caption.

 

The ASP. NET code for DataView control property.

 

<asp:FormView ID="FormView1" runat="server" CellPadding="4"

 ForeColor="#333333" Height="137px"

Width="380px" AllowPaging="True" BorderColor="#FF3399" BorderStyle="Solid"

Caption="User Detail" CaptionAlign="Top">

 

Now run the application again and test it.


fv3.gif

 

Figure3

 

Note: You can see a demo of this article by downloading this application.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor