Results 1 to 4 of 4

Thread: [RESOLVED] add new row to datagridview - problem with incorrect user input datatype

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    42

    Resolved [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!!!

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    42

    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?

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    42

    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
  •  



Click Here to Expand Forum to Full Width