Membuat add, update, delete dengan Gridview dan Detailview tanpa code behind

Buat table dalam database dengan query dibawah

CREATE TABLE [dbo].[AddressBook](
	[AddressId] [int] IDENTITY(1,1) NOT NULL,
	[FullName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	[Address] [nvarchar](150) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	[PhoneNumber] [nvarchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

Buat ConnectionStrings seperti artikel sebelumnya ,…

didalam aspx :

1.Buat SqlDataSource :

 <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:conn %>"
    ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT AddressId, FullName, Address, PhoneNumber FROM AddressBook ORDER BY FullName" 
    UpdateCommand="UPDATE AddressBook SET FullName=@FullName, Address=@Address, PhoneNumber=@PhoneNumber WHERE AddressId=@AddressId"
    DeleteCommand="DELETE FROM AddressBook WHERE AddressId=@AddressId"
    InsertCommand="INSERT INTO AddressBook (FullName, Address, PhoneNumber) VALUES (@FullName, @Address, @PhoneNumber)">
</asp:SqlDataSource>

2. Buat GridView untuk Update, Delete Data, seperti di bawah:

<asp:GridView ID="gridPhoneBook" runat="server" 
    DataKeyNames="AddressId" 
    AutoGenerateColumns="False" 
    AllowPaging="True" 
    Caption="Address Book" 
    DataSourceID="SqlDataSource1" 
    EmptyDataText="No data to display"> 
    <Columns> 
        <asp:BoundField DataField="FullName" HeaderText="Full Name" />
        <asp:BoundField DataField="Address" HeaderText="Address" /> 
        <asp:BoundField DataField="PhoneNumber" HeaderText="Phone Number" /> 
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
    </Columns> 
</asp:GridView> 

3. Buat Listview untuk Add/Insert Data, seperti di bawah:

<asp:DetailsView ID="detailPhoneBook" runat="server" 
    AutoGenerateRows="False" DataKeyNames="AddressId"
    DataSourceID="SqlDataSource1" 
    Caption="Details" 
    AllowPaging="true">
    <Fields>
        <asp:BoundField DataField="FullName" HeaderText="Full Name" />
        <asp:BoundField DataField="Address" HeaderText="Address" />
        <asp:BoundField DataField="PhoneNumber" HeaderText="Phone Number" />
        <asp:CommandField ShowInsertButton="True" />
    </Fields>
    <EmptyDataTemplate>
        <asp:LinkButton ID="LinkButton1" 
             runat="server"
             CausesValidation="False"
             CommandName="New"
             Text="New">
        </asp:LinkButton>
    </EmptyDataTemplate>
</asp:DetailsView>

Selamat Mencoba ,… ^.^

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s