Thursday, November 1, 2012

Duplication Validation with Datatable in vb.net


  Private Function EXSISTdataVALIDATION() As Boolean
        Dim bolformValidate As Boolean = True
        Dim colERRCollection As New Collection
        Dim colControls As New Collection

        Dim dsValidate As DataSet

        Dim dtTempData As New DataTable
        Dim strSearch As String = ""
        Dim drData As DataRow
        Dim dvData As DataView
        dtTempData.Columns.Add("Cus_ID_N", GetType(Int64))
        dtTempData.Columns.Add("Mjm_ID_N", GetType(Int64))
        dtTempData.Columns.Add("Epm_ID_N", GetType(Int64))
        dtTempData.Columns.Add("Gem_ID_N", GetType(Int64))
        dtTempData.Columns.Add("Sos_Date_D", GetType(DateTime))
        dtTempData.Columns.Add("Emp_ID_N", GetType(Int64))
        If dgvEdit.Rows.Count > 0 Then
            For intRcnt As Integer = 0 To dgvEdit.RowCount - 1
                drData = dtTempData.NewRow
                With dgvEdit.Rows(intRcnt)
                    If Convert.ToString(.Cells("colCus_ID_N").Value) <> "" Then drData("Cus_ID_N") = Convert.ToInt64(.Cells("colCus_ID_N").Value)
                    If Convert.ToString(.Cells("colMjm_ID_N").Value) <> "" Then drData("Mjm_ID_N") = Convert.ToInt64(.Cells("colMjm_ID_N").Value)
                    If Convert.ToString(.Cells("colEquipment").Value) <> "" Then drData("Epm_ID_N") = Convert.ToInt64(.Cells("colEquipment").Value)
                    If Convert.ToString(.Cells("colServiceActivity").Value) <> "" Then drData("Gem_ID_N") = Convert.ToInt64(.Cells("colServiceActivity").Value)
                    If Convert.ToString(.Cells("colScd_Date_D").Value) <> "" Then drData("Sos_Date_D") = CDate(Format(.Cells("colScd_Date_D").Value, "dd-MMM-yyyy"))
                    If Convert.ToString(.Cells("colEmp_ID_N").Value) <> "" Then drData("Emp_ID_N") = Convert.ToInt64(.Cells("colEmp_ID_N").Value)
                End With
                dtTempData.Rows.Add(drData)
            Next
            For intRcnt As Integer = 0 To dgvEdit.RowCount - 1
                drData = dtTempData.NewRow
                strSearch = " 1=1 "
                With dgvEdit.Rows(intRcnt)
                    If Convert.ToString(.Cells("colCus_ID_N").Value) <> "" Then strSearch = strSearch & " and Cus_ID_N = " & Convert.ToString(.Cells("colCus_ID_N").Value)
                    If Convert.ToString(.Cells("colMjm_ID_N").Value) <> "" Then  strSearch = strSearch & " And Mjm_ID_N = " & Convert.ToString(.Cells("colMjm_ID_N").Value)
                    If Convert.ToString(.Cells("colEquipment").Value) <> "" Then strSearch = strSearch & " And Epm_ID_N = " & Convert.ToString(.Cells("colEquipment").Value)
                    If Convert.ToString(.Cells("colServiceActivity").Value) <> "" Then strSearch = strSearch & " And Gem_ID_N = " & Convert.ToString(.Cells("colServiceActivity").Value)
                    If Convert.ToString(.Cells("colScd_Date_D").Value) <> "" Then strSearch = strSearch & " And Sos_Date_D = '" & Format(.Cells("colScd_Date_D").Value, "dd-MMM-yyyy") & "'"
                    If Convert.ToString(.Cells("colEmp_ID_N").Value) <> "" Then strSearch = strSearch & " And Emp_ID_N = " & Convert.ToString(.Cells("colEmp_ID_N").Value)
                End With
                'If Convert.ToString(dgvEdit.Rows(intRcnt).Cells("colScd_ID_N").Value).Trim <> "" Then
                '=======================
                dsValidate = New DataSet
                dsValidate = ServiceInterfaceProxy.GetSrvServiceSchedule(CDate(Format(dgvEdit.Rows(intRcnt).Cells("colScd_Date_D").Value, "dd-MMM-yyyy")), CDate(Format(dgvEdit.Rows(intRcnt).Cells("colScd_Date_D").Value, "dd-MMM-yyyy")), gblintOutletID.ToString(), intUserID.ToString())
                Dim strSearchcolScd_ID_N As String = ""
                If Convert.ToString(dgvEdit.Rows(intRcnt).Cells("colScd_ID_N").Value).Trim <> "" Then
                    strSearchcolScd_ID_N = " And Scd_ID_N <> " & Convert.ToString(dgvEdit.Rows(intRcnt).Cells("colScd_ID_N").Value).Trim
                End If
                For Each drRow As DataRow In dsValidate.Tables(0).Select(strSearch & strSearchcolScd_ID_N)
                    colControls.Add(New MissingFieldException("Duplicate Schedule of Sl No : " & (intRcnt + 1).ToString))
                    colERRCollection.Add(dgvEdit)
                Next
                '=======================
                'Else
                If dtTempData.Rows.Count > 0 Then
                    dvData = New DataView(dtTempData, strSearch, "", DataViewRowState.CurrentRows)
                    If dvData.Count > 1 Then
                        colControls.Add(New MissingFieldException("Duplicate Schedule of Sl No : " & (intRcnt + 1).ToString))
                        colERRCollection.Add(dgvEdit)
                    End If
                End If
                '=======================
                'End If
            Next
        Else
        End If
        If colERRCollection.Count > 0 Then
            bolformValidate = False
            ShowMessagebox(colControls, Me)
            If colERRCollection.Count > 0 Then
                If colERRCollection.Item(1).GetType.Name = "Usr_AlphaNumeric" Then
                    Dim objControl As Usr_AlphaNumeric.Usr_AlphaNumeric
                    objControl = CType(colERRCollection.Item(1), Usr_AlphaNumeric.Usr_AlphaNumeric)
                    objControl.Focus()
                ElseIf colERRCollection.Item(1).GetType.Name = "DateTimePicker" Then
                    Dim objControl As DateTimePicker
                    objControl = CType(colERRCollection.Item(1), DateTimePicker)
                    objControl.Focus()
                ElseIf colERRCollection.Item(1).GetType.Name = "DataGridViewTextBoxCell" Then
                    Dim objDataGridViewTextBoxCell As New DataGridViewTextBoxCell
                    objDataGridViewTextBoxCell = CType(colERRCollection.Item(1), DataGridViewTextBoxCell)
                    dgvEdit.Focus()
                ElseIf colERRCollection.Item(1).GetType.Name = "DataGridViewComboBoxCell" Then
                    Dim objDataGridViewComboBoxCell As New DataGridViewComboBoxCell
                    objDataGridViewComboBoxCell = CType(colERRCollection.Item(1), DataGridViewComboBoxCell)
                    dgvEdit.Focus()
                ElseIf colERRCollection.Item(1).GetType.Name = "DataGridViewCell" Then
                    Dim objDataGridViewTextBoxCell As DataGridViewCell
                    objDataGridViewTextBoxCell = CType(colERRCollection.Item(1), DataGridViewTextBoxCell)
                    dgvEdit.Focus()
                Else
                    Dim objControl As Control
                    objControl = CType(colERRCollection.Item(1), Control)
                    objControl.Focus()
                End If
            End If
        End If
        Return bolformValidate

    End Function

Tuesday, September 4, 2012

• ADO.NET DataView - Related Contents


·         ADO.NET DataView - Related Contents
·         How to create a DataView
·         How to Sort DataView
·         How to Filter DataView
·         How to Find a rows in DataView
·         How to update rows in DataView
·         How to delete rows in DataView


How to create a DataView
 DataView provides different views of the data stored in a DataTable. DataView can be used to sort, filter, and search in a DataTable , additionally we can add new rows and modify the content in a DataTable. DataViews can be created and configured both design time and run time . Changes made to a DataView affect the underlying DataTable automatically, and changes made to the underlying DataTable automatically affect any DataView objects that are viewing the DataTable.
We can create DataView in two different ways. We can use the DataView constructor, or you can create a reference to the DefaultView property of the DataTable. The DataView constructor can be empty, or it can take either a DataTable as a single argument, or a DataTable along with filter criteria, sort criteria, and a row state filter.
dataView = dataSet.Tables(0).DefaultView
The following source code shows how to create a DataView in VB.NET. Create a new VB.NET project and drag a DataGridView and a Button on default Form Form1 , and copy and paste the following Source Code on button click event.

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim adapter As New SqlDataAdapter
        Dim ds As New DataSet
        Dim dv As DataView
        Dim sql As String
               connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        sql = "Select  * from product"
        connection = New SqlConnection(connetionString)
        Try
            connection.Open()
            command = New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.Fill(ds, "Create DataView")
            adapter.Dispose()
            command.Dispose()
            connection.Close()

            dv = ds.Tables(0).DefaultView
            DataGridView1.DataSource = dv

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
How to sort DataView
The DataView provides different views of the data stored in a DataTable. DataView can be used to sort, filter, and search data in a DataTable , additionally we can add new rows and modify the content in a DataTable. DataViews can be created and configured at both design time and run time . Changes made to a DataView affect the underlying DataTable automatically, and changes made to the underlying DataTable automatically affect any DataView objects that are viewing that DataTable.
We can sort single or multiple fields in a DataView , also we can specify the sort order like ASC (ascending) and DESC (descending) . The following example creates a View and apply sort on the column Product_Price in descending order. Create a new VB.NET project and drag aDataGridView and a Button on default Form Form1 , and copy and paste the following Source Code on button click event.

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim adapter As New SqlDataAdapter
        Dim ds As New DataSet
        Dim dv As DataView
        Dim sql As String
               connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        sql = "Select  * from product"
        connection = New SqlConnection(connetionString)
        Try
            connection.Open()
            command = New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.Fill(ds, "Sort DataView")
            adapter.Dispose()
            command.Dispose()
            connection.Close()

            dv = New DataView(ds.Tables(0), "Product_Price > 100", "Product_Price Desc", DataViewRowState.CurrentRows)
            DataGridView1.DataSource = dv

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class


How to Filter DataView
DataView enables you to create different views of the data stored in a DataTable. That is we can customize the views of data from a DataTable. DataView can be used to sort, filter, and search a DataTable , additionally we can add new rows and modify the content in a DataTable. We can create DataView in two ways. Either we can use the DataView constructor, or we can create a reference to the DefaultView property of the DataTable. Also we can create multiple DataViews for any given DataTable.
The following source code creates a view that shows all Product Details where the Product Price less than 500. Create a new VB.NET project and drag a DataGridView and a Button on default Form Form1 , and copy and paste the following Source Code on button click event.

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim adapter As New SqlDataAdapter
        Dim ds As New DataSet
        Dim dv As DataView
        Dim sql As String
               connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        sql = "Select  * from product"
        connection = New SqlConnection(connetionString)
        Try
            connection.Open()
            command = New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.Fill(ds, "Filter DataView")
            adapter.Dispose()
            command.Dispose()
            connection.Close()

            dv = New DataView(ds.Tables(0), "Product_Price < = 500", "Product_Name", DataViewRowState.CurrentRows)
            DataGridView1.DataSource = dv

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
How to Find a row in DataView
The DataView provides different views of the data stored in a DataTable. The constructor for the DataView class initializes a new instance of the DataView class and accepts the DataTable as an argument. We can create DataView in two ways. Either we can use the DataView constructor, or we can create a reference to the DefaultView property of the DataTable. We can create multiple DataViews for any given DataTable.
We can search in a DataView according to the sort key values by using the Find method . The Find method returns an integer with the index of the DataRowView that matches the search criteria. If more than one row matches the search criteria, only the index of the first matching DataRowView is returned. If no matches are found, Find returns -1
Dim index As Integer = DataView.Find("Product5")

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim adapter As New SqlDataAdapter
        Dim ds As New DataSet
        Dim dv As DataView
        Dim sql As String
               connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        sql = "Select  * from product"
        connection = New SqlConnection(connectionString)
        Try
            connection.Open()
            command = New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.Fill(ds, "Find Row DataView")
            adapter.Dispose()
            command.Dispose()
            connection.Close()

            dv = New DataView(ds.Tables(0))
            dv.Sort = "Product_Name"
            Dim index As Integer = dv.Find("Product5")

            If index = -1 Then
                MsgBox("Item Not Found")
            Else
                MsgBox(dv(index)("Product_id").ToString() & "  " & dv(index)("Product_Name").ToString())
            End If

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
How to add new rows in DataView
The DataView provides different views of the data stored in a DataTable. DataViews can be created and configured at both design time and run time . We can create DataView in two ways. Either we can use the DataView constructor, or we can create a reference to the DefaultView property of the DataTable. We can create multiple DataViews for any given DataTable.
We can add new rows in the DataView using AddNew method in the DataView. The following source code shows how to add new row in a DataView . Create a new VB.NET project and drag a DataGridView and a Button on default Form Form1 , and copy and paste the following Source Code on button click event.

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim adapter As New SqlDataAdapter
        Dim ds As New DataSet
        Dim dv As DataView
        Dim sql As String
               connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        sql = "Select  * from product"
        connection = New SqlConnection(connetionString)
        Try
            connection.Open()
            command = New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.Fill(ds, "Add New")
            adapter.Dispose()
            command.Dispose()
            connection.Close()

            dv = New DataView(ds.Tables(0))
            Dim newRow As DataRowView = dv.AddNew()
            newRow("Product_ID") = 7
            newRow("Product_Name") = "Product 7"
            newRow("Product_Price") = 111
            newRow.EndEdit()
            dv.Sort = "product_id"

            DataGridView1.DataSource = dv

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
How to update rows in DataView
The DataView provides different views of the data stored in a DataTable. DataView can be used to sort, filter, and search a DataTable , additionally we can add new rows and modify the content in a DataTable. We can create DataView in two ways. Either we can use the DataView constructor, or we can create a reference to the DefaultView property of the DataTable. Also we can create multiple DataViews for any given DataTable.
We can update the data in a DataView . The following source code shows how to update data in a DataView . Create a new VB.NET project and drag a DataGridView and a Button on default Form Form1 , and copy and paste the following Source Code on button click event.

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim adapter As New SqlDataAdapter
        Dim ds As New DataSet
        Dim dv As DataView
        Dim sql As String
               connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        sql = "Select  * from product"
        connection = New SqlConnection(connetionString)
        Try
            connection.Open()
            command = New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.Fill(ds, "Update")
            adapter.Dispose()
            command.Dispose()
            connection.Close()

            dv = New DataView(ds.Tables(0), "", "Product_Name", DataViewRowState.CurrentRows)
            Dim index As Integer = dv.Find("Product5")

            If index = -1 Then
                MsgBox("Product not found")
            Else
                dv(index)("Product_Name") = "Product11"
                MsgBox("Product Updated !")
            End If

            DataGridView1.DataSource = dv

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
How to delete rows in DataView
The DataView provides different views of the data stored in a DataTable. DataView can be used to sort, filter, and search data in a DataTable , additionally we can add new rows and modify the content in a DataTable. Also we can delete the data from the DataView . The following source code shows how to delete the data from DataView.

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim adapter As New SqlDataAdapter
        Dim ds As New DataSet
        Dim dv As DataView
        Dim sql As String
               connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        sql = "Select  * from product"
        connection = New SqlConnection(connetionString)
        Try
            connection.Open()
            command = New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.Fill(ds, "Delete Row")
            adapter.Dispose()
            command.Dispose()
            connection.Close()

            dv = New DataView(ds.Tables(0), "", "Product_ID", DataViewRowState.CurrentRows)
            dv.Table.Rows(3).Delete()
            DataGridView1.DataSource = dv

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
How to create a new DataTable from DataView
 DataView provides different views of the data stored in a DataTable. That is we can customize the views of data from a DataTable. Changes made to a DataView affect the underlying DataTable automatically, and changes made to the underlying DataTable automatically affect any DataView objects that are viewing that DataTable. We can create a new DataTable from the DataView . We can use the ToTable method to copy all the rows and columns, or a subset of the data into a new DataTable.

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim adapter As New SqlDataAdapter
        Dim ds As New DataSet
        Dim dv As DataView
        Dim sql As String
               connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        sql = "Select  * from product"
        connection = New SqlConnection(connetionString)
        Try
            connection.Open()
            command = New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.Fill(ds, "Copy to DataTable")
            adapter.Dispose()
            command.Dispose()
            connection.Close()

            dv = New DataView(ds.Tables(0), "Product_Price <= 200", "Product_ID", DataViewRowState.CurrentRows)
            Dim dTable As DataTable
            dTable = dv.ToTable

            DataGridView1.DataSource = dTable

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class