Customers.vb

 Imports System.Data.SqlClient
Imports BaseFormTemplate.BaseFormTemplate

'Check how easy is to code an Entry Form with my BaseFormTemplate
'-----------------------------------------------------------------
'Help Needed : The method BaseFormTemplate.DeleteData don't work as it should work
'no error is thrown. if you can help me to fix it, I will appreciate it a lot.

Public Class Customers
  Inherits BaseFormTemplate.BaseFormTemplate

#Region " Windows Form Designer generated code "

  Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
  If Not (components Is Nothing) Then
components.Dispose()
  End If
End If
MyBase.Dispose(disposing)
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.IContainer

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer. 
  'Do not modify it using the code editor.
  Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
  Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
  Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
  Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
  Friend WithEvents Label5 As System.Windows.Forms.Label
  Friend WithEvents Label4 As System.Windows.Forms.Label
  Friend WithEvents Label3 As System.Windows.Forms.Label
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents pnlFields As System.Windows.Forms.Panel
  Friend WithEvents Label6 As System.Windows.Forms.Label
  Friend WithEvents Label7 As System.Windows.Forms.Label
  Friend WithEvents Label8 As System.Windows.Forms.Label
  Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
  Friend WithEvents TextBox6 As System.Windows.Forms.TextBox
  Friend WithEvents TextBox7 As System.Windows.Forms.TextBox
  Friend WithEvents TextBox8 As System.Windows.Forms.TextBox
  Friend WithEvents TextBox9 As System.Windows.Forms.TextBox
  Friend WithEvents TextBox10 As System.Windows.Forms.TextBox
  Friend WithEvents Label16 As System.Windows.Forms.Label
  Friend WithEvents Label17 As System.Windows.Forms.Label
  Friend WithEvents Label15 As System.Windows.Forms.Label
  Friend WithEvents TextBox11 As System.Windows.Forms.TextBox
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.pnlFields = New System.Windows.Forms.Panel
Me.TextBox10 = New System.Windows.Forms.TextBox
Me.TextBox11 = New System.Windows.Forms.TextBox
Me.Label15 = New System.Windows.Forms.Label
Me.Label16 = New System.Windows.Forms.Label
Me.Label17 = New System.Windows.Forms.Label
Me.TextBox9 = New System.Windows.Forms.TextBox
Me.TextBox8 = New System.Windows.Forms.TextBox
Me.TextBox7 = New System.Windows.Forms.TextBox
Me.TextBox6 = New System.Windows.Forms.TextBox
Me.TextBox5 = New System.Windows.Forms.TextBox
Me.Label8 = New System.Windows.Forms.Label
Me.Label7 = New System.Windows.Forms.Label
Me.Label6 = New System.Windows.Forms.Label
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Label5 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
CType(Me.grdInfo, System.ComponentModel.ISupportInitialize).BeginInit()
Me.pnlFields.SuspendLayout()
Me.SuspendLayout()
'
'grdInfo
'
Me.grdInfo.AlternatingBackColor = System.Drawing.Color.PowderBlue
Me.grdInfo.FlatMode = False
Me.grdInfo.Name = "grdInfo"
Me.grdInfo.Size = New System.Drawing.Size(132, 172)
'
'pnlFields
'
Me.pnlFields.AutoScroll = True
Me.pnlFields.BackColor = System.Drawing.SystemColors.Control
Me.pnlFields.Controls.Add(Me.TextBox10)
Me.pnlFields.Controls.Add(Me.TextBox11)
Me.pnlFields.Controls.Add(Me.Label15)
Me.pnlFields.Controls.Add(Me.Label16)
Me.pnlFields.Controls.Add(Me.TextBox9)
Me.pnlFields.Controls.Add(Me.TextBox8)
Me.pnlFields.Controls.Add(Me.TextBox7)
Me.pnlFields.Controls.Add(Me.TextBox6)
Me.pnlFields.Controls.Add(Me.TextBox5)
Me.pnlFields.Controls.Add(Me.Label8)
Me.pnlFields.Controls.Add(Me.Label7)
Me.pnlFields.Controls.Add(Me.Label6)
Me.pnlFields.Controls.Add(Me.TextBox4)
Me.pnlFields.Controls.Add(Me.TextBox3)
Me.pnlFields.Controls.Add(Me.TextBox2)
Me.pnlFields.Controls.Add(Me.TextBox1)
Me.pnlFields.Controls.Add(Me.Label5)
Me.pnlFields.Controls.Add(Me.Label4)
Me.pnlFields.Controls.Add(Me.Label3)
Me.pnlFields.Controls.Add(Me.Label2)
Me.pnlFields.Controls.Add(Me.Label1)
Me.pnlFields.Controls.Add(Me.Label17)
Me.pnlFields.Dock = System.Windows.Forms.DockStyle.Fill
Me.pnlFields.Enabled = False
Me.pnlFields.Location = New System.Drawing.Point(0, 203)
Me.pnlFields.Name = "pnlFields"
Me.pnlFields.Size = New System.Drawing.Size(584, 218)
Me.pnlFields.TabIndex = 15
'
'TextBox10
'
Me.TextBox10.Location = New System.Drawing.Point(108, 112)
Me.TextBox10.Name = "TextBox10"
Me.TextBox10.Size = New System.Drawing.Size(96, 21)
Me.TextBox10.TabIndex = 33
Me.TextBox10.Text = "TextBox10"
'
'TextBox11
'
Me.TextBox11.Location = New System.Drawing.Point(208, 112)
Me.TextBox11.Name = "TextBox11"
Me.TextBox11.Size = New System.Drawing.Size(96, 21)
Me.TextBox11.TabIndex = 34
Me.TextBox11.Text = "TextBox11"
'
'Label15
'
Me.Label15.AutoSize = True
Me.Label15.BackColor = System.Drawing.SystemColors.Control
Me.Label15.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label15.Location = New System.Drawing.Point(208, 96)
Me.Label15.Name = "Label15"
Me.Label15.Size = New System.Drawing.Size(26, 17)
Me.Label15.TabIndex = 20
Me.Label15.Text = "Fax:"
'
'Label16
'
Me.Label16.AutoSize = True
Me.Label16.BackColor = System.Drawing.SystemColors.Control
Me.Label16.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label16.Location = New System.Drawing.Point(108, 96)
Me.Label16.Name = "Label16"
Me.Label16.Size = New System.Drawing.Size(39, 17)
Me.Label16.TabIndex = 18
Me.Label16.Text = "Phone:"
'
'Label17
'
Me.Label17.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Label17.AutoSize = True
Me.Label17.BackColor = System.Drawing.SystemColors.Control
Me.Label17.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label17.Location = New System.Drawing.Point(408, 52)
Me.Label17.Name = "Label17"
Me.Label17.Size = New System.Drawing.Size(47, 17)
Me.Label17.TabIndex = 16
Me.Label17.Text = "Country:"
'
'TextBox9
'
Me.TextBox9.Location = New System.Drawing.Point(8, 112)
Me.TextBox9.Name = "TextBox9"
Me.TextBox9.Size = New System.Drawing.Size(96, 21)
Me.TextBox9.TabIndex = 31
Me.TextBox9.Text = "TextBox9"
'
'TextBox8
'
Me.TextBox8.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TextBox8.Location = New System.Drawing.Point(408, 68)
Me.TextBox8.Name = "TextBox8"
Me.TextBox8.Size = New System.Drawing.Size(172, 21)
Me.TextBox8.TabIndex = 29
Me.TextBox8.Text = "TextBox8"
'
'TextBox7
'
Me.TextBox7.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TextBox7.Location = New System.Drawing.Point(328, 68)
Me.TextBox7.Name = "TextBox7"
Me.TextBox7.Size = New System.Drawing.Size(76, 21)
Me.TextBox7.TabIndex = 27
Me.TextBox7.Text = "TextBox7"
'
'TextBox6
'
Me.TextBox6.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TextBox6.Location = New System.Drawing.Point(208, 68)
Me.TextBox6.Name = "TextBox6"
Me.TextBox6.Size = New System.Drawing.Size(116, 21)
Me.TextBox6.TabIndex = 25
Me.TextBox6.Text = "TextBox6"
'
'TextBox5
'
Me.TextBox5.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TextBox5.Location = New System.Drawing.Point(8, 68)
Me.TextBox5.Name = "TextBox5"
Me.TextBox5.Size = New System.Drawing.Size(196, 21)
Me.TextBox5.TabIndex = 23
Me.TextBox5.Text = "TextBox5"
'
'Label8
'
Me.Label8.AutoSize = True
Me.Label8.BackColor = System.Drawing.SystemColors.Control
Me.Label8.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label8.Location = New System.Drawing.Point(8, 96)
Me.Label8.Name = "Label8"
Me.Label8.Size = New System.Drawing.Size(55, 17)
Me.Label8.TabIndex = 14
Me.Label8.Text = "PostCode:"
'
'Label7
'
Me.Label7.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Label7.AutoSize = True
Me.Label7.BackColor = System.Drawing.SystemColors.Control
Me.Label7.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label7.Location = New System.Drawing.Point(328, 52)
Me.Label7.Name = "Label7"
Me.Label7.Size = New System.Drawing.Size(42, 17)
Me.Label7.TabIndex = 12
Me.Label7.Text = "Region:"
'
'Label6
'
Me.Label6.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Label6.AutoSize = True
Me.Label6.BackColor = System.Drawing.SystemColors.Control
Me.Label6.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label6.Location = New System.Drawing.Point(208, 52)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(27, 17)
Me.Label6.TabIndex = 10
Me.Label6.Text = "City:"
'
'TextBox4
'
Me.TextBox4.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TextBox4.Location = New System.Drawing.Point(480, 24)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.TabIndex = 7
Me.TextBox4.Text = "TextBox4"
'
'TextBox3
'
Me.TextBox3.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TextBox3.Location = New System.Drawing.Point(284, 24)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(192, 21)
Me.TextBox3.TabIndex = 5
Me.TextBox3.Text = "TextBox3"
'
'TextBox2
'
Me.TextBox2.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TextBox2.Location = New System.Drawing.Point(80, 24)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(200, 21)
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = "TextBox2"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(8, 24)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(68, 21)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = "TextBox1"
'
'Label5
'
Me.Label5.AutoSize = True
Me.Label5.BackColor = System.Drawing.SystemColors.Control
Me.Label5.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label5.Location = New System.Drawing.Point(8, 52)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(48, 17)
Me.Label5.TabIndex = 8
Me.Label5.Text = "Address:"
'
'Label4
'
Me.Label4.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Label4.AutoSize = True
Me.Label4.BackColor = System.Drawing.SystemColors.Control
Me.Label4.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label4.Location = New System.Drawing.Point(480, 8)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(30, 17)
Me.Label4.TabIndex = 6
Me.Label4.Text = "Title:"
'
'Label3
'
Me.Label3.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Label3.AutoSize = True
Me.Label3.BackColor = System.Drawing.SystemColors.Control
Me.Label3.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label3.Location = New System.Drawing.Point(284, 8)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(46, 17)
Me.Label3.TabIndex = 4
Me.Label3.Text = "Contact:"
'
'Label2
'
Me.Label2.BackColor = System.Drawing.SystemColors.Control
Me.Label2.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label2.Location = New System.Drawing.Point(80, 8)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(96, 17)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Name:"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.BackColor = System.Drawing.SystemColors.Control
Me.Label1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(20, 17)
Me.Label1.TabIndex = 0
Me.Label1.Text = "ID:"
'
'Customers
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
Me.ClientSize = New System.Drawing.Size(584, 421)
Me.Controls.Add(Me.pnlFields)
Me.Name = "Customers"
Me.Text = "Customers"
Me.Controls.SetChildIndex(Me.pnlFields, 0)
CType(Me.grdInfo, System.ComponentModel.ISupportInitialize).EndInit()
Me.pnlFields.ResumeLayout(False)
Me.ResumeLayout(False)

  End Sub

#End Region

  Private Sub Customers_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myConnectionString As String = "Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;"
Dim SQLcnn As New SqlConnection(myConnectionString)

'Assign the connection to the Form
Me.DBConnection = SQLcnn

'Load the Table(s) into the DataSet : Example : Me.LoadDatabaseData("Table0", "Table1", "Table2", "Table3",...)
Me.LoadDatabaseData("Customers")

DefineDataRelations()
DefineGridColumns()
FillControls()
BindControls()
BindParameters()

  End Sub


  Private Sub Customers_StatusHasChanged(ByVal NewStatus As enmStatusTransaction) Handles MyBase.StatusHasChanged

Select Case NewStatus
  Case Is = enmStatusTransaction.AddMode, enmStatusTransaction.EditMode
pnlFields.Enabled = True

  Case Is = enmStatusTransaction.NormalMode
pnlFields.Enabled = False

End Select
  End Sub

  Private Sub DefineDataRelations()
'Use this method if you have more than one Table in your DataSet and those tables need to be related
'also you can change the BindControls to make you controls behave like should be.
'Me.AddDataRelation("NameOfRelation", "FromTable", "Fromfield", "ToTable", "ToField")
  End Sub

  Private Sub DefineGridColumns()

'Create the comlums to be displayed in Datagrid
Try
  Me.AddGridColumn("CustomerID", "ID", 75, "")
  Me.AddGridColumn("CompanyName", "Name", 200, "")
  Me.AddGridColumn("ContactName", "Contact", 200, "")

Catch ex As Exception
  MessageBox.Show(ex.GetBaseException.ToString, "Error in " & ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
End Try

  End Sub

  Private Sub FillControls()

'Fill the DataGrid with the table
Try

  Me.FillGrid("Customers")

Catch ex As Exception
  MessageBox.Show(ex.GetBaseException.ToString, "Error in " & ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
End Try

  End Sub

  Private Sub BindControls()

'Bind the form controls to its datasource

Try
  TextBox1.DataBindings.Add(New Binding("Text", Me.grdInfo.DataSource, "CustomerID"))
  TextBox2.DataBindings.Add(New Binding("Text", Me.grdInfo.DataSource, "CompanyName"))
  TextBox3.DataBindings.Add(New Binding("Text", Me.grdInfo.DataSource, "ContactName"))
  TextBox4.DataBindings.Add(New Binding("Text", Me.grdInfo.DataSource, "ContactTitle"))
  TextBox5.DataBindings.Add(New Binding("Text", Me.grdInfo.DataSource, "Address"))
  TextBox6.DataBindings.Add(New Binding("Text", Me.grdInfo.DataSource, "City"))
  TextBox7.DataBindings.Add(New Binding("Text", Me.grdInfo.DataSource, "Region"))
  TextBox8.DataBindings.Add(New Binding("Text", Me.grdInfo.DataSource, "PostalCode"))
  TextBox9.DataBindings.Add(New Binding("Text", Me.grdInfo.DataSource, "Country"))
  TextBox10.DataBindings.Add(New Binding("Text", Me.grdInfo.DataSource, "Phone"))
  TextBox11.DataBindings.Add(New Binding("Text", Me.grdInfo.DataSource, "Fax"))

Catch ex As Exception
  MessageBox.Show(ex.GetBaseException.ToString, "Error in " & ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
End Try

  End Sub

  Private Sub BindParameters()
'This method "AddParameterControl" is used to link the variable name to variable value on form.
Try
  Me.AddParameterControl("@CustomerID", TextBox1, DBNull.Value, enmDataType.typText, True)
  Me.AddParameterControl("@CompanyName", TextBox2, DBNull.Value, enmDataType.typText, False)
  Me.AddParameterControl("@ContactName", TextBox3, DBNull.Value, enmDataType.typText, False)
  Me.AddParameterControl("@ContactTitle", TextBox4, DBNull.Value, enmDataType.typText, False)
  Me.AddParameterControl("@Address", TextBox5, DBNull.Value, enmDataType.typText, False)
  Me.AddParameterControl("@City", TextBox6, DBNull.Value, enmDataType.typText, False)
  Me.AddParameterControl("@Region", TextBox7, DBNull.Value, enmDataType.typText, False)
  Me.AddParameterControl("@PostalCode", TextBox8, DBNull.Value, enmDataType.typText, False)
  Me.AddParameterControl("@Country", TextBox9, DBNull.Value, enmDataType.typText, False)
  Me.AddParameterControl("@Phone", TextBox10, DBNull.Value, enmDataType.typText, False)
  Me.AddParameterControl("@Fax", TextBox11, DBNull.Value, enmDataType.typText, False)

Catch ex As Exception
  MessageBox.Show(ex.GetBaseException.ToString, "Error in " & ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
End Try
  End Sub

End Class

Project Homepage: