473,507 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rows keeps inserting in database

20 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. Public Sub AddVendor()
  2.         StatusLabel.Text = "Creating new vendor"
  3.         Dim myAddVendorDialog As New AddVendorDialog()
  4.  
  5.         myAddVendorDialog.VendorNumber = 0
  6.         Dim result As DialogResult
  7.         result = myAddVendorDialog.ShowDialog
  8.         If result = Windows.Forms.DialogResult.OK Then
  9.             Try
  10.                 Dim Vendors As VendorViewDataSet.TBL_VENDORRow
  11.                 Vendors = myVendorViewDataSet.TBL_VENDOR.NewTBL_VENDORRow
  12.  
  13.                 Vendors.LastName = myAddVendorDialog.LastName
  14.                 Vendors.CompanyName = myAddVendorDialog.CompanyName1
  15.                 Vendors.PostalCode = myAddVendorDialog.PostalCode
  16.                 Vendors.FirstName = myAddVendorDialog.FirstName
  17.                 Vendors.Address = myAddVendorDialog.Address
  18.                 Vendors.City = myAddVendorDialog.City
  19.                 Vendors._Region = myAddVendorDialog.Region1
  20.                 'Vendors.PostalCode = myAddVendorDialog.PostalCode
  21.                 Vendors.Country = myAddVendorDialog.Country
  22.                 Vendors.ContactNumber = myAddVendorDialog.ContactNumber
  23.                 Vendors.Email = myAddVendorDialog.Email
  24.  
  25.                 myVendorViewDataSet.TBL_VENDOR.AddTBL_VENDORRow(Vendors)
  26.  
  27.                 Dim rowsAffected As Integer = 0
  28.                 rowsAffected = TbL_VENDORTableAdapter2.Update(Vendors)
  29.  
  30.                 If rowsAffected > 0 Then
  31.                     StatusLabel.Text = "New Vendor - '" & myAddVendorDialog.LastName & "'"
  32.                     Dim AddAnotherVendor As DialogResult
  33.                     AddAnotherVendor = MessageBox.Show("No problems encountered.   " & _
  34.                                     "Do you want to add another vendor?", "Sucessfull", MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
  35.                                     MessageBoxDefaultButton.Button3)
  36.                     If AddAnotherVendor = Windows.Forms.DialogResult.Yes Then
  37.  
  38.                         Call AddVendor()
  39.                     Else
  40.                         AddVendorDialog.Close()
  41.                     End If
  42.                 Else
  43.                     StatusLabel.Text = "Problem creating new Vendor.  Could not save into the database."
  44.                 End If
  45.             Catch ex As Exception
  46.                 MessageBox.Show("Problem creating new Vendor: " & ex.Message)
  47.             End Try
  48.         Else
  49.             StatusLabel.Text = "New Vendor operation cancelled."
  50.  
  51.         End If
  52.         myAddVendorDialog = Nothing
  53.     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
Nov 2 '08 #1
7 1189
qazplm114477
20 New Member
Hi again, i think i solved part of the problems here. i just added the following piece of code

Expand|Select|Wrap|Line Numbers
  1. If AddVendorDialog.CompanyNameTxt.Text = "" Then
  2.                     MessageBox.Show("Enter More Information")
  3.                     AddVendorDialog.Close()
  4. Else
  5.                  ' Continue adding
  6. End If
  7.  
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
Nov 2 '08 #2
debasisdas
8,127 Recognized Expert Expert
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.
Nov 2 '08 #3
qazplm114477
20 New Member
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
Nov 2 '08 #4
qazplm114477
20 New Member
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 ^_^
Nov 3 '08 #5
qazplm114477
20 New Member
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
.........continue with the adding process
end if

thanks in advance ^_^
Nov 3 '08 #6
qazplm114477
20 New Member
anyone? any help would be greatly appreciated
Nov 4 '08 #7
debasisdas
8,127 Recognized Expert Expert
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 ?
Nov 4 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
24092
by: Xizor | last post by:
Ok, what I want to do is find out the number of rows in a table. The most obvious solution is to do something like the following: $sql = "SELECT blah FROM blah WHERE 1"; $result =...
4
75830
by: Raj Kotaru | last post by:
Hi, In sqlplus, I can insert a single row using: insert into employee (name, salary, hiredate) values ('xyz', '86378', sysdate); Is there a modification of the insert command that will...
0
2419
by: Marko Poutiainen | last post by:
Situation: We had to make our SQLServer 2000 database multi-lingual. That is, certain things (such as product names) in the database should be shown in the language the user is using (Finnish,...
1
577
by: g_chime | last post by:
How can I make MySQL reject duplicate rows when inserting? The table is quite large: 100+ rows and millions of rows. MySQL doesn't allow me to create a UNIQUE INDEX for all the rows (16 rows...
7
3908
by: GaryB | last post by:
I have an untyped datatable that has financial numbers and controls that were populated by code (not a simple fill from a DA). Now I want to insert subtotals into it. I wrote a sub to do so that...
2
1904
by: Hervé Piedvache | last post by:
Hi, I have a database with one table with about 22 millions records. I have a script every day wich insert about 200 000 records ... Sometime my script takes 4 hours to insert the data, sometime...
8
7847
by: kingskippus | last post by:
I don't know if this is possible, but I haven't been able to find any information. I have two tables, for example: Table 1 (two columns, id and foo) id foo --- ----- 1 foo_a 2 foo_b
2
3578
by: aeblank | last post by:
THE PROBLEM I'm running into performance issues generating and storing a randomly created graph in a SQL Server database. I have a T-SQL script that generates a graph, and then randomly connects the...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.