|
-
Oct 22nd, 2012, 12:19 AM
#1
Thread Starter
Member
[RESOLVED] error handling when trying to add a new row with incorrect datatype
Hello everyone,
I have a Windows application that inserts new data to a mySQL database.
When the user clicks the "Add" button on the main form, a new form (Insert Form) appears. This new form contains a list of labels (for the column names) and a list of blank textboxes.
The user then fills these textboxes with the values they want to insert into the DB table.
I am using a BindingSource to update the DataGridView on the main form and the actual mySQL database.
The code is shown below.
If the user attempts to insert a new record with a wrong datatype (for example, a STRING value when the column is of datatype INTEGER), they get an error message.
When they hit "Retry" or "Cancel" on the error message, both the error message and the Insert Form close.
When they close the error message and refresh the DataGridView, they can see that the program has duplicated the last row in the table, and a new row has been added.
I have 2 problems:
1. I want just the error message to close, not the insert form.
2. When the user encounters the dataType error, I do NOT want the last row to be duplicated in the DataGridView. I have absolutely no idea why it's doing this.
Any help would be greatly appreciated.
Thank you in advance.
Code:
Public Class insertPopup
Public Property dtableMemVar As DataTable
Public Sub New(ByVal dtable As DataTable)
'This call is required by the Windows Form Designer.
InitializeComponent()
Dim dcHeader, dc As DataColumn
Dim i As Integer
Dim dt As DataTable = dtable
dtableMemVar = dtable
TableLayoutPanel1.RowCount = dt.Columns.Count
TableLayoutPanel1.ColumnCount = 2
While TableLayoutPanel1.RowStyles.Count < TableLayoutPanel1.RowCount
TableLayoutPanel1.RowStyles.Add(New RowStyle())
End While
For Each style As RowStyle In TableLayoutPanel1.RowStyles
style.SizeType = SizeType.Percent
style.Height = 100.0F
Next
dcHeader = dt.Columns(0) 'get the ID column
Dim idLabel As New Label()
idLabel.Text = (dcHeader.ColumnName + ":").ToString 'display ID column name
idLabel.Size = New System.Drawing.Size(70, 17)
TableLayoutPanel1.Controls.Add(idLabel, 0, i)
Dim lastRow As DataRow = dt.Rows(dt.Rows.Count - 1)
Dim idTextbox As New TextBox()
idTextbox.Size = New System.Drawing.Size(170, 17)
idTextbox.Text = lastRow(0) + 1 'grabs the last ID in the dgv, then +1 to that
idTextbox.Name = ("textbox0").ToString
idTextbox.ReadOnly = True
TableLayoutPanel1.Controls.Add(idTextbox, 1, i)
For i = 1 To TableLayoutPanel1.RowCount - 1
dc = dt.Columns(i)
Dim label As New Label()
label.Text = (dc.ColumnName + ":").ToString
label.Size = New System.Drawing.Size(70, 17)
TableLayoutPanel1.Controls.Add(label, 0, i)
Dim textbox As New TextBox()
textbox.Size = New System.Drawing.Size(170, 17)
textbox.Text = ""
textbox.Name = ("textbox" & i).ToString
TableLayoutPanel1.Controls.Add(textbox, 1, i)
Next
End Sub
Private Sub addRow()
On Error GoTo driveError
Dim dt As DataTable = dtableMemVar
Dim dc As DataColumn
Dim newrow As DataRow = dt.NewRow()
For i = 0 To dt.Columns.Count - 1
Dim textbox As TextBox = TableLayoutPanel1.Controls.Item("textbox" & i)
textbox.Name = ("textbox" & i).ToString
dc = dt.Columns(i)
newrow(dc.ColumnName) = textbox.Text
Next
dt.Rows.Add(newrow)
driveError:
Dim response As Integer, description As Integer
description = vbExclamation + vbRetryCancel
response = MsgBox(Err.Description, description, "Error") 'else output error message and retry
End Sub
Private Sub addButton_Click(sender As System.Object, e As System.EventArgs) Handles addButton.Click
addRow()
End Sub
End Class
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
|