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:
Thank you ever so much!!!
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