Rows keeps inserting in database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qazplm114477
    New Member
    • Oct 2008
    • 19

    Rows keeps inserting in database

    Hi, i have a problem with a small program i wrote which adds vendors to the database. i created a windows form and made it into a dialog that adds vendors to my database. When I open the dialog to add vendors and click add, everything gets saved into the database without any errors, the problem is that even if the entire form is empty and there are some fields like LastName that doesn't allow nulls, the data still gets saved into the database with no problems.

    here is the entire code:

    Code:
    Public Sub AddVendor()
            StatusLabel.Text = "Creating new vendor"
            Dim myAddVendorDialog As New AddVendorDialog()
    
            myAddVendorDialog.VendorNumber = 0
            Dim result As DialogResult
            result = myAddVendorDialog.ShowDialog
            If result = Windows.Forms.DialogResult.OK Then
                Try
                    Dim Vendors As VendorViewDataSet.TBL_VENDORRow
                    Vendors = myVendorViewDataSet.TBL_VENDOR.NewTBL_VENDORRow
    
                    Vendors.LastName = myAddVendorDialog.LastName
                    Vendors.CompanyName = myAddVendorDialog.CompanyName1
                    Vendors.PostalCode = myAddVendorDialog.PostalCode
                    Vendors.FirstName = myAddVendorDialog.FirstName
                    Vendors.Address = myAddVendorDialog.Address
                    Vendors.City = myAddVendorDialog.City
                    Vendors._Region = myAddVendorDialog.Region1
                    'Vendors.PostalCode = myAddVendorDialog.PostalCode
                    Vendors.Country = myAddVendorDialog.Country
                    Vendors.ContactNumber = myAddVendorDialog.ContactNumber
                    Vendors.Email = myAddVendorDialog.Email
    
                    myVendorViewDataSet.TBL_VENDOR.AddTBL_VENDORRow(Vendors)
    
                    Dim rowsAffected As Integer = 0
                    rowsAffected = TbL_VENDORTableAdapter2.Update(Vendors)
    
                    If rowsAffected > 0 Then
                        StatusLabel.Text = "New Vendor - '" & myAddVendorDialog.LastName & "'"
                        Dim AddAnotherVendor As DialogResult
                        AddAnotherVendor = MessageBox.Show("No problems encountered.   " & _
                                        "Do you want to add another vendor?", "Sucessfull", MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
                                        MessageBoxDefaultButton.Button3)
                        If AddAnotherVendor = Windows.Forms.DialogResult.Yes Then
    
                            Call AddVendor()
                        Else
                            AddVendorDialog.Close()
                        End If
                    Else
                        StatusLabel.Text = "Problem creating new Vendor.  Could not save into the database."
                    End If
                Catch ex As Exception
                    MessageBox.Show("Problem creating new Vendor: " & ex.Message)
                End Try
            Else
                StatusLabel.Text = "New Vendor operation cancelled."
    
            End If
            myAddVendorDialog = Nothing
        End Sub
    The code works fine, its just that it keeps saving data even if there isnt any information in the form.

    Thanks to anybody who is willing to help in anyway.

    PS: Im using Visual Studio 2008 and SQL Server 2005
  • qazplm114477
    New Member
    • Oct 2008
    • 19

    #2
    Hi again, i think i solved part of the problems here. i just added the following piece of code

    Code:
    If AddVendorDialog.CompanyNameTxt.Text = "" Then
                        MessageBox.Show("Enter More Information")
                        AddVendorDialog.Close()
    Else
                     ' Continue adding
    End If
    However with this piece of code added, i cant even add records at all. even if the form is filled correctly, it just dosent allow me to add, the messagebox just keeps popping up.help me please T_T
    Last edited by qazplm114477; Nov 2 '08, 08:01 AM. Reason: wooops

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      You need to check for all the required fields are filled before passing those to database. If any filed is not filled re-prompt to enter ther value for the field.

      Comment

      • qazplm114477
        New Member
        • Oct 2008
        • 19

        #4
        I did try to use the if statement to check if the textbox in the dialog form has no value, but now the message box pops up even if all the required fields are populated making it impossible to add records

        Comment

        • qazplm114477
          New Member
          • Oct 2008
          • 19

          #5
          Originally posted by debasisdas
          You need to check for all the required fields are filled before passing those to database. If any filed is not filled re-prompt to enter ther value for the field.
          I've just understood what you were trying to say. I managed to fix the problem adding validation to the dataset itself. thanks ^_^

          Comment

          • qazplm114477
            New Member
            • Oct 2008
            • 19

            #6
            Does anyone know the Line of code used in VB so that the Form doesn't close after the user clicks the OK button on the message box?

            it kinda goes like this
            (Button Click event)
            If Textbox1.text = "" then
            Messagebox.show ("error")
            'after click the Ok button, the form closes. what code do i use so that it doesn't close?
            else
            .........contin ue with the adding process
            end if

            thanks in advance ^_^

            Comment

            • qazplm114477
              New Member
              • Oct 2008
              • 19

              #7
              anyone? any help would be greatly appreciated

              Comment

              • debasisdas
                Recognized Expert Expert
                • Dec 2006
                • 8119

                #8
                the lines of code that you have mentioned will not close the form. is there any other coe following the messagebox based on user response ?

                Comment

                Working...