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?
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?
Re: Save empty fields to access database.
Quote:
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.
Re: Save empty fields to access database.
Quote:
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.
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).