If you bind your application with database in traditional ASP program then you should use RecordSet and write lots of code for binding . But in ASP.NET Microsoft reduce lots of code for binding application. In ASP.NET you can use SqlDataSource for connecting front-end to back-end.
lets take an example , Add insert , update and delete functionality through SqlDataSource.
Step-1: Drop SqlDataSource control to the design window.
Step-2: Select Configure Data Source using Show Smart tag.
Step-3: Select Connection from DropdownList , expand connection string
now your connection string are
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True
Click Next to continue
Step-4: Select Advanced.. Button for adding insert,edit and delete functionality.
Output
lets take an example , Add insert , update and delete functionality through SqlDataSource.
Step-1: Drop SqlDataSource control to the design window.
Step-2: Select Configure Data Source using Show Smart tag.
Step-3: Select Connection from DropdownList , expand connection string
now your connection string are
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True
Click Next to continue
Step-4: Select Advanced.. Button for adding insert,edit and delete functionality.
If your database table contain one primary key then you can select "Generate INSERT, UPDATE, AND DELETE STATEMENT" And click to Ok button.
Step-5: After finishing process , Drop one DetailsView Control to the page. and Choose SqlDataSource1 for connecting database using ShowSmart tag.
Step-6: Select all given CheckBox
Complete code
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [footerex] WHERE [Id] = @Id" InsertCommand="INSERT INTO [footerex] ([namet], [Income]) VALUES (@namet, @Income)" SelectCommand="SELECT * FROM [footerex]" UpdateCommand="UPDATE [footerex] SET [namet] = @namet, [Income] = @Income WHERE [Id] = @Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="namet" Type="String" />
<asp:Parameter Name="Income" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="namet" Type="String" />
<asp:Parameter Name="Income" Type="Int32" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="namet" HeaderText="namet" SortExpression="namet" />
<asp:BoundField DataField="Income" HeaderText="Income" SortExpression="Income" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
</form>
</body>
</html>
<!DOCTYPE html>
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [footerex] WHERE [Id] = @Id" InsertCommand="INSERT INTO [footerex] ([namet], [Income]) VALUES (@namet, @Income)" SelectCommand="SELECT * FROM [footerex]" UpdateCommand="UPDATE [footerex] SET [namet] = @namet, [Income] = @Income WHERE [Id] = @Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="namet" Type="String" />
<asp:Parameter Name="Income" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="namet" Type="String" />
<asp:Parameter Name="Income" Type="Int32" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="namet" HeaderText="namet" SortExpression="namet" />
<asp:BoundField DataField="Income" HeaderText="Income" SortExpression="Income" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
</form>
</body>
</html>
Output
Tidak ada komentar:
Posting Komentar