Friday, February 15, 2013

VB CODE FOR DESKTOP APPLICATION (ISNUMERIC = TRUE / FALSE CHECK)


ISNUMERIC USAGE IN THIS CODE

Imports System.Data.SqlClient
Imports System.Data

Public Class frmJobCard
    Dim con As SqlConnection
    Dim dr As SqlDataReader
    Dim da As SqlDataAdapter
    Dim da1 As SqlDataAdapter
    Dim ds As DataSet
    Dim ds1 As DataSet
    Dim s1 As String
    Dim cmd As SqlCommand
    Dim s As String
    Dim objcon As New ConnectionString

    Private Sub frmJobCard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnEdit.Visible = False
        DtpDate.CustomFormat = DateTimePickerFormat.Custom
        DtpDate.CustomFormat = "d-MMM-yyyy"
        Me.Size = New Size(595, 181)
        Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
        Me.Top = 10
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim con As SqlConnection = New SqlConnection("Data Source=KAUSHAL-6DC0C02\KAUSHAL; Initial Catalog=MotoSoftDB; Integrated Security=True; MultipleActiveResultSets=True")
        Dim intMaxPartiID As Integer = MaxParticularID() + 1
        If RequiredFieldValidator() = False Then
            Exit Sub
        End If
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        s = "Insert into tblJobparticular (ParticularID, Particular, Qty, Rate, Amount, Job_No) Values (@ParticularID, @Particular, @Qty, @Rate, @Amount, @Job_No)"
        cmd = New SqlCommand(s, con)

        With cmd
            .Parameters.AddWithValue("@ParticularID", intMaxPartiID)
            .Parameters.AddWithValue("@Particular", Trim(txtParticular.Text))
            .Parameters.AddWithValue("@Qty", Trim(txtQty.Text))
            .Parameters.AddWithValue("@Rate", CDbl(Trim(txtRate.Text)))
            .Parameters.AddWithValue("@Amount", CDbl(Trim(txtAmount.Text)))
            .Parameters.AddWithValue("@Job_No", CInt(Trim(lblJobNo.Text)))
        End With
        cmd.ExecuteNonQuery()
        con.Close()

        Load_DataGridview()
        lblEstimateCost.Text = TotalCost()

        txtParticular.Text = ""
        txtAmount.Text = ""
        txtQty.Text = ""
        txtRate.Text = ""
        txtParticular.Focus()

    End Sub
    Public Function RequiredFieldValidator() As Boolean
        If txtJobName.Text = "" Then
            MsgBox("Fill Required fields")
            txtJobName.BackColor = Color.Gold
            txtParticular.BackColor = Color.Gold
            Return False
        Else
            Return True
        End If


    End Function
    Public Function TotalCost() As Double
        Dim dblTotalcost As Double = 0
        Dim con As SqlConnection = New SqlConnection(objCon.constr)
        con.Open()

        s = "Select sum(Amount) from tblJobParticular where Job_No = " & CInt(lblJobNo.Text)
        cmd = New SqlCommand(s, con)
        dr = cmd.ExecuteReader
        If dr.HasRows Then
            dr.Read()
            dblTotalcost = dr.GetDouble(0)
            Return dblTotalcost
        Else
            Return 0.0
        End If

    End Function
    Public Sub Load_DataGridview()

        Dim con As SqlConnection = New SqlConnection(objCon.constr)
        con.Open()
        s1 = "Select * from tblJobParticular where Job_No = " & CInt(lblJobNo.Text)
        da1 = New SqlDataAdapter(s1, con)
        ds1 = New DataSet
        da1.Fill(ds1, "tblJobParticular")
        DataGridView1.DataSource = ds1
        DataGridView1.DataMember = "tblJobParticular"
        con.Close()
    End Sub

 
    Private Sub txtAddRec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAddRec.Click
        Dim con As SqlConnection = New SqlConnection(objCon.constr)
        If RequiredFieldValidator() = False Then
            Exit Sub
        End If
        con.Open()
        lblJobNo.Text = MaxJobNo() + 1
        s = "Insert into tblJob (Job_No, Name, Job_Date, Job_Name) values (@Job_No, @Name, @Job_Date, @Job_Name)"
        cmd = New SqlCommand(s, con)
        With cmd
            .Parameters.AddWithValue("@Job_No", CInt(Trim(lblJobNo.Text)))
            .Parameters.AddWithValue("@Name", Trim(txtName.Text))
            .Parameters.AddWithValue("@Job_Date", CDate(Trim(DtpDate.Text)))
            .Parameters.AddWithValue("@Job_Name", Trim(txtJobName.Text))

        End With
        cmd.ExecuteNonQuery()

        txtParticular.Focus()

        Me.Size = New Size(595, 634)
        con.Close()
        btnEdit.Visible = True

    End Sub

    Public Function MaxParticularID() As Integer
        Dim intParticularID As Integer
        Dim con As SqlConnection = New SqlConnection(objCon.constr)
        Try

            con.Open()
            s = "Select max(ParticularID) from tblJobParticular where job_No = " & CInt(lblJobNo.Text)
            cmd = New SqlCommand(s, con)
            dr = cmd.ExecuteReader
            If dr.HasRows Then
                dr.Read()
                intParticularID = dr.GetInt32(0)

                Return intParticularID
                con.Close()
            Else
                MsgBox("There is no particular yet Added")
                cmd.Dispose()
                Return intParticularID = 1
            End If

        Catch ex As Exception
            ' MsgBox("First record")
            con.Close()
            Return intParticularID = 1

        End Try


    End Function

    Public Function MaxJobNo() As Integer
        Dim intJobNo As Integer
        Dim con As SqlConnection = New SqlConnection(objCon.constr)
        con.Open()
        s = "Select max(Job_No) from tblJob"
        cmd = New SqlCommand(s, con)
        dr = cmd.ExecuteReader
        If dr.HasRows Then
            dr.Read()
            intJobNo = dr.GetInt32(0)
            Return intJobNo
        Else
            'MsgBox("This is your first record")
            Return intJobNo = 1

        End If

    End Function

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim con As SqlConnection = New SqlConnection(objCon.constr)
        con.Open()
        'refer the diary to delete the selected cell from Datagridview and fill the gridview again.
        ' s = "Delete from tblJobParticular where ParticularID = '" & DataGridView1.SelectedColumns.Item("SrNo") & "'"
        For Each row As DataGridViewRow In DataGridView1.Rows
            If row.Cells("Choose").Value Then
                If MsgBox("Do you really don't want " & row.Cells("Particular").Value, MsgBoxStyle.YesNo, "Delete Record") = MsgBoxResult.Yes Then
                    s = "Delete from tblJobParticular where ParticularID = " & row.Cells("ParticularID").Value
                    cmd = New SqlCommand(s, con)
                    cmd.ExecuteNonQuery()
                    DataGridView1.Rows.Remove(row)

                End If
            End If
        Next
        lblEstimateCost.Text = TotalCost()
        MsgBox("Record Deleted successfully", MsgBoxStyle.Information)
        'code for deleting the record from the datagrid


    End Sub


    Private Sub txtRate_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRate.Leave
        If Not IsNumeric(txtRate.Text) Then
            MsgBox("Rate must be in numeric", MsgBoxStyle.Critical)
            txtRate.Focus()
            txtRate.SelectAll()
        End If
    End Sub

    Private Sub txtAmount_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount.Leave
        If Not IsNumeric(txtAmount.Text) Then
            MsgBox("Amount must be in numeric", MsgBoxStyle.Critical)
        End If
    End Sub


    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        'code to update the jobparticulat table
        Try
            Dim con As SqlConnection = New SqlConnection(objCon.constr)
            con.Open()

            Dim intRowCount As Integer = DataGridView1.Rows.Count - 2

            For i = 0 To intRowCount
                'MsgBox("updating row no. :" & DataGridView1.Rows(i).Cells("ParticularID").Value)
                'checking updated Rate and Amount are numeric or not
                If Not IsNumeric(DataGridView1.Rows(i).Cells("Rate").Value) Or IsNumeric(DataGridView1.Rows(i).Cells("Amount").Value) Then
                    MsgBox("Rate / Amount must be numeric in nature", MsgBoxStyle.Exclamation)
                End If
                s = "Update tblJobParticular set Particular = '" & DataGridView1.Rows(i).Cells("Particular").Value & "', Qty = '" & DataGridView1.Rows(i).Cells("Qty").Value & "', Rate = '" & DataGridView1.Rows(i).Cells("Rate").Value & "' , Amount = '" & DataGridView1.Rows(i).Cells("Amount").Value & "' where Job_No = " & CInt(lblJobNo.Text) & " and ParticularID = " & DataGridView1.Rows(i).Cells("ParticularID").Value
                cmd = New SqlCommand(s, con)
                cmd.ExecuteNonQuery()

            Next
            MsgBox("Record Updated Successfully", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox("Problem in last row performation")
        End Try
       
    End Sub
    Public Sub Update_tblJobParticular()
        Dim con As SqlConnection = New SqlConnection(objCon.constr)
        con.Open()
        s = "Select * from tblJobParticular"
        da = New SqlDataAdapter(s, con)
        ds = New DataSet
        da.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)

        'DataGridView1.DataMember = "tblJobParticular"
        da.Update(ds)

    End Sub

 
    Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
        Me.Close() '' Update_tblJobParticular()
        'Dim sqlcmdbld As New SqlCommandBuilder
        'sqlcmdbld.GetUpdateCommand()
        'da1.UpdateCommand = sqlcmdbld.GetUpdateCommand()

        'da1.Update(ds1, "tblJobParticular")

    End Sub

    'Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    '    Dim rowdelete As Integer = DataGridView1.RowCount
    '    DataGridView1.Rows.RemoveAt(rowdelete)
    'End Sub

 
   
    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Try
            Dim con As SqlConnection = New SqlConnection(objCon.constr)
            con.Open()
            s = "Update tblJob set Name = '" & Trim(txtName.Text) & "', job_Date = '" & CDate(Trim(DtpDate.Value)) & "', Job_Name = '" & txtJobName.Text & "' where job_No = " & CInt(lblJobNo.Text)
            cmd = New SqlCommand(s, con)
            cmd.ExecuteNonQuery()
            MsgBox("Record Updated Successfully", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
       
    End Sub
End Class

No comments:

Post a Comment