Sunday 2 December 2012

Binding FormView and Datatable using Object DataSource in VB.NET

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
  •  Add 4 methods  for CRUD Operations
  1. GetStudentDetails   --> R for Get Records
  2. DeleteStudentInfo  --> D for Delete Record
  3. UpdateStudentInfo --> U for Update recor
  4. AddStudentInfo   -->         C for Create Record


  5. Add One more class called StudentInfo
  • It has 3 public properties
  1. SerialNo   int
  2. StudentName  String
  3. Course   String

  4. 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.

  5. //You can add DataAnnotations using System.ComponentModel.DataAnnotations; otherwise remove

  6. Imports System
  7. Imports System.Collections.Generic
  8. Imports System.Linq
  9. Imports System.Web
  10. Imports System.Data
  11. Imports System.ComponentModel.DataAnnotations
  12. Namespace WebApplication1
  13. Public Class StudentInfo
  14. <Key> _
  15. Public Property SerialNo() As Int32
  16. Get
  17. Return m_SerialNo
  18. End Get
  19. Set
  20. m_SerialNo = Value
  21. End Set
  22. End Property
  23. Private m_SerialNo As Int32
  24. <Required(ErrorMessage := "Student Name Required")> _
  25. Public Property StudentName() As [String]
  26. Get
  27. Return m_StudentName
  28. End Get
  29. Set
  30. m_StudentName = Value
  31. End Set
  32. End Property
  33. Private m_StudentName As [String]
  34. <Required> _
  35. Public Property Course() As [String]
  36. Get
  37. Return m_Course
  38. End Get
  39. Set
  40. m_Course = Value
  41. End Set
  42. End Property
  43. Private m_Course As [String]
  44. End Class
  45. Public Class DTOP
  46. Private brandsTable As New DataTable()
  47. Private context As HttpContext = HttpContext.Current
  48. Private session As System.Web.SessionState.HttpSessionState = Nothing

  49. Public Sub New()
  50. AddDataTableColumns()
  51. AddDataTableRows()
  52. 'HttpContext context = HttpContext.Current;
  53. 'System.Web.SessionState.HttpSessionState session = context.Session;
  54. session = context.Session
  55. If session("tbl") Is Nothing Then
  56. session("tbl") = brandsTable
  57. Else
  58. brandsTable = DirectCast(session("tbl"), DataTable)

  59. End If
  60. End Sub
  61. Private Sub AddDataTableColumns()
  62. Dim ID As New DataColumn("SerialNo", GetType(System.Int32))
  63. ID.AutoIncrement = True
  64. ID.AutoIncrementSeed = 1000
  65. ID.AutoIncrementStep = 16

  66. Dim BrandName As New DataColumn("StudentName", GetType(System.String))
  67. BrandName.MaxLength = 100

  68. Dim BrandImage As New DataColumn("Course")
  69. BrandImage.DataType = GetType([String])
  70. 'typeof(System.Web.UI.WebControls.Image);
  71. brandsTable.Columns.Add(ID)
  72. brandsTable.Columns.Add(BrandName)
  73. brandsTable.Columns.Add(BrandImage)

  74. End Sub
  75. Private Sub AddDataTableRows()
  76. Dim row1 As DataRow = brandsTable.NewRow()
  77. row1(1) = "Bata"
  78. row1(2) = "C#"
  79. brandsTable.Rows.Add(row1)
  80. row1 = brandsTable.NewRow()
  81. row1(1) = "'Brands"
  82. row1(2) = "ASP.NET"
  83. brandsTable.Rows.Add(row1)
  84. row1 = brandsTable.NewRow()
  85. row1(1) = "MODULES"
  86. row1(2) = "JAVA"
  87. brandsTable.Rows.Add(row1)

  88. End Sub

  89. Public Function GetStudentTable() As List(Of StudentInfo)
  90. Dim studentquery = 
  91. Return studentquery.ToList()
  92. End Function
  93. Public Function AddRows(sInfo As StudentInfo) As Boolean


  94. brandsTable = DirectCast(session("tbl"), DataTable)
  95. 'brandsTable = (DataTable)HttpContext.Current.Session["tbl"];
  96. Dim row As DataRow = brandsTable.NewRow()
  97. row(1) = sInfo.StudentName
  98. row(2) = sInfo.Course
  99. brandsTable.Rows.Add(row)
  100. HttpContext.Current.Session.Add("tbl", brandsTable)
  101. Return True
  102. End Function
  103. Public Function UpdateRows(sInfo As StudentInfo) As Boolean
  104. Dim bRet As Boolean = False
  105. brandsTable = DirectCast(session("tbl"), DataTable)
  106. Dim row As DataRow = brandsTable.AsEnumerable().SingleOrDefault(Function(s) s.Field(Of Int32)(0) = sInfo.SerialNo)
  107. If row IsNot Nothing Then
  108. row(1) = sInfo.StudentName
  109. row(2) = sInfo.Course
  110. bRet = True
  111. session("tbl") = brandsTable
  112. End If
  113. Return bRet
  114. End Function
  115. Public Function DeleteRows(sInfo As StudentInfo) As Boolean
  116. Dim bRet As Boolean = False
  117. brandsTable = DirectCast(session("tbl"), DataTable)
  118. Dim row As DataRow = brandsTable.AsEnumerable().SingleOrDefault(Function(s) s.Field(Of Int32)(0) = sInfo.SerialNo)
  119. If row IsNot Nothing Then
  120. brandsTable.Rows.Remove(row)
  121. session("tbl") = brandsTable
  122. bRet = True
  123. End If
  124. Return bRet
  125. End Function

  126. End Class
  127. End Namespace


  128. Adding ObjectDataSoure to aspx page as shown below


    Goto  View --> Toolbox
   In Toolbox Under data category  select ObjectDataSource , just drag/double click into aspx 
  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" />
                        &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
                        &nbsp;<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" />
                        &nbsp;<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" />
                &nbsp;<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