Imports System.Data.SqlClient

Public Class frm_wrongdatafrequency
    Dim da As SqlDataAdapter = Nothing
    Dim conn As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("SO_Conn"))
    Dim cmd As SqlCommand = Nothing

    Private Sub btn_wrongdata_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_wrongdata.Click
        Try
            If dtpstart.Value > dtpend.Value Then
                MessageBox.Show("Fromdate Should be Less Than Todate")
                dtpstart.Focus()
                Exit Sub
            End If
            If conn.State <> ConnectionState.Open Then
                conn.Open()
            End If
            Fillmismatchfield()
            If cmbmismatchfield.Items.Count = 0 Then
                MessageBox.Show("There is no field Mismatch in the given period.")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub frm_wrongdatafrequency_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Fillmismatchfield()
    End Sub
    Public Sub Fillgrid()
        Try
            If conn.State <> ConnectionState.Open Then
                conn.Open()
            End If
            Dim cmdtext As String = "SO_GetWrongdatafrequency"
            cmd = Nothing
            cmd = New SqlCommand(cmdtext, conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@start", Me.dtpstart.Value.ToString("d"))
            cmd.Parameters.AddWithValue("@end", Me.dtpend.Value.ToString("d"))
            cmd.Parameters.AddWithValue("@fieldname", Me.cmbmismatchfield.SelectedValue.ToString)
            cmd.Parameters.AddWithValue("@nos", CInt(Me.txtnos.Text))
            Dim ds As New DataSet
            Dim da As New SqlDataAdapter(cmd)
            da.Fill(ds)
            Dim dt As DataTable
            dt = ds.Tables(0)
            dgv_frqnwrongdata.DataSource = ds.Tables(0)
            dgv_frqnwrongdata.Columns(1).Width = 150
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub Fillmismatchfield()
        Try
            If conn.State <> ConnectionState.Open Then
                conn.Open()
            End If
            Dim cmdtext As String = "SO_Getmismatchfieldname"
            cmd = Nothing
            cmd = New SqlCommand(cmdtext, conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@start", Me.dtpstart.Value.ToString("d"))
            cmd.Parameters.AddWithValue("@end", Me.dtpend.Value.ToString("d"))
            Dim ds As New DataSet
            da = New SqlDataAdapter(cmd)
            da.Fill(ds)
            cmbmismatchfield.DataSource = ds.Tables(0)
            cmbmismatchfield.DisplayMember = "field_name"
            cmbmismatchfield.ValueMember = "field_name"

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub cmbmismatchfield_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbmismatchfield.SelectedIndexChanged
        dgv_frqnwrongdata.DataSource = Nothing
        dgv_fielddeviation.DataSource = Nothing
        dgv_personaldeviation.DataSource = Nothing
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If cmbmismatchfield.Items.Count = 0 Then
            Exit Sub
        End If
        Try
            dgv_fielddeviation.DataSource = Nothing
            dgv_personaldeviation.DataSource = Nothing
            If conn.State <> ConnectionState.Open Then
                conn.Open()
            End If
            If cmbmismatchfield.Text = "System.Data.DataRowView" Then
                Fillmismatchfield()
            Else
                Fillgrid()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub dgv_frqnwrongdata_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_frqnwrongdata.CellClick
        Try
            If e.RowIndex <> -1 Then
                Getdata()
                dgv_personaldeviation.DataSource = Nothing
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Getdata()
        Dim fieldname As String
        fieldname = dgv_frqnwrongdata.Item("Field Name", dgv_frqnwrongdata.CurrentRow.Index).Value.ToString.Trim
        If fieldname = "" Then
            Exit Sub
        End If
        Try
            If conn.State <> ConnectionState.Open Then
                conn.Open()
            End If
            Dim cmdtext As String = "SO_FieldDeviation"
            cmd = Nothing
            cmd = New SqlCommand(cmdtext, conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@fieldname", fieldname)
            Dim ds As New DataSet
            Dim da As New SqlDataAdapter(cmd)
            da.Fill(ds)
            Dim dt As DataTable
            dt = ds.Tables(0)
            dgv_fielddeviation.DataSource = ds.Tables(0)
            dgv_fielddeviation.Columns(2).Width = 70
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub dgv_fielddeviation_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_fielddeviation.CellClick
        Try
            If e.RowIndex <> -1 Then
                PersonalDeviation()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub PersonalDeviation()
        Dim value As String
        Dim name As String = ""
        name = dgv_fielddeviation.Item("DEP Name", dgv_fielddeviation.CurrentRow.Index).Value.ToString.Trim
        If name = "" Then
            Exit Sub
        End If
        Dim fieldname As String = dgv_fielddeviation.Item("Field", dgv_fielddeviation.CurrentRow.Index).Value
        If fieldname = "date_installed" Then
            Dim value1 As Date = dgv_fielddeviation.Item("Value", dgv_fielddeviation.CurrentRow.Index).Value
            value = value1.ToString("yyyy-MM-dd HH:mm:ss.000")
        Else
            value = dgv_fielddeviation.Item("Value", dgv_fielddeviation.CurrentRow.Index).Value
            If value = "False" Then
                value = 0
            ElseIf value = "True" Then
                value = 1
            End If
        End If
        Try
            If conn.State <> ConnectionState.Open Then
                conn.Open()
            End If
            Dim cmdtext As String = "SO_Personaldeviation"
            cmd = Nothing
            cmd = New SqlCommand(cmdtext, conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@name", name)
            cmd.Parameters.AddWithValue("@fieldname", fieldname)
            cmd.Parameters.AddWithValue("@value", value)
            Dim ds As New DataSet
            Dim da As New SqlDataAdapter(cmd)
            da.Fill(ds)
            Dim dt As DataTable
            dt = ds.Tables(0)
            If fieldname = "f_stub" Or fieldname = "f_inserted" Or fieldname = "f_retired" Or fieldname = "f_leak" Or fieldname = "f_split" Or fieldname = "f_noblow" Or fieldname = "f_gassource" Or fieldname = "f_tappingtee" Or fieldname = "f_linesealed" Or fieldname = "f_defcoatcor" Or fieldname = "f_multimeter" Or fieldname = "f_serstop" Or fieldname = "f_cathodpro" Or fieldname = "f_tee" Or fieldname = "f_taptee" Or fieldname = "f_galvanized" Or fieldname = "f_insulated" Or fieldname = "f_anoderiser" Or fieldname = "f_relocate" Or fieldname = "f_replace" Then
                Dim value1 As String = dgv_fielddeviation.Item("Value", dgv_fielddeviation.CurrentRow.Index).Value
                If value1 = "False" Then
                    If ds.Tables(0).Rows(0).Item(3) = 0 Then
                        ds.Tables(0).Rows(0).Item(3) = "False"
                    End If
                End If
                If value1 = "True" Then
                    If ds.Tables(0).Rows(0).Item(3) = 1 Then
                        ds.Tables(0).Rows(0).Item(3) = "True"
                    End If
                End If
            End If
            dgv_personaldeviation.DataSource = ds.Tables(0)
            dgv_personaldeviation.Columns(0).Width = 40
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub dgv_personaldeviation_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_personaldeviation.CellClick
        Try
            If e.RowIndex <> -1 Then
                Openimage()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Openimage()
        
        Dim fileid As String = dgv_personaldeviation.Item("Fileid", dgv_personaldeviation.CurrentRow.Index).Value.ToString.Trim
        If fileid = "" Then
            Exit Sub
        End If
        Try
            commonUser.Common.imageid = fileid
            Dim newquickview As New frm_quickview()
            newquickview.ShowDialog()
            newquickview.BringToFront()
            newquickview.Focus()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class