ASP.NET(vb.net) & ObjectDataSource - asp:ObjectDataSource - The ObjectDataSource represents a business object that provides data to data-bound controls in multitier Web application architectures.
ShotDev Focus:
- ASP.NET(vb.net) & ObjectDataSource - asp:ObjectDataSource
Tag Control :
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"></asp:ObjectDataSource>
On current project Right Click -> Add New Item…
Add new item DataSet.
Right Click on DataSet Designer -> Add -> TableAdapter
TableAdapter Configuration Wizard -> Choose Data Connection or Create New Connection.
Choose Database Source.
Click Next for go to next Step.
Save a ConnectionString.
Enter a SQL Statement or Query.
Choose Method to Generate. and Click Finish to Save
TableAdapter From creating.
Drag ObjectDataSource to ASP.NET Web Form. on ObjectDataSource Tasks -> Click Configure Data Source…
Choose a Business Object.
Choose Debine Data Methods (SELECT,UPDATE,INSERT,DELETE).
DragĀ GridView Control to ASP.NET Web Form and Choose Data Source Form ObjectDataSource.
Example Code
Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!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 runat="server"> <title>ShotDev.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <div> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="myCustomerTableAdapters.customerTableAdapter" UpdateMethod="Update"> <DeleteParameters> <asp:Parameter Name="Original_CustomerID" Type="String" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="Name" Type="String" /> <asp:Parameter Name="Email" Type="String" /> <asp:Parameter Name="CountryCode" Type="String" /> <asp:Parameter Name="Budget" Type="Decimal" /> <asp:Parameter Name="Used" Type="Decimal" /> <asp:Parameter Name="Original_CustomerID" Type="String" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="CustomerID" Type="String" /> <asp:Parameter Name="Name" Type="String" /> <asp:Parameter Name="Email" Type="String" /> <asp:Parameter Name="CountryCode" Type="String" /> <asp:Parameter Name="Budget" Type="Decimal" /> <asp:Parameter Name="Used" Type="Decimal" /> </InsertParameters> </asp:ObjectDataSource> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="ObjectDataSource1"> <Columns> <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /> <asp:BoundField DataField="CountryCode" HeaderText="CountryCode" SortExpression="CountryCode" /> <asp:BoundField DataField="Budget" HeaderText="Budget" SortExpression="Budget" /> <asp:BoundField DataField="Used" HeaderText="Used" SortExpression="Used" /> </Columns> </asp:GridView> </div> </form> </body> </html>
Default.aspx.vb
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!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 runat="server"> <title>ShotDev.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <div> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="myCustomerTableAdapters.customerTableAdapter" UpdateMethod="Update"> <DeleteParameters> <asp:Parameter Name="Original_CustomerID" Type="String" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="Name" Type="String" /> <asp:Parameter Name="Email" Type="String" /> <asp:Parameter Name="CountryCode" Type="String" /> <asp:Parameter Name="Budget" Type="Decimal" /> <asp:Parameter Name="Used" Type="Decimal" /> <asp:Parameter Name="Original_CustomerID" Type="String" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="CustomerID" Type="String" /> <asp:Parameter Name="Name" Type="String" /> <asp:Parameter Name="Email" Type="String" /> <asp:Parameter Name="CountryCode" Type="String" /> <asp:Parameter Name="Budget" Type="Decimal" /> <asp:Parameter Name="Used" Type="Decimal" /> </InsertParameters> </asp:ObjectDataSource> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="ObjectDataSource1"> <Columns> <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /> <asp:BoundField DataField="CountryCode" HeaderText="CountryCode" SortExpression="CountryCode" /> <asp:BoundField DataField="Budget" HeaderText="Budget" SortExpression="Budget" /> <asp:BoundField DataField="Used" HeaderText="Used" SortExpression="Used" /> </Columns> </asp:GridView> </div> </form> </body> </html>
Screenshot
2marshall…
…