|
-
Feb 12th, 2013, 04:05 PM
#1
Thread Starter
Lively Member
Save empty fields to access database.
Hi!
I have a code that saves data to access database. But I can't figure out how to make it work when a field is left blank.
Code:
MainForm.RihmatabelTableAdapter.Insert(Me.ComboProfiil.Text, Me.PikkusTextBox.Text, _
Me.OriginaaltoodeCheckBox.Checked.ToString, Me.OriginaalkoodTextBox.Text, _
Me.TootekoodTextBox.Text, Me.SobivusTextBox.Text, Me.ForgardenCheckBox.Checked.ToString, _
Me.ReginettCheckBox.Checked.ToString, Me.VärvusTextBox.Text, Me.HindTextBox.Text, _
Me.MärkmedTextBox.Text)
MainForm.RihmatabelTableAdapter.Fill(MainForm.RihmadDataSet.Rihmatabel)
MsgBox("Uus toode lisatud!", MsgBoxStyle.Information, "Rihmade Andmebaas")
How should I edit my code so that it allows to save even when some of the textboxes are left empty?
-
Feb 12th, 2013, 04:16 PM
#2
Re: Save empty fields to access database.
It shouldn't make any difference if all fields are text. "" is as much a valid value as any other text. What problems have you experienced?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Feb 12th, 2013, 04:24 PM
#3
Thread Starter
Lively Member
Re: Save empty fields to access database.
 Originally Posted by dunfiddlin
It shouldn't make any difference if all fields are text. "" is as much a valid value as any other text. What problems have you experienced?
Few fields are numeric fields so it gives me an error: Conversion from string "" to type Double is not valid.
-
Feb 12th, 2013, 04:47 PM
#4
Re: Save empty fields to access database.
 Originally Posted by lkallas
Few fields are numeric fields so it gives me an error: Conversion from string "" to type Double is not valid.
There you go... You have to test for empty strings. If a string is empty and that field allows null, you substitute it with DBNull.Value; if the string is empty and your field doesn't allow null then you need to use the default value for that field, probably zero; otherwise, convert the string to a Double.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Feb 12th, 2013, 04:48 PM
#5
Re: Save empty fields to access database.
Ah, right. Well in that case you should use variables to pass values or use Numeric Up/Down controls (with a default value of 0) instead of text boxes for those fields that require numbers. To convert textbox entries to variables use ...
Dim r As Double
Double.TryParse(TextBox1.Text, r)
r = 0 when the conversion fails for any reason (including empty text).
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|