Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 27th, 2008, 09:00 AM
Authorized User
 
Join Date: May 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic Buttons,SaveToolStrip, adding records

Hi all im hoping someone could give me some advice, I need help trying to understand excatly why records wont save in my database, when the dynamic save button is pressed records wont add to the database same for the toolStrip with the exeption that the toolstrip will update records so im assuming the problem is with how im trying to create a record, I have worked on this extensively for three weeks and am reaching desperation. The code looks like...

Public Function CreateEmployee(ByVal ID As Integer, ByVal NewPerson As ClassEmployee) As Boolean

        Dim CreateEmployeesAdapter As New DataPlasterDataSetTableAdapters.EmployeesTableAdap ter
        Dim CreateEmployeesTable As New DataPlasterDataSet.EmployeesDataTable

        CreateEmployeesAdapter.Fill(CreateEmployeesTable)

        Dim MyRows() As DataPlasterDataSet.EmployeesRow = CType(CreateEmployeesTable.Select("ID = " & ID.ToString), DataPlasterDataSet.EmployeesRow())

        If MyRows.Length > 0 Then
            With NewPerson

                CreateEmployeesTable.AddEmployeesRow(.FirstName, .LastName, .Address, .Phone, .Mobile, .WeekEnding, .DaysWorked, .PayPerDay, .Wages)
                CreateEmployeesAdapter.Update(CreateEmployeesTable )
                CreateEmployeesAdapter.Fill(CreateEmployeesTable)

            End With
        End If

 End Function

 'to determine which button was clicked
    Private Sub ButtonClickedHandler(ByVal sender As System.Object, _
           ByVal e As System.EventArgs)

        Dim btnSender As Button = CType(sender, Button)
        If btnSender.Name = "btnSave" Then
            If Me.ValidateChildren() = True Then

                RaiseEvent ButtonClicked(1)
            Else
                MessageBox.Show("Please enter the first and last names")
                If txtFirstName.Text = vbNullString Then
                    txtFirstName.Focus()
                Else
                    txtLastName.Focus()
                End If
            End If
        ElseIf btnSender.Name = "btnCancel" Then
            RaiseEvent ButtonClicked(2)
        End If
    End Sub
Private Sub objEmployees_ButtonClicked(ByVal iButtonType As Integer) Handles objEmployees.ButtonClicked
        'If Dynamic save button pressed then save
        Select Case iButtonType
            Case 1
                If CreateEmployee(mID, objEmployees.ClassEmployee) Then
                    objEmployees = New Employees
                    'remove the employees control
                    If objEmployees IsNot Nothing Then
                        PnlMain.Controls.Remove(objEmployees)
                        objEmployees = Nothing
                    End If
                    'show list control
                    PnlMain.Controls.Add(objList)
                    'resize to fill
                    objList.Dock = DockStyle.Fill
                Else
                    MessageBox.Show("Person WAS NOT added successfully")
                End If
            Case 2
                'remove employees control
                If objEmployees IsNot Nothing Then
                    PnlMain.Controls.Remove(objEmployees)
                    objEmployees = Nothing
                End If
        End Select
    End Sub

Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click

        'SAVE OR UPDATE employees DEPENDING ON THE STATE OF ADDMODE T/F

        If objEmployees IsNot Nothing Then
            If objEmployees.AddMode = True Then
                If CreateEmployee(mID + 1, objEmployees.ClassEmployee) Then
                    MessageBox.Show("Person WAS added successfully")
                    objList = New List
                    If objEmployees IsNot Nothing Then
                        PnlMain.Controls.Remove(objEmployees)
                        objEmployees = Nothing
                    End If
                    PnlMain.Controls.Add(objList)
                    objList.Dock = DockStyle.Fill
                Else
                    MessageBox.Show("Person WAS NOT added successfully")
                End If
            Else
                If UpdatePerson(mID, objEmployees.ClassEmployee) Then
                    MessageBox.Show("Person WAS updated successfully")
                Else
                    MessageBox.Show("Person WAS NOT updated successfully")
                End If
            End If
        End If
End sub

any pointers will be greatly appreciated, Thanks in advance

 
Old May 27th, 2008, 09:35 AM
Authorized User
 
Join Date: May 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Another version ive come up with also throws errors this time a nullpointexception, i changed the createEmployee function to AddPerson but again with no success the addPerson funtion looks like

 Public Function AddPerson(ByVal ID As Integer, ByVal NewPerson As ClassEmployee) 'As Boolean

        Dim AddPersonAdapter As New DataPlasterDataSetTableAdapters.EmployeesTableAdap ter
        Dim AddPersonTable As New DataPlasterDataSet.EmployeesDataTable
        AddPersonAdapter.Fill(AddPersonTable)
        With NewPerson

            AddPersonTable.AddEmployeesRow(.FirstName, .LastName, .Address, .Phone, .Mobile, .WeekEnding, .DaysWorked, .PayPerDay, .Wages)
            AddPersonAdapter.Update(AddPersonTable)
            AddPersonAdapter.Fill(AddPersonTable)

        End With


        Return True
    End Function


'If Dynamic save button pressed then save
        Select Case iButtonType
            Case 1
                If AddPerson(mID, objEmployees.ClassEmployee) Then
                    objEmployees = New Employees
                    'remove the employees control
                    If objEmployees IsNot Nothing Then
                        PnlMain.Controls.Remove(objEmployees)
                        objEmployees = Nothing
                    End If
                    'show list control
                    PnlMain.Controls.Add(objList)
                    'resize to fill

                    objList.Dock = DockStyle.Fill
                Else
                    MessageBox.Show("Person WAS NOT added successfully")
                End If
but the error occurs in the
objList.Dock = DockStyle.fill
line thats where the nullpointexception is thrown any help on either of these funtions would be greatly recieved


 
Old May 28th, 2008, 12:46 PM
Authorized User
 
Join Date: May 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dont worry eventually i just cheated and used the bindsource to add records and it works just fine






Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Buttons in PHP Rayne84 PHP How-To 0 March 16th, 2008 07:31 AM
Dynamic created buttons in Vb.Net remya1000 General .NET 1 December 6th, 2007 04:20 PM
Adding Properties to Buttons in a GridView Aaron Edwards ASP.NET 2.0 Basics 3 July 23rd, 2006 01:48 PM
dynamic buttons harpua Flash (all versions) 3 October 19th, 2004 07:33 PM
Dynamic Buttons? TSEROOGY Javascript How-To 6 September 4th, 2003 11:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.