Saturday, 24 November 2012

VB.NET Form Design with code


MODULE CODE

Imports System.Data.OleDb
Module Module1
    Public Cn As New OleDbConnection("Provider=MSDAORA.1;user id=scott;password=tiger")
    Public Fdt As String, Tdt As String, Opt As String, Cont As Int16, Agn As Int16

    Public Sub ConnectDB()
        If Cn.State = ConnectionState.Closed Then Cn.Open()
    End Sub

    Public Function Gen_PK(ByVal Fld As String, ByVal Tbl As String) As Integer
        ConnectDB()
        Dim Cmd As New OleDbCommand("Select Max(" & Fld & ") from " & Tbl)
        Cmd.Connection = Cn

        If Not IsDBNull(Cmd.ExecuteScalar) Then
            Return Cmd.ExecuteScalar + 1
        Else
            Return 1
        End If
        Cmd.Dispose()
    End Function
End Module


STAFF FORM CODE

Imports System.Data.OleDb
Public Class Staff

Private Sub Staff_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DataSet1.KSTAFF' table. You can move, or remove it, as needed.
        Me.KSTAFFTableAdapter.Fill(Me.DataSet1.KSTAFF)
        Btnenb()
        LckCnt()
    End Sub

    Private Sub Btnenb()
        BtnA.Enabled = True
        BtnS.Enabled = False
        BtnE.Enabled = True
        BtnC.Enabled = False
        BtnEX.Enabled = True
    End Sub

    Private Sub Btndenb()
        BtnA.Enabled = False
        BtnS.Enabled = True
        BtnE.Enabled = False
        BtnC.Enabled = True
        BtnEX.Enabled = False
    End Sub

    Private Sub LckCnt()
        Dim obj As Control
        For Each obj In GroupBox1.Controls
            If TypeOf obj Is TextBox Then
                obj.Enabled = False
                obj.Text = ""
            End If
        Next
    End Sub
    Private Sub UnLckCnt()
        Dim obj As Control
        For Each obj In GroupBox1.Controls
            If TypeOf obj Is TextBox Then
                obj.Enabled = True
            End If
        Next
    End Sub

Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTP2.ValueChanged
    End Sub

Private Sub BtnA_Click(ByVal sender As System.Object, ByVal e As    System.EventArgs) Handles BtnA.Click
        Btndenb()
        UnLckCnt()
        TxtSlno.Text = Module1.Gen_PK("SNO", "kstaff")
        TxtFname.Focus()
End Sub

Private Sub BtnS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnS.Click
Dim cmd As New OleDbCommand
ConnectDB()
cmd.Connection = Cn
If BtnS.Text = "Save" Then
cmd.CommandText = "insert into KSTAFF values(" & TxtSlno.Text & " ,'" & TxtFname.Text & "','" & TxtMname.Text & "','" & TxtLname.Text & "','" & TxtAddr.Text & "','" & DTP1.Text & "','" & TxtCity.Text & "'," & TxtPcode.Text & " ," & TxtMob.Text & ",'" & DTP2.Text & "','" & CmbDes.Text & "','" & CmbDept.Text & "')"
cmd.ExecuteNonQuery()
MsgBox("Record Saved")
ElseIf BtnS.Text = "Update" Then
cmd.CommandText = " update kstaff set FNAME ='" & TxtFname.Text & "',MNAME ='" & TxtMname.Text & "', LNAME ='" & TxtLname.Text & "',ADDR ='" & TxtAddr.Text & "', DOB ='" & DTP1.Text & "',CITY ='" & TxtCity.Text & "',PCODE =" & TxtPcode.Text & " , MOB =" & TxtMob.Text & " , DOJ ='" & DTP2.Text & "', DES ='" & CmbDes.Text & "', DEPT ='" & CmbDept.Text & "' where SNO = " & TxtSlno.Text
cmd.ExecuteNonQuery()
MsgBox("Record Modified", MsgBoxStyle.Information, "Setup")
End If
DataSet1.Clear()
Me.KSTAFFTableAdapter.Fill(Me.DataSet1.KSTAFF)
Btnenb()
LckCnt()
BtnS.Text = "Save"
End Sub

Private Sub BtnE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnE.Click
If TxtSlno.Text = "" Then
MsgBox("Select Record to Modify", MsgBoxStyle.Critical, "Setup")
Exit Sub
End If
Btndenb()
UnLckCnt()
BtnS.Text = "Update"
End Sub

Private Sub BtnC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnC.Click
Dim r As String
r = MsgBox("Do you want to cancel", MsgBoxStyle.YesNo, "Warning")
If r = vbYes Then
Me.Close()
Else
TxtFname.Focus()
End If
End Sub


Private Sub BtnEX_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEX.Click
Me.Close()
End Sub

   

  
Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
        TxtSlno.Text = DataGridView1.SelectedCells(0).Value
        TxtFname.Text = DataGridView1.SelectedCells(1).Value
        TxtMname.Text = DataGridView1.SelectedCells(2).Value
        TxtLname.Text = DataGridView1.SelectedCells(3).Value
        TxtAddr.Text = DataGridView1.SelectedCells(4).Value
        DTP1.Text = DataGridView1.SelectedCells(5).Value
        TxtCity.Text = DataGridView1.SelectedCells(6).Value
        TxtPcode.Text = DataGridView1.SelectedCells(7).Value
        TxtMob.Text = DataGridView1.SelectedCells(8).Value
        DTP2.Text = DataGridView1.SelectedCells(9).Value
        CmbDes.Text = DataGridView1.SelectedCells(10).Value
        CmbDept.Text = DataGridView1.SelectedCells(11).Value
End Sub

  
End Class



Get CSV Values in SQL

Query: DECLARE @CSVValue NVARCHAR(50)='100,101,102'                   DECLARE @eachValue NUMERIC(9,0)   while len(@CSVValue) ...