|
-
Oct 29th, 2012, 12:18 AM
#1
Thread Starter
Member
[RESOLVED] add new row to datagridview - problem with incorrect user input datatype
Hi all,
I have a dataGridView and an insert button on the main form. When the user clicks this insert button, a second form appears with a list of labels (column names) and textboxes (blank).
When the user clicks the INSERT button on this new form, the datagridview is updated with a new row containing the new data.
Now, the problem is when the user clicks the INSERT button without having visited the textboxes for INTEGER or DOUBLE columns, something bizarre happens.
The program takes the LAST record in that database table and duplicates it in the dataGridView & the actual database table.
Please look at my code and tell me why it's doing this.
I have a textbox validation function that does forbids the user from entering any characters in a textbox of column field INT or DOUBLE.
But if the user doesn't even visit this textbox and tries to insert a BLANK in an INT or DOUBLE textbox, this duplication problem occurs.
What I want the program to do is, show a message box saying "Column 'ABC' cannot be left blank!" and then allow the user to try again.
Here is my attempt:
Code:
Private Sub addRow()
Dim dt As DataTable = dtableMemVar
Dim dc As DataColumn
Dim newrow As DataRow = dt.NewRow()
Dim datatype As String
Dim label As String
Dim successful As Boolean
Try
For i = 0 To dt.Columns.Count - 1
Dim textbox As TextBox = TableLayoutPanel1.Controls.Item("textbox" & i) 'each textbox is labelled as textbox0, textbox1, etc.
dc = dt.Columns(i)
datatype = (dc.DataType).ToString 'getting the datatype of that table column
label = dc.ColumnName.ToString
If datatype = "System.Int32" And textbox.Text.Trim = "" Then 'if the column's datatype is an integer and the textbox is blank, then show an error message and DO NOTHING
MsgBox("Column '" + label + "' cannot be left blank.")
successful = False
Else
newrow(dc.ColumnName) = textbox.Text 'else add the data into the new row
End If
Next
If successful = True Then
dt.Rows.Add(newrow)
Else
Exit Sub
End If
Catch ex As Exception
MsgBox("Could not insert record. " & ex.Message)
End Try
End Sub
Thank you ever so much!!!
-
Oct 29th, 2012, 08:47 AM
#2
Re: add new row to datagridview - problem with incorrect user input datatype
Hello,
It is your responsibility to check if the TextBox controls are blank then if this is okay supply default values before inserting a new row otherwise I am sure you will have issues.
Assertion example for Integer, same can be done for Decmal and Double etc.
Code:
Dim Value As Int32 = 0
If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then
If Integer.TryParse(TextBox1.Text, Value) Then
'
' We have a valid int
'
Else
'
' Not an integer
'
End If
Else
'
' Ask user to enter a value or
' you supply a default value here
'
End If
-
Oct 29th, 2012, 11:57 PM
#3
Thread Starter
Member
Re: add new row to datagridview - problem with incorrect user input datatype
Thanks for your help.
My database tables contain no "NO NULL" columns. But for ints, I need a default value of 0 in case the user leaves the textbox blank.
I want a default value of "" for strings, varchars, etc.
How do I check if the column is of type int or double, and only for those insert a default value of 0?
-
Oct 29th, 2012, 11:59 PM
#4
Thread Starter
Member
Re: add new row to datagridview - problem with incorrect user input datatype
Ah nvm, I got it. Thanks a lot for your help.
Tags for this Thread
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
|