Binding FormView with Datatable, using ObjectDataSource VB.NET
How to Bind FormView with Datatable using ObjectDataSource VB.NET
Step 1) Create a class name it as DATATABLECRUD.VB
as shown below
Step 2) Goto design view of aspx page click on object data source
Step 3) Choose a Business Object i.e CRUD operations class in our case it is DATATABLECRUD.cs ==> webApplication1.DATATABLECRUD
Step 4) Use SelectMethod as GetStudentTable
as shown below
Step 5) Use UpdateMethod as UpdateRows
Step 6) InsertMethod as AddRows
Step 6) UpdateMethod as UpdateRows
These steps configures ObjectDataSource for CRUD Operations , here we choose DataTable as Data Source. In same way u can configure for MS-ACCESS/SQL-Server/MYSQL/ORACLE.
Step 4) Now add FormView to aspx page and select datasource (i.e created just now objectdatasourceid1)
as shown below
<asp:FormView ID="FormView1" Caption="<h1>FormView Data Operations/CRUD operations/Insert,Update,Delete Operations<h1>" DataKeyNames="SerialNo" runat="server" EmptyDataText="no data found" DataSourceID="ObjectDataSource1" GridLines="Both" AllowPaging="True" Width="295px" >
<ItemTemplate>
SerialNo:
<asp:Label ID="SerialNoLabel" runat="server" Text='<%# Bind("SerialNo") %>'></asp:Label><br />
StudentName:
<asp:Label ID="StudentNameLabel" runat="server" Text='<%# Bind("StudentName") %>'></asp:Label><br />
Course:
<asp:Label ID="CourseLabel" runat="server" Text='<%# Bind("Course") %>'></asp:Label><br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" />
</ItemTemplate>
<EditItemTemplate>
SerialNo:
<asp:TextBox ID="SerialNoTextBox" runat="server" Text='<%# Bind("SerialNo") %>' />
<br />
StudentName:
<asp:TextBox ID="StudentNameTextBox" runat="server" Text='<%# Bind("StudentName") %>' />
<br />
Course:
<asp:TextBox ID="CourseTextBox" runat="server" Text='<%# Bind("Course") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
SerialNo:
<asp:TextBox runat="server" ID="SerialNoTextBox" Text='<%# Bind("SerialNo") %>'></asp:TextBox> <br />
StudentName:
<asp:TextBox runat="server" Text='<%# Bind("StudentName") %>' ID="StudentNameTextBox"></asp:TextBox>
<br />
Course:
<asp:TextBox ID="CourseTextBox" runat="server" Text='<%# Bind("Course") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
</asp:FormView>
Step 5) Above step will automatically generate ItemTemplate, EditTemplates,InsertTemplate in FormView.
FormView SelectMethod OUTPUT
FormView Insert Operation Output
FormView Update Operation Output
FormView Delete Operation
Above article explains databind to FormView in ASP.NET USING c#, Object data Source used for Data Source Navigator. This process Simplifies coding . Makes easier to maintain.
Step 1) Create a class name it as DATATABLECRUD.VB
- Add 4 methods for CRUD Operations
- GetStudentDetails --> R for Get Records
- DeleteStudentInfo --> D for Delete Record
- UpdateStudentInfo --> U for Update recor
- AddStudentInfo --> C for Create Record
Add One more class called StudentInfo
- It has 3 public properties
- SerialNo int
- StudentName String
- Course String
U can add as many fields u want,because this is a demo ,Explains how to use Databinding in FormView using DataTable as DataSource,CRUD operations managed by ObjectDataSource.
//You can add DataAnnotations using System.ComponentModel.DataAnnotations; otherwise remove- Imports System
- Imports System.Collections.Generic
- Imports System.Linq
- Imports System.Web
- Imports System.Data
- Imports System.ComponentModel.DataAnnotations
- Namespace WebApplication1
- Public Class StudentInfo
- <Key> _
- Public Property SerialNo() As Int32
- Get
- Return m_SerialNo
- End Get
- Set
- m_SerialNo = Value
- End Set
- End Property
- Private m_SerialNo As Int32
- <Required(ErrorMessage := "Student Name Required")> _
- Public Property StudentName() As [String]
- Get
- Return m_StudentName
- End Get
- Set
- m_StudentName = Value
- End Set
- End Property
- Private m_StudentName As [String]
- <Required> _
- Public Property Course() As [String]
- Get
- Return m_Course
- End Get
- Set
- m_Course = Value
- End Set
- End Property
- Private m_Course As [String]
- End Class
- Public Class DTOP
- Private brandsTable As New DataTable()
- Private context As HttpContext = HttpContext.Current
- Private session As System.Web.SessionState.HttpSessionState = Nothing
- Public Sub New()
- AddDataTableColumns()
- AddDataTableRows()
- 'HttpContext context = HttpContext.Current;
- 'System.Web.SessionState.HttpSessionState session = context.Session;
- session = context.Session
- If session("tbl") Is Nothing Then
- session("tbl") = brandsTable
- Else
- brandsTable = DirectCast(session("tbl"), DataTable)
- End If
- End Sub
- Private Sub AddDataTableColumns()
- Dim ID As New DataColumn("SerialNo", GetType(System.Int32))
- ID.AutoIncrement = True
- ID.AutoIncrementSeed = 1000
- ID.AutoIncrementStep = 16
- Dim BrandName As New DataColumn("StudentName", GetType(System.String))
- BrandName.MaxLength = 100
- Dim BrandImage As New DataColumn("Course")
- BrandImage.DataType = GetType([String])
- 'typeof(System.Web.UI.WebControls.Image);
- brandsTable.Columns.Add(ID)
- brandsTable.Columns.Add(BrandName)
- brandsTable.Columns.Add(BrandImage)
- End Sub
- Private Sub AddDataTableRows()
- Dim row1 As DataRow = brandsTable.NewRow()
- row1(1) = "Bata"
- row1(2) = "C#"
- brandsTable.Rows.Add(row1)
- row1 = brandsTable.NewRow()
- row1(1) = "'Brands"
- row1(2) = "ASP.NET"
- brandsTable.Rows.Add(row1)
- row1 = brandsTable.NewRow()
- row1(1) = "MODULES"
- row1(2) = "JAVA"
- brandsTable.Rows.Add(row1)
- End Sub
- Public Function GetStudentTable() As List(Of StudentInfo)
- Dim studentquery =
- Return studentquery.ToList()
- End Function
- Public Function AddRows(sInfo As StudentInfo) As Boolean
- brandsTable = DirectCast(session("tbl"), DataTable)
- 'brandsTable = (DataTable)HttpContext.Current.Session["tbl"];
- Dim row As DataRow = brandsTable.NewRow()
- row(1) = sInfo.StudentName
- row(2) = sInfo.Course
- brandsTable.Rows.Add(row)
- HttpContext.Current.Session.Add("tbl", brandsTable)
- Return True
- End Function
- Public Function UpdateRows(sInfo As StudentInfo) As Boolean
- Dim bRet As Boolean = False
- brandsTable = DirectCast(session("tbl"), DataTable)
- Dim row As DataRow = brandsTable.AsEnumerable().SingleOrDefault(Function(s) s.Field(Of Int32)(0) = sInfo.SerialNo)
- If row IsNot Nothing Then
- row(1) = sInfo.StudentName
- row(2) = sInfo.Course
- bRet = True
- session("tbl") = brandsTable
- End If
- Return bRet
- End Function
- Public Function DeleteRows(sInfo As StudentInfo) As Boolean
- Dim bRet As Boolean = False
- brandsTable = DirectCast(session("tbl"), DataTable)
- Dim row As DataRow = brandsTable.AsEnumerable().SingleOrDefault(Function(s) s.Field(Of Int32)(0) = sInfo.SerialNo)
- If row IsNot Nothing Then
- brandsTable.Rows.Remove(row)
- session("tbl") = brandsTable
- bRet = True
- End If
- Return bRet
- End Function
- End Class
- End Namespace
Adding ObjectDataSoure to aspx page as shown below
Goto View --> Toolbox
as shown below
Step 2) Goto design view of aspx page click on object data source
Step 3) Choose a Business Object i.e CRUD operations class in our case it is DATATABLECRUD.cs ==> webApplication1.DATATABLECRUD
Step 4) Use SelectMethod as GetStudentTable
as shown below
Step 5) Use UpdateMethod as UpdateRows
Step 6) InsertMethod as AddRows
Step 6) UpdateMethod as UpdateRows
These steps configures ObjectDataSource for CRUD Operations , here we choose DataTable as Data Source. In same way u can configure for MS-ACCESS/SQL-Server/MYSQL/ORACLE.
Step 4) Now add FormView to aspx page and select datasource (i.e created just now objectdatasourceid1)
as shown below
<asp:FormView ID="FormView1" Caption="<h1>FormView Data Operations/CRUD operations/Insert,Update,Delete Operations<h1>" DataKeyNames="SerialNo" runat="server" EmptyDataText="no data found" DataSourceID="ObjectDataSource1" GridLines="Both" AllowPaging="True" Width="295px" >
<ItemTemplate>
SerialNo:
<asp:Label ID="SerialNoLabel" runat="server" Text='<%# Bind("SerialNo") %>'></asp:Label><br />
StudentName:
<asp:Label ID="StudentNameLabel" runat="server" Text='<%# Bind("StudentName") %>'></asp:Label><br />
Course:
<asp:Label ID="CourseLabel" runat="server" Text='<%# Bind("Course") %>'></asp:Label><br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" />
</ItemTemplate>
<EditItemTemplate>
SerialNo:
<asp:TextBox ID="SerialNoTextBox" runat="server" Text='<%# Bind("SerialNo") %>' />
<br />
StudentName:
<asp:TextBox ID="StudentNameTextBox" runat="server" Text='<%# Bind("StudentName") %>' />
<br />
Course:
<asp:TextBox ID="CourseTextBox" runat="server" Text='<%# Bind("Course") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
SerialNo:
<asp:TextBox runat="server" ID="SerialNoTextBox" Text='<%# Bind("SerialNo") %>'></asp:TextBox> <br />
StudentName:
<asp:TextBox runat="server" Text='<%# Bind("StudentName") %>' ID="StudentNameTextBox"></asp:TextBox>
<br />
Course:
<asp:TextBox ID="CourseTextBox" runat="server" Text='<%# Bind("Course") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
</asp:FormView>
Step 5) Above step will automatically generate ItemTemplate, EditTemplates,InsertTemplate in FormView.
FormView SelectMethod OUTPUT
FormView Insert Operation Output
FormView Update Operation Output
FormView Delete Operation
Above article explains databind to FormView in ASP.NET USING c#, Object data Source used for Data Source Navigator. This process Simplifies coding . Makes easier to maintain.
No comments:
Post a Comment