Advertisement
calfred2808

mainform database (infoSystem - jordan)

Jan 19th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.99 KB | None | 0 0
  1. Imports Microsoft.SqlServer.Server
  2. Imports System.Data.SqlClient
  3.  
  4. Public Class frmMain
  5.     Dim mToggle, mModifyData, mSelectRecord As Integer
  6.     Dim txt, cbo As Control
  7.  
  8.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  9.         mToggle = 0
  10.         ModifyControls()
  11.         'bind the data to the controls
  12.         BindSex()
  13.         BindCivilstatus()
  14.         BindCitizenship()
  15.         BindGridview()
  16.     End Sub
  17.  
  18.     Private Sub ModifyControls()
  19.         Select Case mToggle
  20.             Case 0
  21.                 For Each txt In Me.Controls
  22.                     If TypeOf txt Is TextBox Then
  23.                         txt.Enabled = False
  24.                     End If
  25.                 Next
  26.                 For Each cbo In Me.Controls
  27.                     If TypeOf cbo Is ComboBox Then
  28.                         cbo.Enabled = False
  29.                     End If
  30.                 Next
  31.                 dtpBirthdate.Enabled = False
  32.                 mnuSave.Enabled = False
  33.                 mnuCancel.Enabled = False
  34.  
  35.                 grdInformation.Enabled = True
  36.                 mnuAdd.Enabled = True
  37.             Case 1
  38.                 For Each txt In Me.Controls
  39.                     If TypeOf txt Is TextBox Then
  40.                         txt.Enabled = True
  41.                     End If
  42.                 Next
  43.                 For Each cbo In Me.Controls
  44.                     If TypeOf cbo Is ComboBox Then
  45.                         cbo.Enabled = True
  46.                     End If
  47.                 Next
  48.                 dtpBirthdate.Enabled = True
  49.                 mnuSave.Enabled = True
  50.                 mnuCancel.Enabled = True
  51.  
  52.                 grdInformation.Enabled = False
  53.                 mnuAdd.Enabled = False
  54.                 mnuEdit.Enabled = False
  55.                 mnuDelete.Enabled = False
  56.         End Select
  57.     End Sub
  58.  
  59.     Private Sub BindSex()
  60.         BindCombobox("select * from dbo.gender", "sex", "id", cboSex)
  61.     End Sub
  62.  
  63.     Private Sub BindCivilstatus()
  64.         BindCombobox("select * from dbo.civilstatus", "civilstatus", "id", cboCivilstatus)
  65.     End Sub
  66.  
  67.     Private Sub BindCitizenship()
  68.         BindCombobox("select * from dbo.citizenship", "citizenship", "id", cboCitizenship)
  69.     End Sub
  70.  
  71.     Private Sub ResetControls()
  72.         ClearTextbox(Me)
  73.         dtpBirthdate.Value = Now
  74.         cboSex.SelectedIndex = 0
  75.         cboCivilstatus.SelectedIndex = 0
  76.         cboCitizenship.SelectedIndex = 0
  77.     End Sub
  78.  
  79.     Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
  80.         'required validation
  81.         If txtSurname.Text = "" Then MsgBox("Please fillup the field surname!", MsgBoxStyle.Critical, "Save error") _
  82.             : txtSurname.Focus() : Exit Sub
  83.         If txtFirstname.Text = "" Then MsgBox("Please fillup the field first name!", MsgBoxStyle.Critical, "Save error") _
  84.             : txtFirstname.Focus() : Exit Sub
  85.         If txtMiddlename.Text = "" Then MsgBox("Please fillup the field middle name!", MsgBoxStyle.Critical, "Save error") _
  86.             : txtMiddlename.Focus() : Exit Sub
  87.         If txtBirthplace.Text = "" Then MsgBox("Please fillup the field place of birth!", MsgBoxStyle.Critical, "Save error") _
  88.             : txtBirthplace.Focus() : Exit Sub
  89.         If txtAddress.Text = "" Then MsgBox("Please fillup the field address!", MsgBoxStyle.Critical, "Save error") _
  90.             : txtAddress.Focus() : Exit Sub
  91.         'saving to the database
  92.         Select Case mModifyData
  93.             Case 0
  94.                 Try
  95.                     OpenConnection()
  96.                     objCmd = New SqlCommand("insert into dbo.personalinfo(fname,lname,mname,suffix,birthdate," & _
  97.                         "birthplace,sex,civilstatus,citizenship,address,zipcode,telephone) " & _
  98.                         "values(@fname,@lname,@mname,@suffix,@birthdate,@birthplace,@sex," & _
  99.                         "@civilstatus,@citizenship,@address,@zipcode,@telephone)", objCon)
  100.                     objCmd.Parameters.Clear()
  101.                     With objCmd.Parameters
  102.                         .AddWithValue("@fname", txtFirstname.Text.ToUpper)
  103.                         .AddWithValue("@lname", txtSurname.Text.ToUpper)
  104.                         .AddWithValue("@mname", txtMiddlename.Text.ToUpper)
  105.                         .AddWithValue("@suffix", txtSuffix.Text.ToUpper)
  106.                         .AddWithValue("@birthdate", dtpBirthdate.Text)
  107.                         .AddWithValue("@birthplace", txtBirthplace.Text.ToUpper)
  108.                         .AddWithValue("@sex", cboSex.SelectedValue)
  109.                         .AddWithValue("@civilstatus", cboCivilstatus.SelectedValue)
  110.                         .AddWithValue("@citizenship", cboCitizenship.SelectedValue)
  111.                         .AddWithValue("@address", txtAddress.Text.ToUpper)
  112.                         .AddWithValue("@zipcode", txtZipcode.Text.ToUpper)
  113.                         .AddWithValue("@telephone", txtTelephone.Text.ToUpper)
  114.                     End With
  115.                     objCmd.ExecuteNonQuery()
  116.                     BindGridview()
  117.                     MsgBox("Information successfully saved!", MsgBoxStyle.Information, "Save complete")
  118.                 Catch ex As Exception
  119.                     MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Save error")
  120.                 Finally
  121.                     CloseConnection()
  122.                     ResetControls()
  123.                     mToggle = 0
  124.                     ModifyControls()
  125.                 End Try
  126.             Case 1
  127.                 Try
  128.                     OpenConnection()
  129.                     objCmd = New SqlCommand("update dbo.personalinfo set fname=@fn,lname=@ln,mname=@mn,suffix=@sf,birthdate=@bd," & _
  130.                         "birthplace=@bp,sex=@sx,civilstatus=@cs,citizenship=@cz,address=@ad,zipcode=@zc,telephone=@tp " & _
  131.                         "where id='" & grdInformation.Item(0, grdInformation.CurrentRow.Index).Value & "'", objCon)
  132.                     objCmd.Parameters.Clear()
  133.                     With objCmd.Parameters
  134.                         .AddWithValue("@fn", txtFirstname.Text.ToUpper)
  135.                         .AddWithValue("@ln", txtSurname.Text.ToUpper)
  136.                         .AddWithValue("@mn", txtMiddlename.Text.ToUpper)
  137.                         .AddWithValue("@sf", txtSuffix.Text.ToUpper)
  138.                         .AddWithValue("@bd", dtpBirthdate.Text)
  139.                         .AddWithValue("@bp", txtBirthplace.Text.ToUpper)
  140.                         .AddWithValue("@sx", cboSex.SelectedValue)
  141.                         .AddWithValue("@cs", cboCivilstatus.SelectedValue)
  142.                         .AddWithValue("@cz", cboCitizenship.SelectedValue)
  143.                         .AddWithValue("@ad", txtAddress.Text.ToUpper)
  144.                         .AddWithValue("@zc", txtZipcode.Text.ToUpper)
  145.                         .AddWithValue("@tp", txtTelephone.Text.ToUpper)
  146.                     End With
  147.                     objCmd.ExecuteNonQuery()
  148.                     BindGridview()
  149.                     MsgBox("Information successfully updated!", MsgBoxStyle.Information, "Update complete")
  150.                 Catch ex As Exception
  151.                     MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Update error")
  152.                 Finally
  153.                     CloseConnection()
  154.                     ResetControls()
  155.                     mToggle = 0
  156.                     ModifyControls()
  157.                 End Try
  158.         End Select
  159.     End Sub
  160.  
  161.     Private Sub BindGridview()
  162.         'populating the gridview from the database
  163.         Try
  164.             OpenConnection()
  165.             objDa = New SqlDataAdapter("select dbo.personalinfo.id as ID, lname + ', ' + fname + ' ' + left(mname, 1) + '.' as Name, " & _
  166.                 "birthdate as 'Birth Date', birthplace as 'Birth Place', dbo.gender.sex as Sex, " & _
  167.                 "dbo.civilstatus.civilstatus as 'Civil Status', dbo.citizenship.citizenship as Citizenship, " & _
  168.                 "address as Address, zipcode as 'Zip Code', telephone as Telephone from dbo.personalinfo " & _
  169.                 "inner join dbo.gender on dbo.personalinfo.sex=dbo.gender.id inner join " & _
  170.                 "dbo.civilstatus on dbo.personalinfo.civilstatus=dbo.civilstatus.id inner join " & _
  171.                 "dbo.citizenship on dbo.personalinfo.citizenship=dbo.citizenship.id order by dbo.personalinfo.id", objCon)
  172.             objDt = New DataTable
  173.             objDa.Fill(objDt)
  174.             grdInformation.DataSource = objDt
  175.             lblCount.Text = grdInformation.Rows.Count
  176.             If objDt.Rows.Count > 0 Then
  177.                 mnuEdit.Enabled = True
  178.                 mnuDelete.Enabled = True
  179.             Else
  180.                 mnuEdit.Enabled = False
  181.                 mnuDelete.Enabled = False
  182.             End If
  183.         Catch ex As Exception
  184.             MsgBox(ex.Message)
  185.         Finally
  186.             CloseConnection()
  187.         End Try
  188.     End Sub
  189.  
  190.     Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
  191.         Application.Exit()
  192.     End Sub
  193.  
  194.     Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAdd.Click
  195.         mToggle = 1
  196.         ModifyControls()
  197.         mModifyData = 0
  198.         txtSurname.Focus()
  199.     End Sub
  200.  
  201.     Private Sub mnuCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCancel.Click
  202.         mToggle = 0
  203.         ModifyControls()
  204.         ResetControls()
  205.         BindGridview()
  206.     End Sub
  207.  
  208.     Private Sub mnuDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDelete.Click
  209.         mSelectRecord = grdInformation.Item(0, grdInformation.CurrentRow.Index).Value
  210.         If MsgBox("Are you sure you want to delete the selected record?", _
  211.             MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2 + MsgBoxStyle.Question, "Delete") = MsgBoxResult.Yes Then
  212.             Try
  213.                 OpenConnection()
  214.                 objCmd = New SqlCommand("delete from dbo.personalinfo where id='" & mSelectRecord & "'", objCon)
  215.                 objCmd.ExecuteNonQuery()
  216.                 BindGridview()
  217.                 MsgBox("The record has been deleted!", MsgBoxStyle.Information, "Delete")
  218.             Catch ex As Exception
  219.                 MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Delete error")
  220.             Finally
  221.                 CloseConnection()
  222.             End Try
  223.         End If
  224.     End Sub
  225.  
  226.     Private Sub mnuEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEdit.Click
  227.         mModifyData = 1
  228.         mSelectRecord = grdInformation.Item(0, grdInformation.CurrentRow.Index).Value
  229.         Try
  230.             OpenConnection()
  231.             objCmd = New SqlCommand("select * from dbo.personalinfo where id='" & mSelectRecord & "'", objCon)
  232.             objDr = objCmd.ExecuteReader()
  233.             If objDr.HasRows = True Then
  234.                 objDr.Read()
  235.                 txtSurname.Text = objDr(2).ToString()
  236.                 txtFirstname.Text = objDr(1).ToString()
  237.                 txtMiddlename.Text = objDr(3).ToString()
  238.                 txtSuffix.Text = objDr(4).ToString()
  239.                 dtpBirthdate.Value = objDr(5).ToString()
  240.                 txtBirthplace.Text = objDr(6).ToString()
  241.                 cboSex.SelectedValue = objDr(7).ToString()
  242.                 cboCivilstatus.SelectedValue = objDr(8).ToString()
  243.                 cboCitizenship.SelectedValue = objDr(9).ToString()
  244.                 txtAddress.Text = objDr(10).ToString()
  245.                 txtZipcode.Text = objDr(11).ToString()
  246.                 txtTelephone.Text = objDr(12).ToString()
  247.             End If
  248.         Catch ex As Exception
  249.             MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Edit error")
  250.         Finally
  251.             CloseConnection()
  252.             mToggle = 1
  253.             ModifyControls()
  254.         End Try
  255.     End Sub
  256.  
  257.     Private Sub mnuMain_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles mnuMain.ItemClicked
  258.  
  259.     End Sub
  260. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement