Need lesson in bindingcontext, example below...

Need lesson in bindingcontext, example below...

Post by jeff » Sat, 17 May 2003 03:52:59

1) Create new windows VB application.
2) Add new windows form.
3) Add this code to the Code Window:

Imports System.Data.SqlClient
Imports System.Data
Imports System

Public Class AdminUsers
    Inherits System.Windows.Forms.Form

    ' Our data adapters (for each table).
    Private UserAdapter As SqlDataAdapter
    Private RoleAdapter As SqlDataAdapter
    Private URAdapter As SqlDataAdapter

    ' Command builders.
    Private UserBuilder As SqlCommandBuilder = New SqlCommandBuilder()
    Private RoleBuilder As SqlCommandBuilder = New SqlCommandBuilder()
    Private URBuilder As SqlCommandBuilder = New SqlCommandBuilder()

    ' The dataset.
    Private dsUser As New DataSet("Users")
    ' Hold original role
    Private iOriginalRole As Integer
    Private SQLConn As New SqlConnection()

#Region " Windows Form Designer generated code "

    Public Sub New()
        Dim sConn As String

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call
        ' Create adapters.
        sConn = "server=(local);" + _
        "USER ID=sa;" + _
        "PASSWORD=999999;" + _
        "database=TEST;" + _

        SQLConn.ConnectionString = sConn

            UserAdapter = New SqlDataAdapter("Select * from Users", SQLConn)
            RoleAdapter = New SqlDataAdapter("Select * from Roles", SQLConn)
            URAdapter = New SqlDataAdapter("Select * from UserRoles",

            UserAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
            RoleAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
            URAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

        Catch e As Exception
        End Try

        ' Auto gen commands.
        UserBuilder = New SqlCommandBuilder(UserAdapter)
        RoleBuilder = New SqlCommandBuilder(RoleAdapter)
        URBuilder = New SqlCommandBuilder(URAdapter)

        ' Fill tables into DS.

            UserAdapter.Fill(dsUser, "Users")
            RoleAdapter.Fill(dsUser, "Roles")
            URAdapter.Fill(dsUser, "UR")
        Catch e As Exception
        End Try

        ' Build relations.
        AddHandler cmbGroup.SelectedIndexChanged, AddressOf Me.NewRole
    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
            End If
        End If
    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 Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents pnlNav As System.Windows.Forms.Panel
    Friend WithEvents btnNavFirst As System.Windows.Forms.Button
    Friend WithEvents btnNavPrev As System.Windows.Forms.Button
    Friend WithEvents lblNavLocation As System.Windows.Forms.Label
    Friend WithEvents btnNavNext As System.Windows.Forms.Button
    Friend WithEvents btnLast As System.Windows.Forms.Button
    Friend WithEvents btnUpdate As System.Windows.Forms.Button
    Friend WithEvents btnCancelAll As System.Windows.Forms.Button
    Friend WithEvents btnAdd As System.Windows.Forms.Button
    Friend WithEvents btnDelete As System.Windows.Forms.Button
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents tbFN As System.Windows.Forms.TextBox
    Friend WithEvents tbLN As System.Windows.Forms.TextBox
    Friend WithEvents tbEmail As System.Windows.Forms.TextBox
    Friend WithEvents tbLogin As System.Windows.Forms.TextBox
    Friend WithEvents tbPW As System.Windows.Forms.TextBox
    Friend WithEvents cmbGroup As System.Windows.Forms.ComboBox
    Friend WithEvents chkAuto As System.Windows.Forms.CheckBox
    Friend WithEvents tbUserID As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.pnlNav = New System.Windows.Forms.Panel()
        Me.btnNavFirst = New System.Windows.Forms.Button()
        Me.btnNavPrev = New System.Windows.Forms.Button()
        Me.lblNavLocation = New System.Windows.Forms.Label()
        Me.btnNavNext = New System.Windows.Forms.Button()
        Me.btnLast = New System.Windows.Forms.Button()
        Me.btnUpdate = New System.Windows.Forms.Button()
        Me.btnCancelAll = New System.Windows.Forms.Button()
        Me.btnAdd = New System.Windows.Forms.Button()
        Me.btnDelete = New System.Windows.Forms.Button()
        Me.btnCancel = New System.Windows.Forms.Button()
        Me.Label7 = New System.Windows.Forms.Label()
        Me.tbFN = New System.Windows.Forms.TextBox()
        Me.tbLN = New System.Windows.Forms.TextBox()
        Me.tbEmail = New System.Windows.Forms.TextBox()
        Me.tbLogin = New System.Windows.Forms.TextBox()
        Me.tbPW = New System.Windows.Forms.TextBox()
        Me.cmbGroup = New System.Windows.Forms.ComboBox()
        Me.chkAuto = New System.Windows.Forms.CheckBox()
        Me.tbUserID = New System.Windows.Forms.TextBox()
        Me.Label1.Location = New System.Drawing.Point(16, 22)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(100, 21)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "First Name"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.TopRight
        Me.Label2.Location = New System.Drawing.Point(16, 54)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(100, 21)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "Last Name"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.TopRight
        Me.Label3.Location = New System.Drawing.Point(16, 88)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(100, 21)
        Me.Label3.TabIndex = 2
        Me.Label3.Text = "Email"
        Me.Label3.TextAlign = System.Drawing.ContentAlignment.TopRight
        Me.Label4.Location = New System.Drawing.Point(16, 120)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(100, 21)
        Me.Label4.TabIndex = 3
        Me.Label4.Text = "Login"
        Me.Label4.TextAlign = System.Drawing.ContentAlignment.TopRight
        Me.Label5.Location = New System.Drawing.Point(16, 154)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(100, 21)
        Me.Label5.TabIndex = 4
        Me.Label5.Text = "Password"
        Me.Label5.TextAlign = System.Drawing.ContentAlignment.TopRight
        Me.Label6.Location = New System.Drawing.Point(16, 186)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(100, 21)
        Me.Label6.TabIndex = 5
        Me.Label6.Text = "Auto Search"
        Me.Label6.TextAlign = System.Drawing.ContentAlignment.TopRight
        Me.pnlNav.Controls.AddRange(New System.Windows.Forms.Control()
{Me.btnNavFirst, Me.btnNavPrev, Me.lblNavLocation,

Me.btnNavNext, Me.btnLast})
        Me.pnlNav.Location = New System.Drawing.Point(61, 290)
        Me.pnlNav.Name = "pnlNav"
        Me.pnlNav.Size = New System.Drawing.Size(310, 32)
        Me.pnlNav.TabIndex = 46
        Me.btnNavFirst.Location = New System.Drawing.Point(33, 5)
        Me.btnNavFirst.Name = "btnNavFirst"
        Me.btnNavFirst.Size = New System.Drawing.Size(40, 23)
        Me.btnNavFirst.TabIndex = 35
        Me.btnNavFirst.Text = "<<"
        Me.btnNavPrev.Location = New System.Drawing.Point(73, 5)
        Me.btnNavPrev.Name = "btnNavPrev"
        Me.btnNavPrev.Size = New System.Drawing.Size(35, 23)
        Me.btnNavPrev.TabIndex = 36
        Me.btnNavPrev.Text = "<"
        Me.lblNavLocation.BackColor = System.Drawing.Color.White
        Me.lblNavLocation.Location = New System.Drawing.Point(108, 5)
        Me.lblNavLocation.Name = "lblNavLocation"
        Me.lblNavLocation.Size = New System.Drawing.Size(95, 23)
        Me.lblNavLocation.TabIndex = 37
        Me.lblNavLocation.Text = "No Records"
        Me.lblNavLocation.TextAlign =
        Me.btnNavNext.Location = New System.Drawing.Point(203, 5)
        Me.btnNavNext.Name = "btnNavNext"
        Me.btnNavNext.Size = New System.Drawing.Size(35, 23)
        Me.btnNavNext.TabIndex = 38
        Me.btnNavNext.Text = ">"

read more »


Need lesson in bindingcontext, example below...

Post by Tian Min Hua » Tue, 20 May 2003 22:55:32

Hi Jeff,

Thanks for your posing, I am checking this issue and will update you with
my information.

Have a nice day!

This posting is provided "AS IS" with no warranties, and confers no rights.


Need lesson in bindingcontext, example below...

Post by Tian Min Hua » Thu, 29 May 2003 00:18:18


I reviewed your code carefully, and now I'd like to share the following
information with you:

Based on my research, the reason why the code cannot add the new role and
update the table Users is caused by the following problems:

1. You use the check box. There is a known issue about it as described in
the following KB article:

BUG: Check Box Is Not Cleared When You Call AddNew() Method on a DataSet

2. Since Users.idUser is a primary key, we should designate a unique value
to it in order to assign it to Users.idUser and UR.idUser. Please refer to
the following code snippet:

Private Sub BuildTableRelationship()
      dsUser.Tables("Users").Columns("iAutoSearch").DefaultValue = False
      dsUser.Tables("Users").Columns("idUser").AutoIncrement = False
      dsUser.Tables("Users").Columns("idUser").DefaultValue = 8
      dsUser.Tables("Users").Columns("FirstName").DefaultValue = "test"
      dsUser.Tables("Users").Columns("LastName").DefaultValue = "test"
      dsUser.Tables("UR").Columns("idRole").DefaultValue = 3
      dsUser.Tables("UR").Columns("idUser").DefaultValue = 8
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
      n = dsUser.Tables("Users").Columns("idUser").DefaultValue + 1
      dsUser.Tables("Users").Columns("idUser").DefaultValue = n
      dsUser.Tables("UR").Columns("idUser").DefaultValue = n

3. Now that if you click add, and move next button, we can go to the new

4. However, the problem remains when updating UR table. I suggest that you
can try to add invisible Text Boxes to bind to Users.idUser and UR.idUser,
and see if it works.

Hope them helps.

This posting is provided "AS IS" with no warranties, and confers no rights.


1. this.BindingContext[Ds,"orders"].Position versus this.BindingContext[Ds.orders].Position

What's the difference beetween
I 've got 2 differents results

Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (
Version: 6.0.423 / Virus Database: 238 - Release Date: 25/11/2002

2. General ASP.NET question (Good practices)

3. .NET Add-ins in Excel do not show up in Add-ins

4. Schematic needed OLD equipment

5. Need Placements for the below guys

6. Address of reference to incomplete type (probably DR)

7. Need it folks: Below is our hottest requirement for SAP SRM Analyst !!!!!!!!!

8. FS: Sony ML2000+Acc $700 OBO

9. Need help on Add-Ins

10. Create Office COM Add-Ins with VB .NET - need help

11. Korean Video Tale Lesson #4 - Snail Wife

12. Learn chinese culture lesson 1